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:
- For loop
- 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)
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")
output
Hello geek
Hello geek
Hello geek
explanation
The output will be printed, until the statement get false.
Discussed problems:
- sum of numbers.
- voting age problems.
Reference
(https://www.geeksforgeeks.org/python/loops-in-python/)
(https://www.w3schools.com/Python/python_for_loops.asp)












