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
foriinrange(5):print(i)
Its output:
01234
For Loop Syntax
for loops repeat code by iterating over every item in some group / collection of items:
"""
for <loop variable> in <iterable object>:
(<loop variable> can be used here)
"""
Again, the same example:
"""
keyword loop an iterable object,
for variable like range
| | |
| | |
for i in range(5):
print(i)
"""
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: →
# make a range object - an arithmetic sequence from 0 through 5numbers=range(5)# let's look at that objectprint(numbers)# what type is it?print(type(numbers))# can we actually see the sequence? yes, but we have to use list.print(list(numbers))
range(0,5)<class'range'>[0, 1, 2, 3, 4]
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? →
range(3)
# start:0, end:3, step:10,1,2
range(10,16)
# start:10, end:16, step:110,11,12,13,14,15
range(-2,3)
# start:-2, end:3, step:1-2,-1,0,1,2
Guess That Series of Numbers
What is the start, stop, and step? What is the resulting arithmetic sequence? →
height=int(input('How tall do you want this ladder to be?'))foriinrange(height):print('========\n| |')# or... just multiply instead of using the for loop# print(height * '========\n| |')