Exercise 1: Looping (1)
a) What is this code trying to achieve?
total = 0
for number in range(10):
total = total + (number + 1)
print(total)
- summing all numbers from 1 to 10
- summing all numbers from 1 to 9
- summing all numbers from 0 to 10
- summing all numbers from 1 to 11
Solution.
- Correct:
range(10)
goes through the numbers0
to9
, including, but the(number + 1)
changes that to1
to10
- Maybe you thought
range(10)
goes from 1 to 9 - You may have missed the
(number + 1)
- Maybe you thought
range(10)
goes to10
, including
Exercise 2: Looping (2)
a) What can fill in the blank?
genus = "Arabidopsis"
species = ["suecica", "thaliana" , "arenosa", "neglecta", "halleri", "lyrata"]
for ______ in species:
print(genus, ______)
- species
- specie
- name
- x
Exercise 3: Ranges
a) Fill in the blank.
for i in _________:
print(i)
10.0
10.5
11.0
11.5
- range(10, 11.5, .5)
- range(10, 12, .5)
- arange(10, 11.5, .5)
- arange(10, 12, .5)
from pylab import *
)
-
range()
cannot be used with a non-integer set size -
range()
cannot be used with a non-integer set size - the last elemt of the range is not included, so this would only print
10.0
,10.5
,11.0
- correct
Solution.
from pylab import *
for i in arange(10, 12, .5):
print(i)
10.0
10.5
11.0
11.5