DEV Community

Cover image for PYTHON(LOOP SATEMENT)
priyaganth k
priyaganth k

Posted on

PYTHON(LOOP SATEMENT)

loop definition

A loop in Python is used to repeat a block of code multiple times automatically. Instead of writing the same code again and again this loop function is used .

The process will be repeated until the code get false.

There are two types of loop statement:

  1. For loop
  2. while loop

for loop

knows how many steps to perform.
Used when the number of repetation is known by the user.

syntax;
"for variable in sequence"
code block

Examples

for i in range(5):
    print(i)
Enter fullscreen mode Exit fullscreen mode

output
0
1
2
3
4

Explanation

The given value is 5 so the value will print from 0 to 4 total 5 values.
I stores the current value for each iteration.

while loop

It repeate the process until the statement get false....

syntax
" while condition"
code block

Examples

count = 0
while (count < 3):
    cnt = count + 1
    print("Hello Geek")
Enter fullscreen mode Exit fullscreen mode

output
Hello geek
Hello geek
Hello geek

explanation

The output will be printed, until the statement get false.

Discussed problems:

  1. sum of numbers.
  2. voting age problems.

Reference
(https://www.geeksforgeeks.org/python/loops-in-python/)
(https://www.w3schools.com/Python/python_for_loops.asp)

Top comments (0)