leafleafleafDocy banner shape 01Docy banner shape 02Man illustrationFlower illustration

Python tuple (tuple) detailed

Estimated reading: 5 minutes 82 views
Python tuple detailed

By learning, we know that lists can be modified. However, sometimes we need to create a list of elements that cannot be modified. For example, the elements representing Monday to Sunday should be fixed. At this time, tuples come in handy. Python calls a list of unmodifiable elements a tuple. 

There are two main differences between tuples and lists:

  • First, tuples are immutable, while lists can modify their elements;
  • Second, tuples are represented using parentheses, while lists are represented using square brackets.

Python create tuple

Let’s start by creating a week tuple that records each day from Monday to Sunday.

>>> week=("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
>>> print(week)
('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')

Once the week tuple is created, elements cannot be modified, deleted, or added.

Let’s try to modify the elements in it and see what happens.

>>> print(week[1])
>>> week[1]="Tue"
Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    week[1] ="Tue"
TypeError: 'tuple' object does not support item assignment

We first print the second element in the tuple, which is “Tuesday”. Then, when we try to modify its value, Python returns a type error message, telling us that the tuple type does not support data assignment.

Python modify tuple variable

Although the elements in the tuple cannot be modified, we can reassign the variable that stores the tuple. Let’s take the week tuple as an example, see the following code:

>>> week=("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
>>> print(week)
('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')
>>> week=("Mon","Tue","Wed","Thu","Fri","Sat", "Sun")
>>> print(week)
('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')

We first define the variable week, then assign a tuple to it, and print it to the screen; next, reassign another new tuple to the variable week, and print it to the screen.

This time, Python will not throw an error, because it is legal to reassign a variable. And we see that the value of the variable week has become the new tuple that was last assigned to it.

Python uses tuples

Tuples also have some common methods, let’s take a look.

Get the length of the tuple

Use the len() method to get the length of the tuple. In the following example, the tuple week contains 7 elements, so its length is 7:

>>> week=("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
>>> len(week)

Find the index of a single element in a tuple

To find the index of a single element in a tuple, use the index() method. Let’s still take the week tuple as an example, to get the index of the element “Friday” in it, the code is as follows.

>>> week=("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")

Data type conversion functions list() and tuple()

As we have already covered data type conversion, you can use the srt() function to convert a non-string value to a string representation, for example, use str(8) to convert the number 8 to the string “8”.

Similar to the str() function, the list() function and tuple() function can implement type conversion between lists and tuples. The list() function can convert tuples to lists, and the tuple() function can convert lists to tuples.

For example, we can convert the week tuple to a list of weeks. First, we assign a tuple to the variable week and print it out. At this time, we can clearly see that the elements of the tuple are enclosed in parentheses.

>>> week=("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
>>> print(week)
('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')

Then, we use the list() function to convert the tuple to a list and assign it to the variable week. Printing out the week list, we can clearly see that the elements of the list are enclosed in square brackets. After that, we try to modify the first element of the week list to “Mon”, we can see that the modification is successful, and Python does not report an error, because the list is allowed to modify its elements.

>>> week=list(week)
>>> print(week)
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
>>> week[ 0]="Mon"

You can also use the function tuple() to convert a list to a tuple, this time we convert the week variable, which has become a list, to a tuple.

>>> week=tuple(week)
>>> print(week)
('Mon', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')

When the variable week is converted from a list to a tuple, we cannot modify the value of the elements in it, otherwise an error will be reported.

>>> week[0]="Monday"
Traceback (most recent call last):
  File "<pyshell#46>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

The examples here familiarize us with the use of the list() and tuple() functions, and further understand the difference between lists and tuples.