Hardware Reference
In-Depth Information
for count in [1, 2, 3]:
print (“loop number “,count)
print (“hello world”)
You can use commas to separate multiple items for printing.
TIP
You can use
range
instead of typing every number in a list. Type
list(range (10))
In Python 2
range
returns a list. In Python 3 you need to tell it specifically when you want it
to return a list
-
this is not necessary within a
for
statement.
TIP
Python returns the list of numbers from 0 to 9:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
You can also give
range
two arguments - a start and an end - like this:
range (5,15)
You will use a
for
loop with a range to test the
printInsult
function. In the same interac-
tive Python window where you defined
printInsult()
, enter the following code:
for testAge in range (14,17):
print (“age: “ + str(testAge)
printInsult(“Monty”,testAge)
Python prints the following:
age: 14
Monty, you are a young big turnip
age: 15
Monty, you are a young big dog
age: 16
Monty, you are a old big dog
age: 17
Monty, you are a old wet dog