• No results found

To close out this topic, there is one other Python data type you’ll run into that can create multiple data elements. The range data type contains an immutable sequence of numbers that work a lot like a tuple but are a lot easier to create.

You create a new range value by using the range() method, which has the following format:

range(start, stop, step)

The start value determines the number where the range starts, and the stop value determines where the range stops (always one less than the stop value specified). The step value determines the increment value between values. The stop and step values are optional; if you leave them out, Python assumes a value of 0 for start and 1 for step.

The range data type is a bit odd to work with: You can’t reference it directly, such as to print it out. You can reference only the individual data values contained in the range, like this:

>>> range1 = range(5)

>>> print(range1) range(0, 5)

>>> print(range1[2]) 2

>>> for x in range1:

print(x)

0 1 2 3 4

>>>

When you try to print the range1 variable, Python just returns the range object, showing the start and stop values. However, you can print the range1[2] value, which references the third data value in the range.

The range data type comes in most handy in the for statement, as shown in the

preceding example. You easily can iterate through a range of values in the for loop by just specifying the range.

Watch Out!: The range() Change

In Python v2, the range() function created a sequence of numbers as a standard list data type. Python v3 changed that to make the range data type separate from the list data type. Be careful if you run into any v2 code that assumes that range is list!

Summary

In this hour, you took a look at the tuple and list data types in Python. Tuples enable you to reference multiple data values using a single variable. Tuple values are immutable, so once you create a tuple, you can’t change it in your program code. Lists also contain multiple data values, but you can change, add, and delete values in lists. Python supports lots of functions to help you manipulate data using lists. They come in handy when you need to iterate through a data set of values in your scripts. List comprehensions allow you to create new lists based on values in another list, a tuple, or a range of values. You can define complex equations to manipulate the data as Python transfers it using a list comprehension, making it a versatile tool in Python.

In the next hour, we turn our attention to yet another type of data storage in Python, using dictionaries and sets.

Q&A

Q. Can you use lists to perform matrix arithmetic?

A. Not easily. Python doesn’t have any built-in functions that can perform

mathematical operations on list data values directly. You’d have to write your own code to iterate through the individual list values and perform the calculations.

Fortunately, the NumPy module (see Hour 5, “Using Arithmetic in Your Programs”) 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 use only numeric index values.

Workshop Quiz

1. What does Python use to denote a list value?

a. Parentheses

b. Square brackets c. Braces

2. You can change a data value in a tuple but not in a list. True or false?

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

4. What range should you use to extract a slice of index values 1 up to and including 3 in a tuple?

5. How do you specify a step amount in a tuple slice?

6. The max() function works in tuples that consist of string values. True or false?

7. What function creates a list from a tuple value?

a. tuple() b. list() c. braces

8. What index value retrieves the last value in a list?

9. What function do you use to remove a value from the middle of a list?

10. What function do you use to add a new data value to the end of a list?

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.

4. [1:4]. Remember that the range goes to one less than the last value specified, so you must specify the index of 4 to retrieve the value at Index position 3.

5. [i:j:k]. The third position (k) defines the step amount to use.

6. True. The max() function will return the largest string value in the tuple.

7. B. The list() function will convert a tuple value into a list value that you can modify.

8. [-1]. The -1 Index retrieves the last value in the list.

9. pop(). The pop() function can remove a value from anywhere in the list.

10. append(). The insert() function can add a value to a specific location, but the append() function can only add a new value to the end of the list.

Hour 9. Dictionaries and Sets

What You’ll Learn in This Hour:

What a dictionary is

How to populate a dictionary

How to obtain information from a dictionary What a set is

How to program with sets

In this hour, you will read about two additional Python collection types: dictionaries and sets. You will learn what they are, how to create them, how to fill them with data, how to manage them, and how to use them in Python scripts.