Professional Documents
Culture Documents
Python - Loops
Python - Loops
Python - Loops
Copyright © LEARNXT
Introduction to Python Programming
Loops in Python
Copyright © LEARNXT
Objectives
At the end of this topic, you will be able to understand and explain:
Understand the basics of loops in Python
Copyright © LEARNXT
Loops in Python
Copyright © LEARNXT
Python Loops
A loop statement allows us to execute a statement or group of statements multiple times
Statements are executed sequentially: The first statement in a function is executed first,
followed by the second, and so on
Conditional Code
If condition
is true
condition
If condition
is false
Copyright © LEARNXT
Types of Loops
Types of Loops
while Loop
for Loop
Run as long as a specific
Run a set number of times
logical condition is true
Copyright © LEARNXT
for and while Loops
Copyright © LEARNXT
for Loop Statement
Definition: The for loop executes a sequence of statements multiple times and abbreviates the
code that manages the loop variable
Syntax:
statements(iter)
Copyright © LEARNXT
for Loop - Flowchart
Yes
Is this the last
element of
the
sequence?
No
Statements
End of ‘for’ loop
inside ‘for’ loop
Copyright © LEARNXT
Example I - for Loop Using a Range
for i in range(5):
print("Hello Python")
Hello Python
Hello Python
Hello Python
Hello Python
Hello Python
Copyright © LEARNXT
Example II: for Loop Using a List
list1 = [25, 50, 75]
print(list1)
Copyright © LEARNXT
Example III: for Loop Using a String
for letter in "Gabby":
Copyright © LEARNXT
while Loop Statement
Definition:
A while loop statement in Python repeatedly executes a target statement as long as a given
condition is true
A while loop is a type of loop that runs as long as a logical condition is True
When the logical condition becomes False, the loop stops running
Syntax:
while <logical_condition>:
<code>
Copyright © LEARNXT
while Loop - Flowchart
False
Condition
True
Statements or
Exit the loop
body of while
Copyright © LEARNXT
while Loop - Example
i=1
print(i)
while i < 3:
print(i)
i=i+1
1
1
2
Copyright © LEARNXT
break and continue Statements
Copyright © LEARNXT
Break and Continue Statements
In Python, break and continue statements can alter the flow of a normal loop
Loops iterate over a block of code until the test expression is false, but sometimes we wish to
terminate the current iteration or even the whole loop without checking test expression
Copyright © LEARNXT
break Statement
Definition:
The break statement terminates the loop containing it. Control of the program flows to the
statement immediately after the body of the loop
If the break statement is inside a nested loop (loop inside another loop), the break statement
will terminate the innermost loop
Syntax:
break
Copyright © LEARNXT
break - Flowchart
Enter loop
True
Yes
break?
No
Exit Loop
Remaining body
of loop
Copyright © LEARNXT
break - Example
for val in "string":
if val == "i":
print(val)
print("The end")
s
t
r
The end
Copyright © LEARNXT
continue Statement
Definition:
The continue statement is used to skip the rest of the code inside a loop for the current
iteration only
Loop does not terminate but continues on with the next iteration
Syntax:
continue
Copyright © LEARNXT
continue - Flowchart
Enter loop
test expression False
of loop
True
Yes
Continue?
No
Exit Loop
Remaining body
of loop
Copyright © LEARNXT
continue - Example
for val in "string":
if val == "i": # Program to show the use of continue statement inside loops
continue
print(val)
print("The end")
s
t
r
n
g
The end
Copyright © LEARNXT
Flow Control Within Loops
Copyright © LEARNXT
Flow Controls Within Loops
General structure of a loop:
else:
Copyright © LEARNXT
Using the “loop else”
An else statement after a loop is useful for taking care of a case where an item isn't found in a
list
for i in range(3):
if i == 4:
break
Don't care about 0
else:
Don't care about 1
print ("Don't care about“,i)
Don't care about 2
else: I searched but never found 4!
if name[0] == "M":
else:
Copyright © LEARNXT
Summary
A loop statement allows us to execute a statement or group of statements multiple times
Statements are executed sequentially: The first statement in a function is executed first,
followed by the second, and so on
for loop executes a sequence of statements multiple times and abbreviates the code that
manages the loop variable
while loop repeatedly executes a target statement as long as a given condition is true
In Python, break and continue statements can alter the flow of a normal loop
Copyright © LEARNXT
Additional Resources
McKinney, W. (2013). Python for data analysis. O'Reilly Media.
Beazley, D., & Jones, B. K. (2013). Python cookbook: Recipes for mastering Python 3. O'Reilly
Media.
Copyright © LEARNXT
e-References
Welcome to Python.org. (n.d.). Python.org. https://www.python.org
Copyright © LEARNXT 30
Any Questions?
Thank you
Copyright © LEARNXT
Copyright © LEARNXT