Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours

(singke) #1
which provides a separate matrix object and functions to perform matrix math using those
objects.
Q. Most programming languages support associative arrays, matching a key to a value in an
array. Do lists or tuples support this feature?
A. No. Python uses a separate data type to support associative array features (see Hour 9,
“Dictionaries and Sets”). Tuples and lists can only use numeric index values.

Workshop


Quiz


1. What does Python use to denote a list value?
a. Parentheses
b. Square brackets
c. Braces


  1. You can change a data value in a tuple, but not in a list. True or False.

  2. What list comprehension statement should you use to quickly create a list of multiples of 3 up
    to 30?


Answers


1. b. You’ll need to get in the habit of remembering that Python uses parentheses for tuples, and
square brackets for lists.
2. False. Python allows you to change the data values in a list, but tuple values remain constant,
you can’t change them!
3. [x * 3 for x in range(11)]. The comprehension uses the variable x to represent
the numbers in the range. Each iteration multiplies the number by 3 before saving it in the range.
Free download pdf