This is a quick way to find the range of values stored in a tuple!
Concatenating Tuples
While you can’t change the data elements contained within a tuple value, you can concatenate two or
more tuple values to create a new tuple value:
Click here to view code image
>>> tuple10 = 1, 2, 3, 4
>>> tuple11 = 5, 6, 7, 8
>>> tuple12 = tuple10 + tuple11
>>> print(tuple12)
(1, 2, 3, 4, 5, 6, 7, 8)
>>>
This can be somewhat misleading if you’re not familiar with tuples. The plus sign isn’t used as the
addition operator; with tuples, it’s used as the concatenation operator. Notice that concatenating the
two tuple values creates a new tuple value that contains all the data elements from the original two
tuple values.
Introducing Lists
Lists are similar to tuples, storing multiple data values referenced by a single list variable. However,
lists are mutable, and you can change the data values as well as add or delete data values stored in
the list. This adds a lot of versatility for your Python scripts!
The following sections show how to create lists, as well as how to extract the data you store in a list
and work with the data.
Creating a List
Very much like with tuples, there are four different ways to create a list variable:
Create an empty list by using an empty pair of square brackets, as in this example:
>>> list1 = []
>>> print(list1)
[]
>>>
Place square brackets around a comma-separated list of values, as in this example:
>>> list2 = [1, 2, 3, 4]
>>> print(list2)
[1, 2, 3, 4]
>>>
Use the list() function to create a list from another iterable object, as in this example:
>>> tuple11 = 1, 2, 3, 4
>>> list3 = list(tuple11)
>>> print(list3)
[1, 2, 3, 4]
>>>
Use a list comprehension.
The list comprehension method of creating lists is a more complicated process of generating a list
from other data. We’ll discuss how it works toward the end of this hour. Notice that with lists, Python
uses square brackets around the data values, not parentheses as with tuples.
Just as with tuples, lists can contain any type of data, not just numbers, as in this example: