The document discusses loops in Python, including while and for loops. It provides examples of using while loops to find the largest number input by a user and count even and odd numbers. It also demonstrates using for loops with the range() function, and using break and continue statements to change loop flow. The else block that can be used with while and for loops is also covered.
2. Module 2 Loops in Python
Looping your code with while
while conditional_expression:
instruction_one
instruction_two
instruction_three
:
:
instruction_n
# Store the current largest number here.
largest_number = -999999999
# Input the first value.
number = int(input("Enter a number or type -1 to stop: "))
# If the number is not equal to -1, continue.
while number != -1:
# Is number larger than largest_number?
if number > largest_number:
# Yes, update largest_number.
largest_number = number
# Input the next number.
number = int(input("Enter a number or type -1 to stop: "))
# Print the largest number.
print("The largest number is:", largest_number)
3. Module 2 Loops in Python
Examples
# A program that reads a sequence of numbers and counts how many numbers are even and how many are odd.
# The program terminates when zero is entered.
odd_numbers = 0
even_numbers = 0
# Read the first number.
number = int(input("Enter a number or type 0 to stop: "))
# 0 terminates execution.
while number != 0:
# Check if the number is odd.
if number % 2 == 1:
# Increase the odd_numbers counter.
odd_numbers += 1
else:
# Increase the even_numbers counter.
even_numbers += 1
# Read the next number.
number = int(input("Enter a number or type 0 to stop: "))
# Print results.
print("Odd numbers count:", odd_numbers)
print("Even numbers count:", even_numbers)
4. Module 2 Loops in Python
Counter variable to exit a loop
counter = 5
while counter != 0:
print("Inside the loop.", counter)
counter -= 1
print("Outside the loop.", counter)
Inside the loop. 5
Inside the loop. 4
Inside the loop. 3
Inside the loop. 2
Inside the loop. 1
Outside the loop. 0
counter = 5
while counter:
print("Inside the loop.", counter)
counter -= 1
print("Outside the loop.", counter)
Inside the loop. 5
Inside the loop. 4
Inside the loop. 3
Inside the loop. 2
Inside the loop. 1
Outside the loop. 0
5. Module 2 Loops in Python
Looping your code with for
i = 0
while i < 100:
# do_something()
i += 1
for i in range(10):
print("The value of i is currently", i)
for i in range(100):
# do_something()
pass
The value of i is currently 0
The value of i is currently 1
The value of i is currently 2
The value of i is currently 3
The value of i is currently 4
The value of i is currently 5
The value of i is currently 6
The value of i is currently 7
The value of i is currently 8
The value of i is currently 9
6. Module 2 Loops in Python
for loop & range() with 3 arguments
for i in range(2, 8, 3):
print("The value of i is currently", i)
The value of i is currently 2
The value of i is currently 5
for i in range(1, 1):
print("The value of i is currently", i)
for i in range(2, 1):
print("The value of i is currently", i)
no output here
no output here
The range()'s second argument must be greater than the first.
7. Module 2 Loops in Python
break & continue statements 1
print("The break instruction:")
for i in range(1, 6):
if i == 3:
break
print("Inside the loop.", i)
print("Outside the loop.")
print("nThe continue
instruction:")
for i in range(1, 6):
if i == 3:
continue
print("Inside the loop.", i)
print("Outside the loop.")
The break instruction:
Inside the loop. 1
Inside the loop. 2
Outside the loop.
The continue instruction:
Inside the loop. 1
Inside the loop. 2
Inside the loop. 4
Inside the loop. 5
Outside the loop.
8. Module 2 Loops in Python
break & continue statements 2
largest_number = -99999999
counter = 0
while True:
number = int(input("Enter a number or type -1 to end program: "))
if number == -1:
break
counter += 1
if number > largest_number:
largest_number = number
if counter != 0:
print("The largest number is", largest_number)
else:
print("You haven't entered any number.")
9. Module 2 Loops in Python
break & continue statements 3
largest_number = -99999999
counter = 0
number = int(input("Enter a number or type -1 to end program: "))
while number != -1:
if number == -1:
continue
counter += 1
if number > largest_number:
largest_number = number
number = int(input("Enter a number or type -1 to end program: "))
if counter:
print("The largest number is", largest_number)
else:
print("You haven't entered any number.")
10. Module 2 Loops in Python
while loop & else branch
i = 1
while i < 5:
print(i)
i += 1
else:
print("else:", i)
1
2
3
4
else: 5
i = 5
while i < 5:
print(i)
i += 1
else:
print("else:", i)
else: 5
11. Module 2 Loops in Python
for loop & else branch
i = 111
for i in range(2, 1):
print(i)
else:
print("else:", i)
for i in range(5):
print(i)
else:
print("else:", i)
0
1
2
3
4
else: 4
'else:', 111
12. Module 2 Loops in Python
Key takeaways
● Two types of loops: while & for
● break & continue statements to change the flow of a loop
● while & for loops and else branch
● range() function
13. Module 2 Loops in Python
14. Guess the secret number
15. Counting mississippily
16. Stuck in a loop
17. The Ugly Vowel Eater
18. The Ugly Vowel Eater (1)
19. Essentials of the while loop
20. Collatz's hypothesis
LAB Practice