Python for Loop Statements
The most used looping control statement in Python is the for loop. For loop is used to iterate over a range of items like the list, tuples, strings or other iterable objects.
The syntax of for loop in python is given below.
text = "Demo" for val in text: print(val)
e
m
o
for val in range(0,10): print(val, end="\t") print("\nEnd of loop")
End of loop
numbers = [1, 2, 3] for val in numbers: print(val)
2
3
for loop with else
We can use an optional block with for loop that will execute once the loop is terminated. But one thing to notice is that, if the loop contains any loop control statement ( i.e. break, continue or pass ), then the else block is not executed.
numbers = [1, 2, 3] for val in numbers: print(val) else: print("Loop terminated")
2
3
Loop terminated