A for loop is used for iterating over a sequence.
Sequence in python may be:
list
tuple
dictionary
set
string
Print each number in a numbers list:
#list
numbers = [1, 2, 3, 4, 5, 6]
for number in numbers:
print(number)
Output:
1
2
3
4
5
6
Even strings are iterable objects, they contain a sequence of characters:
for char in "Codersarts":
print(char)
Output:
C
o
d
e
r
s
a
r
t
s
range() Function in Python
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.
for x in range(5):
print(x)
Output:
0
1
2
3
4