In this tutorial, we are going to discuss how can we use for loop to iterate over a sequence of elements. So let's discuss Python for loop.
What is for loop?
A for loop is an iterator that is used to iterate over a sequence of elements. The sequence of elements could be anything a string or a list or tuple or range(). for loop syntax: for val in sequence: statements() the statement() will execute continuously until the value val reach the last element of a sequence . As with other statements we use indentation to perform the for loop statements. Let’s understand it with an example:
lis = [1,"hello","world", True, 5.6] for val in lis: print(val) print("now the value of val is:",val)
1 hello world True 5.6 now the value of val is: 5.6
In the above example, we saw that the for loop iterate over all the values present in lis and assign all the values to val one by one and at last the value of val becomes 5.6. but In the above example more than the val value we need to focus on the number of times the print() statement execute which is inside the for statement. The print() statement run the number of list values present in the lis that all the for loop is all about it continues to execute the statement until it reaches the last value of the iterators.
Iterate a string using a for loop
string = "hello world how are you" for i in string: print(i, end="-")
h-e-l-l-o- -w-o-r-l-d- -h-o-w- -a-r-e- -y-o-u-
What is range()?
range () is not a function it is a generator which you will learn in detail in upcoming tutorials. But for now, you can treat range () as an in-built function of python. range() is used to create a sequence of numbers that can be iterated through the for a loop. If you write range(20 ) then it will generate a number from 0 to 19 which a for loop can use to iterate. range() function can also accept three parameters if you do not want the range function to start a sequence from 0.
Syntax of a range() with one parameter
for number in range(end_point): statement()
range(start_point, end_point) function with 2 parameters:
for number in range(start_point, end_point): statement()
range(start_point, end_point, steps) function with 3 parameters:
for number in range(start_point, end_point, steps): statement()
let’s understand with an example:
print("range() function with one parameter") for number in range(5): print(number) print("range with 2 parameters start from 3 and end with 9") for number in range(3 ,9): print(number) print("range() function with 3 parameters") for number in range(4,10,2): print(number)
range() function with one parameter 0 1 2 3 4 range with 2 parameters start from 3 and end with 9 3 4 5 6 7 8 range() function with 3 parameters 4 6 8
for loop with else statement:
the else statement used with the for loop to tell that all the elements of the sequence have been iterated. The else statement will execute after all the elements of the sequence has iterated and not element left.
Let’s understand it with an example:
lis=[0, 1,2,4,"hello world"] for i in lis: print(i) else: print("the list has been exhausted")
0 1 2 4 hello world the list has been exhausted