for loop in Python
In our previous lecture, we learned about while loops in Python, now let us check ‘for loops in Python’. First thing to make crystal-clear is that aim of ‘for’ loop is same as that of ‘while’ loop. However, for loop is used to iterate over a particular sequence or order. for loop can iterate over string, list, tuple, range and many more.
Syntax
for variable in sequence:
Statements
Here, the first element of the sequence is assigned to the specified variable and then the next set of statements are executed. Similarly, as a part of next iteration, second element is assigned to the specified variable and statements are executed. This process keeps on repeating until each element of the sequence gets covered.
for variable in range(0, outer_limit):
Statements
#Fun_Facts about Python ‘for’ loop
- Python for loop is always used with the ‘in’/ ‘not in’ operator. ‘in’ operator in Python checks whether a specified value is an element of a sequence like string, array, list, or tuple etc. ‘not in’ operator in Python checks whether a specified value is not an element of a sequence like string, array, list, or tuple etc
- Python allows the concept of nested-for loops.
- We can use for loop inside while loop and vice versa.
- We do have concept of for-else loop in Python.
- for loop supports range() function.
- We can make use of continue statements to skip the current iteration of loop, pass statements to simply pass the iteration, or even break statements to pull the control out of the loop for a given condition, all inside for loop. We will see them in upcoming lessons.
- We do have something called for-else loop in Python. See how it works here
Let’s understand the working of for loop with the help of an example :
Program : To traverse a given list of items.
# for loop illustration
planets = ['mercury', 'venus', 'earth', 'mars']
for each_planet in planets:
print(each_planet, end=' ')
O/P :
mercury venus earth mars
Here, variable ‘each_planet’ is acting as a list element.
We can also iterate over a given sequence using range() function within the for loop.
range() in Python
range() function in Python is used to generate a sequence or series of numbers, starting from 0, ending just before the given limit and increments by 1. These are the default values which can be changed as per the requirements.
Syntax
range(start, end+1, step_size)
start : This signifies the starting point of the sequence.
end : This signifies the ending point of the sequence.
step_size : This signifies the value with which each digit of sequence will either increase or decrease.
Like, in series 2, 4, 6, 8, we have two as a step size.
Do you know ?
Step size in range() function can be negative as well.
Below are the different ways in which range() function can be used :
1. range(n) : We will use this when we want to generate a sequence of numbers from 0 to n-1, keeping 1 as step size.
2. range(0, 10) : We will use this when we want to generate a sequence of numbers from 0 to 9, keeping 1 as step size.
3. range(0, 10, 2) : We will use this when we want to generate a sequence of numbers from 0 to 9, keeping 2 as step size. This series will be as ‘0, 2, 4, 6, 8’.
4. range(10, 0, -1) : We will use this when we want to generate a sequence of numbers from 10 to 1, keeping -1 as step size. Here, starting number is 10, and ending number is one before 0, i.e 1.
Let’s understand the working of range() function with the help of above example :
Program : To traverse a given list of items.
# for loop illustration with range()
planets = ['mercury', 'venus', 'earth', 'mars']
for planet_iterator in range(0, len(planets)):
print(planets[planet_iterator], end=' ')
O/P :
mercury venus earth mars
We all know that just like arrays, Python Lists are indexable too. In the above program, we were able to access each element of the given list with its index using range() function.
Consider line-3 of code
for planet_iterator in range(0, len(planets)):
This for loop will run ‘len(planets)’ times, starting from 0 and ending to len(planets) – 1.
Variable ‘planet_iterator‘ holds integer value.
Do you know ?
The Python len() method is a built-in function used to calculate the length of any iterable object like string, list, tuple, set etc.
Here, in our program, len(planets) = 4
Hence, this for loop will run four times, starting from 0 and ending to 3.
planet_iterator | 0 | 1 | 2 | 3 |
List[planet_iterator] | mercury | venus | earth | mars |
Hence, we now know the significance of range() function.
That’s all, see you in the next lecture.