The multiple choice questions here are intended for the section on (python loops)[http://swcarpentry.github.io/python-novice-inflammation/02-loop.html]
Question 1
How would you create a loop to iterate over the contents of the list `mylist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] and print out each element ?
- for item in range(mylist): print(item)
- for item in range(len(mylist)): print(item)
- for item in mylist: print(item)
- for item in mylist[0] print(item)
Question 2
You have a list which contains ten elements. Which of the following uses of range() would produce a list of the indexes in the list?
- range(1,10)
- range(10)
- range(0,9)
- range(1,9)
Diagnostics
- Answer ‘a’ forgets that lists are counted from zero.
- Answer ‘b’ is the correct answer
- Answer ‘c’ forgets that range counts up to, but not including the maximum, so that len(mylist) does the right thing
- Answer ‘d’ is a combination of errors ‘a’ and ‘c’