Loop control statements in Python

Understanding how to implement loops in Python

Many objects in Python are ‘iterable’. That means that one can iterate over every element in the object. Such as every element in a list or every character in a string. ‘for’ loop is used for iteration.

‘for’ loop

Here we create a list over which we have to iterate. Then we use the keyword ‘for’ and mention ‘i’ which is the variable name and acts like a placeholder. It takes the value of every element in the object that has to be iterated.

>>> for i in list1:
print(i)
a
b
c

Notice how the variable ‘i’ is changed to ‘pizza’ and it still gives the output. That is so because the variable name can be set as anything.

>>> for pizza in list2:
print(pizza)
11
22
33

Now we will try printing only the even numbers by implementing both the loop and conditional statements.

Odd number: 11
22
Odd number: 33

Let’s try to get the sum of all numbers in the list using for loop.

>>> for i in list2:
sum = sum + i
print(sum)
66

‘for’ loop usage in case of strings

>>> for i in str1:
print(i)
P
y
t
h
o
n

i
s

f
u
n

‘for’ loop usage in case of tuples

>>> for _ in tup1:
print(_)
1
2
3

‘for’ loop usage in case of tuples unpacking. That is list with tuples in it.

# Gives the tuples inside the list
>>> for i in list3:
print(i)
(11, 22)
(33, 44)
(55, 66)
# Gives the individual items in the tuples inside the list
>>> for (a,b) in list3:
print(a)
print(b)
11
22
33
44
55
66

‘for’ loop usage in case of dictionary

>>> for i in dict1:
print(i)
a
b
c

Note that when you try to iterate over a dictionary, you iterate over the keys. To iterate over items, do the following.

('a', 1)
('b', 2)
('c', 3)
>>> for key,value in dict1.items():
print(value)
1
2
3
>>> for i in dict1.values():
print(i)
1
2
3

‘while’ loop

While loops will continue to execute a block of code while some condition remains true. For example, while the motor is on, the car should be running.

x is: 5
x is: 6
x is: 7
x is: 8
x is: 9

Incase we mention a condition which is never met then the loop will run forever. It will go into an infinite loop.

Now coming to some important keywords to use with loops. We can used break, continue and pass statements in the loops to add additional functionality.

‘break’ keyword

Breaks out of the current closest enclosing loop

>>> for i in str2:
if i == 'a':
break
print(i)
P
y
t
h
o
n

l

Here as soon as the letter ‘a’ is encountered, the current enclosing loop is exited and therefore no printing is done after ‘l’.

‘continue’ keyword

Goes to the top of the closest enclosing loop

>>> for i in str2:
if i == 'a':
continue
print(i)
P
y
t
h
o
n

l
n
g
u
g
e

Here we wanted to skip the letter ‘a’ in the string. So we used ‘continue’ keyword. Everytime ‘a’ occured it went back to its closed enclosing loop which is the for loop.

‘pass’ keyword

Does nothing at all

>>> for i in list4:
#statement
File "<ipython-input-25-6fe39b558d44>", line 4
#statement
^
SyntaxError: unexpected EOF while parsing

The error has occured because python expects you to mention a statement as indentation is a crucial part of it. So instead of leaving it blank, using the keyword ‘pass’ can help get rid of this error.

Refer to the notebook here.

Beginner-level books to refer to learn Python:

Advance-level books to refer to learn Python:

Reach out to me: LinkedIn

Check out my other work: GitHub

Self-driven woman who wishes to deliver creative and engaging ideas and solutions in the field of technology.