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?
---
Enter: Range
range(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]
Looping Over a Range
Let's look at range in action:
Looping Over a Range
Looping 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.
Range to Modify Collections
Why 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"]forguestinguest_list:guest="A new name"
But we can do:
guest_list=["Fred","Cho","Brandi","Yuna","Nanda","Denise"]forguestinrange(len(guest_list)):guest_list[guest]="A new name"
Looping Over a Range
Let's make the list all uppercase:
Knowledge Check: Which of the following lines is correct?
my_list=['mon','tue','wed','thu','fri']
fordayinrange(my_list):# answer Afordayinrange(len(my_list)):# answer Bfordayinrange(my_list.length):# answer C
You Do: Range
Locally, 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]
Quick Review: For Loops and Range
for loops:
# On a list (a collection of strings)guest_list=["Fred","Cho","Brandi","Yuna","Nanda","Denise"]forguestinguest_list:print("Hello, "+guest+"!")# On a string (a collection of characters)my_string="Hello, world!"forcharacterinmy_string:print(character)##### Range #####range(4)# => [0, 1, 2, 3]# Using Range as an Index Counternames=["Flint","John Cho","Billy Bones","Nanda Yuna"]foreach_nameinrange(4):print(names[each_name])# ORforeach_nameinrange(len(names)):print(names[each_name])# Using Range to Change a List:guest_list=["Fred","Cho","Brandi","Yuna","Nanda","Denise"]forguestinrange(len(guest_list)):guest_list[guest]="A new name"
The While Loop
What 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 to False(now the bread is brown, so stop).
While Loop Syntax
# While <something> is true:# Run some code# If you're done, set the <something> to false# Otherwise, repeat.a=0whilea<10:print(a)a+=1
While Loop: Be Careful!
Don't ever do:
a=0whilea<10:print(a)
And don't ever do:
a=0whilea<10:print(a)a+=1
Your program will run forever!
If your program ever doesn't leave a loop, hit control-c.
We Do: Filling a Glass of Water
Create 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?
We Do: Filling a Glass of Water
Add the loop:
glass=0glass_capacity=12whileglass<glass_capacity:glass+=1# Here is where we add more water
That's it!
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?
You Do: A Guessing Game
Now, 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 when guess is equal to answer.
In the while loop, prompt the user for a new guess.
After the while loop, print "You did it!"
Discuss with your partner: Why do we need to make an initial variable before the loop?
You Do: A Guessing Game (Solution)
answer="4"guess=input("Guess what number I'm thinking of (1-10): ")whileguess!=answer:guess=input("Nope, try again: ")print("You got it!")
How'd you do? Questions?
Exiting a Loop
There are times when you may want to exit a loop before the final condition has been met. Perhaps the input from another part of the program has satisfied another, separate condition that makes the rest of the loop unnecessary. Enter the break statement.
whileTrue:ifmy_condition==1:# my condition is met! No need to continue onbreakelsemy_condition==0:my_condition=1# note that this is within the scope of the while loopprint('This doesn\'t get run if the break is triggered!')
Continuing a Loop
There are times when you may want to to continue a loop without running code beneath the continue statement. The continue allows you to do just that! After the continue statement is triggered, the loop continues the next iteration of the loop without executing any code beneath it on that iteration.
number=0fornumberinrange(5):number=number+1ifnumber==3:continueprint('My number is currently 3')print(f'Number is {str(number)}')print('Out of loop')
Prints:
Numberis1Numberis2Numberis4Numberis5Outofloop
Passing within a Loop
The pass statement is like a placebo in a loop: it allows a loop to execute without any interruption. This example may seem odd, and we'll cover the more common use case in the next example.
number=0fornumberinrange(5):number=number+1ifnumber==3:passprint('My number is currently 3')print(f'Number is {str(number)}')print('Out of loop')
The most common use case for pass is to act as a placeholder for a function that has yet to be written. Developers will often do this if they're creating the architecture for a program but haven't gotten to actually building the logic yet.
defmy_empty_function():pass
What happens if we don't put the pass statement in the code and attempt to execute the function definition?
Throwing Exceptions within a Function or Class
Note that the previous example will allow the function to be called, but the function won't do anything. If the programmer wishes to alert the user, they may also use raise to interrupt the program execution. The following is common to see in larger applications that are in the process of being built by a dev team:
defmy_empty_function():raiseNotImplementedError
What happens when we call this function? Hint: look at the type of error that is returned!
Summary + Q&A
Loops:
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.