Python Programming: Loops
#
OverviewThis lesson introduces you to the concept of creating for
and while
loops. It begins with for
, covering lists and strings, then goes into using range
to modify a list. It then dives into while
loops.
#
Learning ObjectivesAfter this lesson, you will be able to:
- Use a
for
loop to iterate a list. - Use
range()
to dynamically generate loops. - Use a
while
loop to control program flow.
#
Duration60 minutes
#
AgendaDuration | Activity |
---|---|
3 minutes | Welcome |
30 minutes | For Loops |
20 minutes | While Loops |
7 minutes | Summary |
#
Discussion: A Small ListThis situation isn't so bad:
visible_colors = ["red", "orange", "yellow", "green", "blue", "violet"]print(visible_colors[0])print(visible_colors[1])print(visible_colors[2])print(visible_colors[3])print(visible_colors[4])print(visible_colors[5])
But what would we do if there were 1,000 items in the list to print?
Quick Checks:
- But, what if we had a list with 283,000 items in it? We can't write one line for each element. We need a way to write the
print
statement once and have it run for every element in the list. - One of the most powerful things that programming can do for you is automatically perform repetitive tasks.
- In the tiny scripts we've been writing, printing out every variable has not been much of an issue. But with lists in the game now, things can get a bit more challenging. We need a way to automate some tasks so that they repeat for every item in a list.
for
Loop#
The Quick Checks:
A common and extremely useful type of loop is the for
loop. for
loops appear in several computer programming languages and get their name from the fact that they loop (or iterate) for a specific number of times: once for each item in a list.
The for
loop is perfect for when you have a specific collection of items — each of which must be processed once — or for when you know that you must execute a set of instructions a specific number of times. Those are the use cases for which it was designed.
This is our template. When we write a for
loop we do the following:
- We replace
item
with a meaningful name for the items in the collection.- We use this name as the name of the item within our loop.
- We replace
collection
with the name of our list (or other collection). - Indented inside the loop, we write the code to be run for each item.
- Python will start with the first item and automatically stop after it loops with the last item.
The for
loop always follows this form:
for item in collection: # Do something with item
For example:
visible_colors = ["red", "orange", "yellow", "green", "blue", "violet"]
for each_color in visible_colors: print(each_color)
Quick Checks:
- We need what is called a
Loop
. - A common and extremely useful type of loop is the
for
loop.for
loops appear in several computer programming languages and get their name from the fact that they loop (or iterate) for a specific number of times: once for each item in a list.
This code:
- Creates a new list containing some name strings.
- Begins the
for
loop. We loop once for each "name" in the list (names
). - Prints the current name inside the loop."
#
Knowledge Check: What will this code do?Think about what the code will do before you actually run it. Repl.it Note: This replit has:
for name in ["Tom", "Deborah", "Murray", "Axel"]: print("Now appearing in the Refreshment Room...") # in the loop print(name) # in the loopprint("THUNDEROUS APPLAUSE!") # OUTSIDE the loop
Quick Checks:
- The statements inside a loop that you want to repeat must be indented like the statements inside an
if
block. So, if you have three lines of code that you want to execute on each loop iteration, each must be indented one level underneath yourfor
line. - Each name is met with
"THUNDEROUS APPLAUSE!"
because that line, and the two above it, are indented to be in the body of thefor
loop.
#
We Do: Writing a LoopLet's write a loop to print names of guests.
First, we need a list.
Create a local
.py
file namedmy_loop.py
.Make your list: Declare a variable
my_list
and assign it to a list containing the names of at least five people.
The code is:
guest_list = ["Fred", "Cho", "Brandi", "Yuna", "Nanda", "Denise"]
#
We Do: Write a Loop - Making the LoopNow, we'll add the loop.
- Skip a line and write the first line of your
for
loop.- For the variable that holds each item, give it a name that reflects what the item is (e.g.
name
orperson
).
- For the variable that holds each item, give it a name that reflects what the item is (e.g.
- Inside your loop, add the code to print
"Hello,"
plus the name.
"Hello, Felicia!"
"Hello, Srinivas!"
Quick Checks:
- The word collection is a list; this collection is the list variable they have.
The code is:
guest_list = ["Fred", "Cho", "Brandi", "Yuna", "Nanda", "Denise"]
for guest in guest_list: # Print out a greeting in here print("Hello, " + guest + "!")
#
We Do: Write a loop to greet people on your guest listOur guests are definitely VIPs! Let's give them a lavish two-line greeting.
- Inside your loop, add the code to print another sentence of greeting:
"Hello, Srinivas!"
"Welcome to the party!"
The code is:
Quick Checks:
- "Fantastic! Now each guest is greeted by their name and welcomed to the party. Those two
print()
lines are executed on every iteration because both are indented to be in thefor
loop's code block. Think of the indented block as a unit of instructions executed as a group each time the loop runs."
guest_list = ["Fred", "Cho", "Brandi", "Yuna", "Nanda", "Denise"]
for guest in guest_list: # Print out a greeting in here print("Hello, " + guest + "!") print("Welcome to the party!")
#
Discussion: Where else could we use a loop?A loop prints everything in a collection of items.
guest_list = ["Fred", "Cho", "Brandi", "Yuna", "Nanda", "Denise"]
What, besides a list, could we use a loop on?
Hint: There are six on this slide!
- The answer is a string! We can loop through the characters.
#
Looping StringsLoops are collections of strings and numbers.
Strings are collections of characters! Repl.it Note:: This code has:
my_string = "Hello, world!"
for character in my_string: print(character)
- The point is to understand that a string is a collection of characters, and any collection can be looped.
Quick Checks:
- You may not realize it, but a string is a collection of characters. Just so you can see that a
for
loop has the same syntax for any collection, let's add the following code below what we've just written
#
What about...Looping For a Specific Number of Iterations?We have:
guest_list = ["Fred", "Cho", "Brandi", "Yuna", "Nanda", "Denise"]
for guest in guest_list: print("Hello, " + guest + "!")
The loop runs for every item in the list - the length of the collection. Here, it runs 6 times.
What if we don't know how long guest_list
will be?
Or only want to loop some of it?
Quick Checks:
Whenever we have a collection, such as a list or string, and we want to iterate over each item it contains, we should use a
for
loop. Python has internal logic for determining exactly how many times the loop should run based on the length of the collection.But, what if we want to do something in a loop a specific number of times, but we don't have a collection to start with? Maybe we are initializing a new collection and we need to add a specific number of items to it or maybe we just want something to run exactly 15 times. In this case, we can have the
for
loop iterate over a range of numbers.
#
Enter: Rangerange(x)
:
- Automatically generated.
- A list that contains only integers.
- Starts at zero.
- Stops before the number you input.
range(5) # => [0, 1, 2, 3, 4]
Quick Checks:
- You can actually feed more parameters into
range()
to control what number it starts at and how big each step is to the next number, but we will keep it simple for now. For now it is enough to know that if you loop overrange(5)
then your loop will execute five times. Let's use this in a loop...
#
Looping Over a RangeLet's look at range
in action:
Repl.it Note: This code is:
for i in range(10): print(i)
squares = []
for num in range(5): sqr = num ** 2 squares.append(sqr)
print(squares)
Quick Checks:
- "We can see that this code prints each of the numbers in our range of 0 through 9 (10 numbers total.) We don't need to have our loop print anything. This loop could be used to execute any sequence of code 10 times. "
#
Looping Over a Range IILooping over names
here is really just going through the loop 4 times - at index 0
, 1
, 2
, and 3
.
We can instead use range(x)
to track the index and loop names
: range(4)
is [0, 1, 2, 3]
.
We can then use len(names)
, which is 4, as our range.
Repl.it Note: This code is:
names = ["Flint", "John Cho", "Billy Bones", "Nanda Yuna"]
for each_name in range(len(names)): print(names[each_name])
- Vary the size of range - try
range(4)
andrange(2)
- so that you can see the differences. - Give a bunch of examples. Range might make sense conceptually, but be hard to remember the syntax for and use.
Quick Checks:
- Recall that you can use the
len()
function to get the length of a list. Since that will always be an integer, we can feed that value into therange()
function to generate a range that contains each index in the list. - Don't be alarmed about the function inside the function. That's fairly common. Let's break it down:
len(names)
will return the length of thenames
list; in this case, 4. The number 4 is then used as the parameter forrange()
creating a range containing 0, 1, 2, and 3. These happen to all be valid indices for our list so we can use them to modify the values stored at those indices.
#
Range to Modify CollectionsWhy would you use range
on a list, when you could just loop the list?
We can't do:
guest_list = ["Fred", "Cho", "Brandi", "Yuna", "Nanda", "Denise"]
for guest in guest_list: guest = "A new name"
But we can do:
guest_list = ["Fred", "Cho", "Brandi", "Yuna", "Nanda", "Denise"]
for guest in range(len(guest_list)): guest_list[guest] = "A new name"
Quick Checks:
- "But there is one special use for this that is vital to know about. When we loop using
for item in collection
we can't ever really modify the elements in the list. Whenever we accessitem
we are actually getting a copy of the item in the list. In order to modify the item in the list, we would need the index of that item in the list. And guess whatrange()
gives us..."
#
Looping Over a Range IIILet's make the list all uppercase:
Repl.it Note: This code is:
# This won't work
guest_list = ["Fred", "Cho", "Brandi", "Yuna", "Nanda", "Denise"]
for guest in guest_list: guest = guest.upper()
print("Without range, guest_list is", guest_list)
# This will!
for guest in range(len(guest_list)): guest_list[guest] = guest_list[guest].upper()
print("With range, guest_list is", guest_list)
- This is a bit difficult. Make sure that you understand that range hits the index versus a regular
for
loop makes a copy.
Quick Checks:
- Now when we run this we see that the
names
list has had all the string changed to uppercase. It is necessary to use list indices if you want to modify list elements. If we tried doing the same thing with anitem
from thatlist
then the changes would only apply to the temporary copy stored initem
and would never actually make it into the list.
#
Knowledge Check: Which of the following lines is correct?my_list = ['mon', 'tue', 'wed', 'thu', 'fri']
for day in range(my_list): # answer Afor day in range(len(my_list)): # answer Bfor day in range(my_list.length): # answer C
#
You Do: RangeLocally, create a new file called range_practice.py
.
In it:
- Create a list of colors.
- Using a
for
loop, print out the list. - Using
range
, set each item in the list to be the number of characters in the list. - Print the list.
For example:
["red", "green", "blue"]# =>[3, 5, 4]
5-10 minutes
#
Quick Review: For Loops and Rangefor
loops:
# On a list (a collection of strings)guest_list = ["Fred", "Cho", "Brandi", "Yuna", "Nanda", "Denise"]for guest in guest_list: print("Hello, " + guest + "!")
# On a string (a collection of characters)my_string = "Hello, world!"for character in my_string: print(character)
##### Range #####
range(4) # => [0, 1, 2, 3]
# Using Range as an Index Counternames = ["Flint", "John Cho", "Billy Bones", "Nanda Yuna"]for each_name in range(4): print(names[each_name])
# OR
for each_name in range(len(names)): print(names[each_name])
# Using Range to Change a List:
guest_list = ["Fred", "Cho", "Brandi", "Yuna", "Nanda", "Denise"]for guest in range(len(guest_list)): guest_list[guest] = "A new name"
- Any questions? That was a lot!
#
The While LoopWhat about "While the bread isn't brown, keep cooking"?
Python provides two loop types.
for
:
- You just learned!
- Loops over collections a finite number of times.
while
:
- You're about to learn!
- When your loop could run an indeterminate number of times.
- Checks if something is
True
(the bread isn't brown yet) and runs until it's set toFalse
(now the bread is brown, so stop).
Quick Checks:
- “Now that we’ve learned how to write for loops to to iterate lists, and how to use range to dynamically generate loops, we’re going to transition to while loops. I’m going to show you how to write while loops, and then you’re going to practice while loops with your partner.”
#
While Loop Syntax# While <something> is true:# Run some code# If you're done, set the <something> to false# Otherwise, repeat.
a = 0while a < 10: print(a) a += 1
Quick Checks:
- We won't always have the luxury of a collection of discrete data items for controlling our loop. Frequently, we will need to write a loop that will run an unknown number of times. This is what the
while
loop is for. It's another loop construct that will continue to iterate while a given condition is true. These loops are quite useful for data sets of unknown sizes, or for when we need to loop until some value changes. - Outside of our
while
loop, we create the variablea
, which we'll use as our conditional. We then start our loop. We say to loop "whilea
is less than 10." Then, in the loop, we printa
and add one to its value. Once the value ofa
reaches 10, the loop condition evaluates to false and the loop finishes.
#
While Loop: Be CarefulDon't ever do:
a = 0while a < 10: print(a)
And don't ever do:
a = 0while a < 10: print(a)a += 1
Your program will run forever!
If your program ever doesn't leave a loop, hit control-c
.
Quick Checks:
While
loops present a potential "gotcha" in programming: the infinite loop. Because thewhile
loop only terminates when a condition turns to false, it's possible to write the loop in such a way that it never terminates. This creates a serious bug in your code where the loop never, ever returns control to the app, and it will freeze indefinitely. The way to avoid this is to always remember to update your conditional variable inside your loop block.
#
We Do: Filling a Glass of WaterCreate a new local file, practicing_while.py
.
In it, we'll create:
- A variable for our current glass content.
- Another variable for the total capacity of the glass.
Let's start with this:
glass = 0glass_capacity = 12
Can you start the while
loop?
Quick Checks:
Can you think of the mental process you follow when pouring water into a glass? You start with an empty glass and begin adding water to it, right? While you are adding the water, you must constantly check to see if the glass has reached its maximum capacity. If it has, you then stop pouring. Otherwise, you continue adding water. Let's see how that would work as a
while
loop...We a variable for our current glass content and we need one for the total capacity of the glass.
What we want to do is add water to the glass one unit at a time until the glass reaches capacity. Said another way, while the glass contains less than its capacity, add another unit of water. Can you start the loop code before we move to the next slide?
#
We Do: Filling a Glass of Water IIAdd the loop:
glass = 0glass_capacity = 12
while glass < glass_capacity: glass += 1 # Here is where we add more water
That's it!
Quick Checks:
- We declare our
glass
variable and set it to0
water, currently. Then, we declare our glass' capacity and set it to12
units. Next, we set up ourwhile
loop. We want to loop while the glass has a value less thanglass_capacity
. - Inside of our loop, we add
1
unit of water to our glass. Each time the loop runs, it checks the value ofglass
to see if it has reached the same value asglass_capacity
. - The loop stops once
glass
reaches12
, conveniently before we spill.
#
Side Note: Input()Let's do something more fun.
With a partner, you will write a program that:
- Has a user guess a number.
- Runs until the user guesses.
But first, how do we have users input numbers?
Using input()
.
user_name = input("Please enter your name:")# user_name now has what the user typedprint(user_name)
Erase the code in your practicing_while.py
file and put the above. Run it! What happens? Does it work?
Quick Checks:
While
loops are also great for guessing games. Let's use a small function calledinput()
to get some user input so that they can take guesses about our secret number. Here's howinput()
works.Whatever string you put in the parentheses is printed to the screen. The user then has the opportunity to type something. The script will wait patiently for the user until something is typed and entered (a perfect example of a while loop in the Python internals.) When the user hits Enter, the string they typed is stored in the variable
user_name
above.
#
You Do: A Guessing GameNow, get with a partner! Let's write the the game.
Decide who will be driver and who will be navigator. Add this to your existing file.
- Set a variable,
answer
to"5"
(yes, a string!). - Prompt the user for a guess and save it in a new variable,
guess
. - Create a
while
loop, ending whenguess
is equal toanswer
. - In the
while
loop, prompt the user for a newguess
. - After the
while
loop, print "You did it!"
Discuss with your partner: Why do we need to make an initial variable before the loop?
5 MINUTES
Quick Checks:
Now that you know how to accept input from the keyboard, make a game where the player must correctly guess a number. The game will continually prompt the user to enter a guess if they did not guess correctly. Once they correctly guess the number, the program congratulates them and exits. Can you think of how this would be written?
You need a variable to hold the correct answer. You need another variable to hold each subsequent guess. We will need to compare the guess to the answer and if it is wrong, ask the user again for a guess. This sounds like we will be repeatedly asking for a guess while the previous guess was incorrect.
One thing to keep in mind is that
input()
returns a string so if the user types5
it will result in the string"5"
. You cannot compare numbers to strings in Python. To work around this for a number guessing game, set your correct answer variable to be the string of the number (e.g. "4" or "9") instead of the number itself. This way when you do your loop comparison, you'll be comparing the same types.
#
You Do: A Guessing Game (Solution)answer = "4"guess = input("Guess what number I'm thinking of (1-10): ")while guess != answer: guess = input("Nope, try again: ")print("You got it!")
How'd you do? Questions?
#
Summary + Q&ALoops:
- Common, powerful control structures that let us efficiently deal with repetitive tasks.
for
loops:
- Used to iterate a set number of times over a collection (e.g. list, string, or using
range
). range
use indices, not duplicates, so it lets you modify the collection.
while
loops:
- Run until a condition is false.
- Used when you don't know how many times you need to iterate.
That was a tough lesson! Any questions?