for Loop in Pythonfor Loops in Other Languagesfor Loopsrange Functionrangerangerange Valuesrange function?for Loops that Don't Use the Loop ValueYou should read Chapter 5, Functions, from our textbook, Starting Out with Python, before next week's class.
I have posted homework 5 here.
It is due this coming Sunday at 11:59 PM.
while Loopswhile loop keeps repeating as long as a certain condition is true
while BOOLEAN_EXPRESSION:
STATEMENT
STATEMENT
...
while loop whenever we do not know how many times the loop should runwhile loop
that will make the boolean expression 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_point
for Loop in Pythonfor loopfor loop in other computer languagesfor loops work in other languagesfor Loops in Other Languagesfor loopfor
followed by three
statements
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
to print the value of 2 raised to the powers of 1 to 10
$ 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
that you can do with the for loop in other languages
for 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
that consists of a sequence of integers
>>> 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
rangefor loop changes the loop variablerange function as well range function with one or two argumentsrange a third argumentrange how much to add to each value to get the next number in the sequencerange
>>> 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 into the range function
for loop more powerfulfor loops does 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 statement
you can have a loop inside another loop
for 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 argument endprint
statement is called