Python range() function and list

Mar 11, 2015 • Narayanan Raghupathy

The two multiple choice questions here are related to the Programming with Python lesson Storing Multiple Values in Lists.

Question 1

You want to create a list containing elements 1,2,3,4,5, and store it in a variable named “myList”, Select the one or more python commands that will give you the list

a. myList = range(5)

b. myList = range(1,5)

c. myList = range(1,6)

d. myList = range(1,7)[0:5]

Question 2

Consider the following three Python statements

  myList=range(5)
  myList.reverse()
  print myList[3:4]

What will the last print statement do ?

a. 1

b. 4

c. [2,1]

d. [1]

Explanation for Question 2’s answers

a: did not understand the slice operation returns a list

b: did not understand the list starts with 0 not 1 and slice operation returns a list

c: did not understand the specific slice operation gives list with single element

d: correct answer