2️⃣ for Loop
We can iterate on a sequential object (list,string,tuple) using for loop.
The statements inside for loop are executed for each element present in the object.
The obj to be iterated using for loop should be iterable (list,tuple,string,range function,dict,etc)
Iteration, Iterator, Iterable:
Iteration:
Iteration is the process of fetching elements from iterable objects [list, tuple, string, dictionary, set] with the help of loop.
Iterator:
Iterator is the variable which is used to iterate an iterable. It is used for iteration.
Iterable:
Iterable is an object containing elements which is used to iterate elements by iterator.
1 2 3 | # -- syntax of for loop --
for item in obj:
print(item)
|
Lets iterate different objects using for loop:
💻 Example 1:
1 2 3 4 | # -- 1] iterate list using for loop --
a = ['one','two','three','four','five']
for item in a:
print(item)
|
✅ Output:
one
two
three
four
five
💻 Example 2:
1 2 3 4 | # -- 2] iterate tuple using for loop --
a = ('one','two','three','four','five')
for item in a:
print(item)
|
✅ Output:
one
two
three
four
five
💻 Example 3:
1 2 3 4 5 6 | # -- 3] iterate dictionary using for loop --
# Lets print values of dictionary using for loop.
numDict = {'one':100,'two':200,'three':300,'four':400}
for key in numDict:
print(numDict[key])
|
✅ Output:
100
200
300
400
💻 Example 4:
1 2 3 4 | # -- 4] iterate set using for loop --
rollNum = {1,2,3,4,5,6,7,8,9,10}
for item in rollNum:
print(item)
|
✅ Output:
1
2
3
4
5
6
7
8
9
10
💻 Example 5: You have list containing random numbers. Prepare two lists from current list such that one list should contain numbers greater than 5 and another should contain numbers less than 5.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | # current list
mylist = [1,11,3,4,10,6,1,4,4.5,7,8]
# define two blank lists
greater_than_five=[]
less_than_five = []
for i in mylist:
if i>5:
# append first list
greater_than_five.append(i)
else:
# append second list
less_than_five.append(i)
# Display the outputs
print(f'first list: {greater_than_five}')
print(f'second list: {less_than_five}')
|
✅ Output:
first list: [11, 10, 6, 7, 8]
second list: [1, 3, 4, 1, 4, 4.5]
Range function:
Python has built-in range() function which provides an object of sequential numbers.
It always starts from 0 and end at specified number provided as input to it.
Syntax of range function: range(start, stop, step)
start: Number from which the sequence starts.
stop (optional): Number at which the sequence ends.
step (optional): By how much numbers sequence should be incremented at each step.
We can print the numbers sequentially by applying for loop to it, as follows:
💻 Example 1:
1 2 3 4 | # 1] Lets print the numbers from 0 to 4 sequentially using range() function.
for i in range(4):
print(i)
|
✅ Output:
0
1
2
3
💻 Example 2:
1 2 3 4 | # 2] Lets start the sequence with number 5 and end it at 10
for i in range(5, 10):
print(i)
|
✅ Output:
5
6
7
8
9
💻 Example 3:
1 2 | for i in range(5,15,2):
print(i)
|
✅ Output:
5
7
9
11
13
Pattern Printing:
We can also print different patterns using for loop and range function.
💻 Example 1: Square pattern using nested for loop
1 2 3 4 | for i in range(6):
for j in range(6):
print('* ', end=' ')
print('\n')
|
✅ Output:
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
💻 Example 2:**Square pattern using **range function
1 2 | for i in range(1, 6):
print('* '*5)
|
✅ Output:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
💻 Example 3: Right Angled Triangle pattern using range function
1 2 | for i in range(1, 6):
print('* '*i)
|
✅ Output:
*
* *
* * *
* * * *
* * * * *
💻 Example 4: Inverse Right Angled Triangle pattern using range function
1 2 | for i in range(6, 0, -1):
print('* '*i)
|
✅ Output:
* * * * * *
* * * * *
* * * *
* * *
* *
*
Be prepared with below questions prior to your interview !
Frequently asked Interview Questions
Write a basic for loop using Python and explain.
How does a for loop iterate over a sequence?
What is iterable, iterator and iteration?
What is the difference between for loop and while loop?
What is the range() function and how it’s used?
Explain iterating over a dictionary using for loop.