0% found this document useful (0 votes)
14 views12 pages

5 Loops

Loops in Python can be "for" loops or "while" loops. For loops iterate over a sequence, like characters in a string. While loops repeat as long as a condition is true. The range function can generate a sequence of numbers to iterate over. Looping constructs like break and continue can alter the flow. The else clause runs code after the loop finishes normally without breaking.

Uploaded by

psiharisthanos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views12 pages

5 Loops

Loops in Python can be "for" loops or "while" loops. For loops iterate over a sequence, like characters in a string. While loops repeat as long as a condition is true. The range function can generate a sequence of numbers to iterate over. Looping constructs like break and continue can alter the flow. The else clause runs code after the loop finishes normally without breaking.

Uploaded by

psiharisthanos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Loops in Python

Sokratis Sofianopoulos

1
Loops

• There are two types of loops in Python, for and while


• For loops iterate over a given sequence, like the characters of a string:
astring = "Hello world!"
for x in astring:
print(x)
• While loops repeat as long as a certain Boolean condition is met:
count = 0
while count < 5:
print(count)
count += 1

2
For loop using the range function
• For loops can iterate over a sequence of numbers using the range function (we will see
functions next week)
• The range function can take 1, 2 or three parameters
• One parameter (n): Prints integer numbers from 0 to n-1
for x in range(5):
print(x)
• Two parameters (x, y) : Prints integer numbers from x to y-1
for x in range(4, 9):
print(x)
• Three parameters (x, y, z) : Prints integer numbers from x to y-1 with stride z
for x in range(3, 8, 2):
print(x)

3
Exercise

• Ask the user to enter a number from 1 to 10


• if the user enters a number outside of the range ask again
• Then, using a for loop and f-string write the multiplication table of the number
• You can ask the user to enter a number with the following code:
number = int(input("Enter a number: "))
• The multiplication table can be printed for example like this:
1x1=1

4
Solution

n = -1
while n not in range(1, 11):
n = int(input("Input a number: "))

for i in range(1,11):
print(f"{n} x {i} = {n*i}")

5
break and continue statements
• break is used to exit a loop (for or while)
• continue is used to skip the current block, and return to the "for" or "while" statement
• Break example:
count = 0
while True:
print(count)
count += 1
if count >= 5:
break
• Continue example:
for x in range(10):
if x % 2 == 0:
continue
print(x)

6
Using the else clause in a loop

• Unlike other programming language, in Python we can use else in a loop


(for and while)
• When the loop condition of a for or while statement fails then the code in
the else block is executed
• If break statement is executed inside for loop then the "else" part is skipped
• Note that "else" part is executed even if there is a continue statement

7
Else example in a simple while loop

count=0
while(count<5):
print(count)
count +=1
else:
print("count value reached %d" %(count))

8
Else not reached because of break statement

for i in range(1, 10):


if(i%5==0):
break
print(i)
else:
print("this is not printed: for loop is terminated because of break")

9
Exercise

• Loop through and print out all even numbers from 1 to 200 with
a stride of 3 but
1. You must use a continue statement
2. As you go, count how many even numbers you find
3. After the loop, use an f-string to print a message with the
total number of even numbers that your script found

10
Solution

count=0
for number in range(1, 201, 3):
if number % 2 == 1:
continue
if count == 30:
break
count+=1
print(number)
print(f"Number of even numbers in range {count}")
11
The pass Statement

• for loops cannot be empty, but if you for some reason have a for loop with
no content, put in the pass statement to avoid getting an error:

for x in range(5):
pass

12

You might also like