Loops
While loop videos
While Loop | Python | Tutorial 20
Counter variable
Loop condition
Beginner Python Tutorial 69 - do while Loop in Python
Post-test loop (Do while)
break
Beginner Python Tutorial 70 - Indefinite Loop
Post-test loop with user input
How to Use “break” and “continue” in Python “while” Loops
break
continue
For loop videos
Loop with a counter variable
Three steps to looping with a counter variable:
what is the first value I want to output?
Set ‘i’ to that value.
what is the final value I want to output?
Include that in the while condition.
what do I want it to go up by?
Increase ‘i’ by that amount.
i = 0
while i < 5:
print("hello")
i += 1
Practice Question Generator
Try the Loop Practice Question Generator. Press the “play” button to run the program.
Loop with an accumulator variable
total = 0 # accumulator
i = 1 # counter
while i <= 10:
total += i # add to the accumulator
i += 1 # increase the counter
break
i = 0
while i <= 10:
print(i)
if i == 5:
break
i += 1
continue
i = 0
while i <= 10:
if i == 5 or i == 7:
i += 1
continue
print(i)
i += 1
Get user input in a loop
This is broken down in three patterns:
Create a loop that iterates five times.
Take user input in the loop.
Use an accumulator variable to store the input.
i = 0 # counter
total = 0 # accumulator
while i < 5: # set to loop 5 times
num = int(input("Enter number: "))
total += num
i += 1
print(total)
Quit loop with sentinel value
The sentinel value in the code below is -1
.
The loop will continue forever until the user
enters that vaue. We break
when the sentinel value
is encountered.
total = 0
while True:
num = int(input("Enter number, -1 to stop: "))
if num == -1:
break
total += num
print(total)
Loop through a string (while)
name = "Mr. Gallo"
i = 0
while i < len(name):
print(i, name[i])
i += 1
0 M
1 r
2 .
3
4 G
5 a
6 l
7 l
8 o
Loop through a list (while)
friends = ["Frank", "Sally", "Jimbo"]
i = 0
while i < len(friends):
print(i, friends[i])
i += 1
0 Frank
1 Sally
2 Jimbo
Loop through a string (for)
For loops can be a short cut, but, can sometimes be limiting because as they are looping, they don’t have access to the index values of the elements like we see in Loop through a string (while) and Loop through a list (while). For a work-around, see Loop using enumerate.
name = "Mr. Gallo"
for character in name:
print(character)
M
r
.
G
a
l
l
o
Loop through a list (for)
friend_list = ["Frank", "Sally", "Jimbo"]
# For every friend in my friend list
# print the friend
for friend in friend_list:
print(friend)
Frank
Sally
Jimbo
Loop using range
# while loop print 50-100
i = 50
while i <= 100:
print(i)
i += 1
# range(start, end)... Goes up to, but, doesn't include the end
for num in range(50, 101):
print(num)
# while loop print 1-25 by 5
i = 0
while i <= 25:
print(i)
i += 5
# range(start, end, step)
for num in range(0, 26, 5):
print(num)
Loop using enumerate
To have access to the index number while looping through a
collection, we can use the enumerate
function.
This allows for the simplicity of a for loop, while
still having access to the index value.
name = "Mr. Gallo"
for i, character in enumerate(name):
print(i, character)
0 M
1 r
2 .
3
4 G
5 a
6 l
7 l
8 o
String building and filtering
To build a string with a loop:
Create an empty string (before the loop).
Loop over whatever you need to loop over.
Add to the string inside the loop.
1my_string = ""
2for n in range(65, 70):
3 my_string += str(n) + " "
4
5print(my_string) # "65 66 67 68 69 "
The same concept applies to building and filtering lists.
1nums = [1, 6, -4, 1, -5, 1]
2
3only_ones = []
4for n in nums:
5 if n == 1:
6 only_ones.append(n)
7
8print(only_ones) # [1, 1, 1]