Loops in Python: Objectives
Loops in Python: Objectives
Loops in Python
Estimated time needed: 20 minutes
Objectives
After completing this lab you will be able to:
work with the loop statements in Python, including for-loop and while-loop.
Loops in Python
Welcome! This notebook will teach you about the loops in the Python Programming Language. By the end of
this lab, you'll know how to use the loop statements in Python, including for loop, and while loop.
https://labs.cognitiveclass.ai/tools/jupyterlab/lab/tree/labs/PY0101EN/PY0101EN-3-4-Classes.ipynb?lti=true 1/11
21:14, 24/02/2022 PY0101EN-3-2-Loops
Table of Contents
Loops (https://#loop)
Range (https://range/?
utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&
SkillsNetwork-Channel-
SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-2021-01-01)
What is for loop? (https://for/?
utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&
SkillsNetwork-Channel-
SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-2021-01-01)
What is while loop? (https://while/?
utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&
SkillsNetwork-Channel-
SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-2021-01-01)
Quiz on Loops (https://#quiz)
Loops
Range
Sometimes, you might want to repeat a given operation many times. Repeated executions like this are
performed by loops. We will look at two types of loops, for loops and while loops.
Before we discuss loops lets discuss the range object. It is helpful to think of the range object as an ordered
list. For now, let's look at the simplest case. If we would like to generate an object that contains elements ordered
from 0 to 2 we simply use the following command:
In [1]:
range(3)
Out[1]:
range(0, 3)
https://labs.cognitiveclass.ai/tools/jupyterlab/lab/tree/labs/PY0101EN/PY0101EN-3-4-Classes.ipynb?lti=true 2/11
21:14, 24/02/2022 PY0101EN-3-2-Loops
NOTE: While in Python 2.x it returned a list as seen in video lessons, in 3.x it returns a range object.
The for loop enables you to execute a code block multiple times. For example, you would use this if you would
like to print out every element in a list.\
Let's try to use a for loop to print all the years presented in the list
dates :
In [2]:
dates = [1982,1980,1973]
N = len(dates)
for i in range(N):
print(dates[i])
1982
1980
1973
The code in the indent is executed N times, each time the value of i is increased by 1 for every execution.
The statement executed is to print out the value in the list at index i as shown here:
https://labs.cognitiveclass.ai/tools/jupyterlab/lab/tree/labs/PY0101EN/PY0101EN-3-4-Classes.ipynb?lti=true 3/11
21:14, 24/02/2022 PY0101EN-3-2-Loops
In [3]:
print(i)
https://labs.cognitiveclass.ai/tools/jupyterlab/lab/tree/labs/PY0101EN/PY0101EN-3-4-Classes.ipynb?lti=true 4/11
21:14, 24/02/2022 PY0101EN-3-2-Loops
In [4]:
print(year)
1982
1980
1973
For each iteration, the value of the variable year behaves like the value of dates\[i] in the first example:
https://labs.cognitiveclass.ai/tools/jupyterlab/lab/tree/labs/PY0101EN/PY0101EN-3-4-Classes.ipynb?lti=true 5/11
21:14, 24/02/2022 PY0101EN-3-2-Loops
In [5]:
squares[i] = 'white'
In [6]:
# Loop through the list and iterate on both index and element value
print(i, square)
0 red
1 yellow
2 green
3 purple
4 blue
As you can see, the for loop is used for a controlled flow of repetition. However, what if we don't know when
we want to stop the loop? What if we want to keep executing a code block until a certain condition is met? The
while loop exists as a tool for repeated execution based on a condition. The code block will keep being
executed until the given logical condition returns a False boolean value.
Let’s say we would like to iterate through list dates and stop at the year 1973, then print out the number of
iterations. This can be done with the following block of code:
https://labs.cognitiveclass.ai/tools/jupyterlab/lab/tree/labs/PY0101EN/PY0101EN-3-4-Classes.ipynb?lti=true 6/11
21:14, 24/02/2022 PY0101EN-3-2-Loops
In [7]:
i = 0
year = dates[0]
while(year != 1973):
print(year)
i = i + 1
year = dates[i]
1982
1980
A while loop iterates merely until the condition in the argument is not met, as shown in the following figure:
Quiz on Loops
https://labs.cognitiveclass.ai/tools/jupyterlab/lab/tree/labs/PY0101EN/PY0101EN-3-4-Classes.ipynb?lti=true 7/11
21:14, 24/02/2022 PY0101EN-3-2-Loops
Write a for loop the prints out all the element between -5 and 5 using the range function.
In [8]:
for i in range(-5,6):
print(i)
-5
-4
-3
-2
-1
Print the elements of the following list: Genres=\[ 'rock', 'R\&B', 'Soundtrack', 'R\&B', 'soul',
'pop']
Make sure you follow Python conventions.
In [9]:
n = len(Genres)
for i in range(n):
print(Genres[i])
rock
R&B
Soundtrack
R&B
soul
pop
Write a for loop that prints out the following list: squares=\['red', 'yellow', 'green', 'purple',
'blue']
https://labs.cognitiveclass.ai/tools/jupyterlab/lab/tree/labs/PY0101EN/PY0101EN-3-4-Classes.ipynb?lti=true 8/11
21:14, 24/02/2022 PY0101EN-3-2-Loops
In [10]:
print(square)
red
yellow
green
purple
blue
Write a while loop to display the values of the Rating of an album playlist stored in the list PlayListRatings . If
the score is less than 6, exit the loop. The list PlayListRatings is given by: PlayListRatings = \[10,
9.5, 10, 8, 7.5, 5, 10, 10]
In [16]:
i = 0
Rating = PlayListRatings[0]
Rating = PlayListRatings[i]
print(Rating)
i = i + 1
10
9.5
10
7.5
https://labs.cognitiveclass.ai/tools/jupyterlab/lab/tree/labs/PY0101EN/PY0101EN-3-4-Classes.ipynb?lti=true 9/11
21:14, 24/02/2022 PY0101EN-3-2-Loops
Write a while loop to copy the strings 'orange' of the list squares to the list new_squares . Stop and exit
the loop if the value on the list is not 'orange' :
In [15]:
new_squares = []
i = 0
new_squares.append(squares[i])
i = i + 1
print(new_squares)
['orange', 'orange']
https://labs.cognitiveclass.ai/tools/jupyterlab/lab/tree/labs/PY0101EN/PY0101EN-3-4-Classes.ipynb?lti=true 10/11
21:14, 24/02/2022 PY0101EN-3-2-Loops
Author
Joseph Santarcangelo (https://www.linkedin.com/in/joseph-s-50398b136/?
utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA
SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-
2021-01-01)
Other contributors
Mavis Zhou (https://www.linkedin.com/in/jiahui-mavis-zhou-a4537814a?
utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA
SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-
2021-01-01)
Change Log
Date (YYYY-MM-DD) Version Changed By Change Description
https://labs.cognitiveclass.ai/tools/jupyterlab/lab/tree/labs/PY0101EN/PY0101EN-3-4-Classes.ipynb?lti=true 11/11