for
Loop in Pythonfor
Loops in Other Languagesfor
Loopsrange
Functionrange
range
range
Valuesrange
function?for
Loops that Don't Use the Loop Valuefor
Loopfor
LoopI have posted homework 5 here.
It is due this coming Sunday at 11:59 PM.
I have posted the answers to Quiz 3 here.
Are there any questions before I begin?
True
while
Loopswhile
loop keeps repeating as long as its
boolean expression
is True
while BOOLEAN_EXPRESSION:
STATEMENT
STATEMENT
...
while
loop whenever we do not know how many times the
loop should run
False
while
loop ...False
...Garbage in Garbage out
while
loop is in data validation
set a flag to False
while the flag is not True
get input from the user and store in a variable
if the input value is good
set the flag to true
else:
print an error message
done = False while not done: value = int(input("Please enter an integer greater than 0: ")) if value > 0: done = True else: print(value, "is less than 0") print(value, "is greater than 0")
distance = speed_of_vehicle * time_traveled_today \ + distance_of_starting_pointis two lines ...
for
Loop in Pythonfor
loopfor
loop in other computer
languages
for
loops workfor
Loops in Other Languagesfor
loopfor
...
True
for ($i = 1; $i <= 10; $i++) { print "$i\n"; }
Entry | Purpose | When Run |
---|---|---|
$i = 1 |
Gives the loop variable its first value | Once, before the code block is run for the first time |
$i <= 10 |
Decides whether the code block is run | Before running the statements in the code block |
i++ |
Changes the value of the loop variable | After the last statement in the code block, but before running the loop test |
for
Loopsfor
loopfor
loop has the following format
for VARIABLE_NAME in LIST_OF_VALUES:
STATEMENT
STATEMENT
...
while
loopfor
keyword is followed by a variablein
keyword followed by a list of valuesfor
loop ...$ cat powers_of_2.py # this program uses a for loop to print the powers of 2 # from 1 to 10 for number in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]: value = 2 ** number print("2 to the power", number, "is", value) $ python3 powers_of_2.py 2 to the power 1 is 2 2 to the power 2 is 4 2 to the power 3 is 8 2 to the power 4 is 16 2 to the power 5 is 32 2 to the power 6 is 64 2 to the power 7 is 128 2 to the power 8 is 256 2 to the power 9 is 512 2 to the power 10 is 1024
for
loop ...for
loop in other languagesfor
loops makes some things easierfloat
$ cat return.py # prints the return on $1000 for different rates of interest principle = 1000 for rate in [.01, .02, .03, .04, .05, .06, .07, .08, .09, .1]: new_value = principle * (1 + rate) print("The return on $1000 at " + format(rate, ".0%") + " is $" + format(new_value, ",.0f")) $ python3 return.py The return on $1000 at 1% is $1,010 The return on $1000 at 2% is $1,020 The return on $1000 at 3% is $1,030 The return on $1000 at 4% is $1,040 The return on $1000 at 5% is $1,050 The return on $1000 at 6% is $1,060 The return on $1000 at 7% is $1,070 The return on $1000 at 8% is $1,080 The return on $1000 at 9% is $1,090 The return on $1000 at 10% is $1,100
$ cat rainbow.py # prints the colors of the rainbow print("The colors of the rainbow are") for color in ["Red", "Orange", "Yellow", "Green","Blue", "Indigo", "Violet"]: print(color) $ python3 rainbow.py The colors of the rainbow are Red Orange Yellow Green Blue Indigo Violet
$ cat types.py # prints the type of different values for value in [1, 1.0, "one", True]: print("The data type of", value, "is", type(value)) $ python3 types.py The data type of 1 is <class 'int'> The data type of 1.0 is <class 'float'> The data type of one is <class 'str'> The data type of True is <class 'bool'>
range
Functionrange
function to make this easyrange
function creates a special kind of list >>> for number in range(10): ... print(number) ... 0 1 2 3 4 5 6 7 8 9
range
function works this wayrange
>>> for number in range(1, 11): ... print(number) ... 1 2 3 4 5 6 7 8 9 10
>>> for number in range(11, 21): ... print(number) ... 11 12 13 14 15 16 17 18 19 20
range
for
loop
changes the loop variable ...
range
function as well range
function with one or two argumentsrange
a third argumentrange
how much to add to each value ...range
>>> for number in range(2, 11, 2): ... print(number) ... 2 4 6 8 10
>>> for number in range(3, 22, 3): ... print(number) ... 3 6 9 12 15 18 21
range
Valuesrange
were increasingrange
negative>>> for number in range(10, 2, -1): ... print(number) ... 10 9 8 7 6 5 4 3
>>> for number in range(10, 0, -1): ... print(number) ... 10 9 8 7 6 5 4 3 2 1
range
function?range
function?
$ cat perl_evens.pl
for ($i=2; $i <= 10; $i+=2) {
print "$i\n";
}
$ perl perl_evens.pl
2
4
6
8
10
range
function
>>> for number in range(2, 11, 2): ... print(number) ... 2 4 6 8 10
range
function in Python provides loop variable valuesfor
loop ...
range
functionfor
loop more powerfulfor
loops do in other languages ...$ cat al_east_teams_1.py # prints the teams in the American league print("Here are the teams in the American League East") for team in ["Boston Red Sox", "Baltimore Orioles", "Toronto Blue Jays", "Tampa Rays", "New York Yankees"]: print(team) $ python3 al_east_teams_1.py Here are the teams in the American League East Boston Red Sox Baltimore Orioles Toronto Blue Jays Tampa Rays New York Yankees
for
Loops that Don't Use the Loop Valuefor
loop to do something a certain number of timesrange
function>>> for number in range(5): ... print("Go Red Sox!") ... Go Red Sox! Go Red Sox! Go Red Sox! Go Red Sox! Go Red Sox!
range
to loop 5 timesif
statement inside another
if
statementfor
loop or a while
loopfor
loops are the most commonfor
loops you must use different names for each
loop variable
$ cat times_table.py # This script creates a times table using nested for loops for row in range(1, 11): for column in range(1, 11): entry = row * column print(entry, end="\t") print() $ python3 times_table.py 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
print
inside the innermost loop may seem oddprint
statement automatically
moved down to the next line
print
argument
end ...
for
Loopfor
loops are often used to perform calculation with numbers
set a variable to hold the total to 0
for each number in the file:
add the number to the total
$ cat for_total.py # totals the numbers in a list total = 0 for value in [56, 89, 43, 65, 32, 14, 57]: total = total + value print(total)
for
loop tells Python to
for
Loopfor
loops can also be used to find the average using the following
formula
average = total/number_of_values
set a variable to hold the total to 0
set a variable to hold the count to 0
for each number in the file:
add 1 to the count
add the number to the total
average = total/count
$ cat for_average_1.py # averages the numbers in a list total = 0 count = 0 for value in [56, 89, 43, 65, 32, 14, 57]: count = count + 1 total = total + value average = total/count print(average) $ python3 for_average_1.py 50.857142857142854
round
functionround
with one argument >>> round(56235.47553653) 56235
round with a 2nd argument, an integer
>>> round(56235.47553653, 2) 56235.48
$ cat for_average_2.py # averages the numbers in a list total = 0 count = 0 for value in [56, 89, 43, 65, 32, 14, 57]: count = count + 1 total = total + value average = round(total/count, 2) print(average) $ python3 for_average_2.py 50.86
while
loopsfor
loopsfor
or while
loop