For Loops
Loops, In General
Does anyone remember the two broad categories of loops? What are they, and how are they different? →
- condition-controlled - repeats as long as a condition is true
- count-controlled - repeats a specific number of times
Condition Controlled Loops in Python
What programming construct (repetition structure) do we use in Python to create a condition-controlled loop? →
A while loop is a repetition structure in Python that repeats a block of code as long as a check for a particular condition is true.
Count-Controlled Loops
For Loops
An Example
Its output:
For Loop Syntax
for loops repeat code by iterating over every item in some group / collection of items:
Again, the same example:
Let's Break That Down a Bit
A for loop:
- consists of the keyword for (obvs!)
- followed by a loop variable (this could be named anything you like)
- followed by some iterable object (some sequence of values)
- followed by a colon
- in this case, the iterable object is returned by the range function
- the range function returns an arithmetic sequence of numbers
- in this case, that sequence is [0, 1, 2, 3, 4]
For Loops - Details, Details, Details
What Does It Do?
A for loop:
- repeats a block of code for a specific number of times
- it does this by running through all of the elements in a sequence, one at a time
- the current element can be accessed in the body of the for loop using the loop variable
- (note that the loop variable is called target variable in Starting Out with Python)
- that is… for each element in your sequence, the loop variable is set to that element
For Loops
A more technical explanation is: for loops iterate over every item in an iterable object.
An iterable object:
- is a thing (object) that contains other values / members
- is capable of returning each of its members one at a time
- examples of iterable objects include:
- strings (we know these)
- range objects (we'll learn these today)
- lists (for the future!)
- so… let's see about these range objects
Range and Range Objects
A range object is another data type! It represents an arithmetic sequence, such as 0, 1, 2, 3, 4.
- a range object is created by calling the range() function.
- with one argument, it creates an arithmetic sequence from 0 up to but not including that argument
- consequently, range(5) produces 0, 1, 2, 3, 4
- to see the elements in a range, you can use the list() function
- (we'll learn more about lists later!)
Showing the Results of Range
Let's see if we can make sense of this: →
So… All of That Meant…
- there's a type called range
- the string representation of a range shows you the original arguments that you passed in
- you can call the built-in list function (used to convert value into a list) to show the exact ints in the list
Quick Summary So Far…
We've learned two new built in functions
- range returns a range object, a representation of an arithmetic sequence
- list returns a list object
- for now, we use list in combination with print to show each number in a range object
- we'll learn more about lists later
Range Life…
Range
range() returns a range object, an arithmetic sequence of numbers.
- one argument: range(stop)
- returns a series of numbers starting with 0, up to, but not including stop by ones
- range(3) → 0, 1, 2
- two arguments: range(start, stop)
- returns a series of numbers starting with start , up to, but not including stop by ones
- range(5, 10) → 5, 6, 7, 8, 9
- three arguments: so many arguments, you'll have to go to the next slide →
Range (Continued)
(continued from previous slide)
- three arguments: range(start, stop, step)
- returns a series of numbers starting with start , up to, but not including stop by step
- range(4, 9, 2) → 4, 6, 8
Some Other Things You Should Know About Range
- negative step goes backwards
- range(8, 3, -2) → 8, 6, 4
- printing the result of a range object just gives you the original arguments that you used to call range
- print(range(8, 3, -2)) → range(8, 3, -2)
- use the list function to show the elements in a range
- print(list(range(8, 3, -2)))
Guess That Series of Numbers
Given the following calls to the range function, what is the start, stop, and step? What is the resulting arithmetic sequence? →
Guess That Series of Numbers
What is the start, stop, and step? What is the resulting arithmetic sequence? →
And Now, Back to For Loops
- notice the different loop variable name, whatevs?
- What does this snippet of code output?
Let's Take a Closer Look
Going over this step-by-step, what is the value of whatevs, and what is printed? →
- range(4) gives a sequence of 1, 2, 3
- first iteration, the loop variable, whatevs, is 1
- "1 Mississippi" is printed
- next iteration, the loop variable, whatevs, is 2
- "2 Mississippi" is printed
- next iteration, the loop variable, whatevs is 3
- "3 Mississippi" is printed
Another For Loop Example
And… step-by-step, that's: →
- the sequence is 6, 9, 12
- num: 6, result: 36, 6 squared is 36
- num: 9, result: 81, 9 squared is 81
- num: 12,result: 144, 12 squared is 144
And by using pythontutor.com
How About Another?
What is the output of this code? Let's read through it line-by-line to figure it out. →
Some Quick Excercises
Let's do these together →
- count to 100 starting from 1
- count to 100 by twos from 0
- count backwards starting from 100 down to and including 0
Fizz Buzz
- fizz buzz
- print out 1 to 100 …with the following exceptions:
- for multiples of three, print out "Fizz" instead of the number
- for multiples of five, print out "Buzz" instead of the number
- for multiples of both three and five print “FizzBuzz”
- example output, next slide, plz!
FizzBuzz Output
FizzBuzz Solution
Using an Accumulator Variable
Accumulator Variable
An accumulator variable is a variable used to keep the running total of a repeated calculation or operation that's within a loop:
- a variable is declared outside of the loop
- it is added to within the loop
Some examples include:
- counting the number of times a while loop runs (count += 1)
- incrementally building a string within a loop (s += word)
Summing Numbers
Counting Dice For Loop
Roll a die 1000 times; count how many times a one is rolled! Print out the result. Use a for loop.→
Counting Dice While Loop
Roll a die 1000 times; count how many times a one is rolled! Print out the result. Use a while loop.→
We can base the number of repetitions that a for loop goes through by asking the user to enter a value.
Let's check out a programming problem where this idea might come in handy… →
A Ladder
Make me a ladder! →
- ask for a height, make a ladder with that many rungs
- can you do this using a for loop?
- or how about just string multiplication?
A Ladder Implementation
(Or… two implementations, really)