1. For loop in Python
The for
loop in Python helps iterate through elements in list, set, string, etc. Syntax:
for val in sequence:
#loop body
Where val is the value of each element in sequence (list, set, string, etc.) iterated through in each loop. The loop will continue until we iterate to the last element in the sequence. The body of the for loop starts with indentation.
Example Python program iterating through list, string
# List of numbers
numbers = [6, 5, 3, 8, 4]
print("List Iteration")
for i in numbers:
print(i)
# String
s = "Gochocit"
print("String Iteration")
for i in s:
print(i)
Result
List Iteration
6
5
3
8
4
String Iteration
G
o
c
h
o
c
i
t
2. For loop with range() function
In Python, the built-in function range() is used to define a sequence of integers. The range() function often help to determine the number of iterations for a for loop. The syntax of the range() function is:
range(start, stop[, step])
All parameters of the range() function must be integers. Specifically:
- start: the starting value of the integer sequence. If the start parameter is not specified, the default value is 0.
- stop: the value that determines the end of the integer sequence. Note: the last integer in the sequence is stop – 1.
- step: an integer that determines the distance between integers in the sequence. If the step parameter is not specified, the default value is 1.
Note: if the range() function has only one argument, that argument is passed as the stop value.
For example:
print("#Result 1:")
# start=0, stop=7, step=1
for i in range(7):
print(i, end=' ')
print("\n#Result 2:")
# start=1, stop=7, step=1
for j in range(1, 7):
print(j, end=' ')
print("\n#Result 3:")
# start=1, stop=7, step=2
for k in range(1, 7, 2):
print(k, end=' ')
print("\n#Result 4:")
# start=1, stop=-10, step=-1
for k in range(1, -10, -1):
print(k, end=' ')
print("\n#Result 5:")
# start=5, stop=0, step=1
for k in range(5, 0):
print(k, end=' ')
Result
#Result 1:
0 1 2 3 4 5 6
#Result 2:
1 2 3 4 5 6
#Result 3:
1 3 5
#Result 4:
1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9
#Result 5:
We can also use the range() function to create a list and then loop through its elements using a for loop. Example:
print(range(10))
print(list(range(10)))
print(list(range(2, 8)))
print(list(range(2, 20, 3)))
numbers = list(range(5, 9))
print("numers list:")
for i in numbers:
print(i, end=' ')
Result
range(0, 10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 3, 4, 5, 6, 7]
[2, 5, 8, 11, 14, 17]
numers list:
5 6 7 8
We can also use the range() function to represent the index of a list to iterate through its elements. For example:
job = ['teacher', 'singer', 'football player']
# iterate over the list using index
for i in range(len(job)):
print("I want to be a", job[i])
Result
I want to be a teacher
I want to be a singer
I want to be a football player
3. For loop with else
In Python, the for loop can use with else. The code block of else executes only after the for loop has finished executing.
Note: If the for loop has a break statement and the break statement exits the for loop, the code block in else will not execute.
Example of using for loop with else
numbers = [0, 1, 5]
for i in numbers:
print(i)
else:
print("That is all in numbers list.")
Result
0
1
5
That is all in numbers list.
Example of using for loop with break and else statements
print("#Result 1")
for i in range(1, 4):
if(i!=9):
print(i)
else:
break
else: # Executed because break in for is not executed
print("No Break")
print("#Result 2")
for i in range(1, 4):
if(i!=3):
print(i)
else:
break
else: # Not executed as there is a break is executed
print("No Break")
Result
#Result 1
1
2
3
No Break
#Result 2
1
2
In the above example, if break in for is executed (escaping from the for loop), else will not be executed. However, if there is a break in for but it is not executed, else will still be executed.