Here, I would like to touch on the data structure ** tuple ** instead of the list. It's similar to a list, but I'd like to compare a tuple with a list.
I would like to create tuples immediately. This time as well, the operation will die from the Python Console. Enter the following code in the ** Python Console **. I would like to explain this element with a character string.
>>> T= ('Ibaraki Prefecture', 'Tochigi Prefecture', 'Gunma Prefecture', 'Saitama', 'Tokyo', 'Chiba', 'Kanagawa Prefecture')
>>> T
('Ibaraki Prefecture', 'Tochigi Prefecture', 'Gunma Prefecture', 'Saitama', 'Tokyo', 'Chiba', 'Kanagawa Prefecture')
As you can see by entering the code above, tuples are created using ** () ** instead of ** [] **.
It is possible to retrieve each element in the tuple and find the length of the element with the ** len function ** as shown below. You can see that it is the same as the list so far.
>>> T[2]
'Gunma Prefecture'
>>> T[1:3]
('Tochigi Prefecture', 'Gunma Prefecture')
>>> len(T)
7
You can omit the ** () ** to enclose the tuple.
>>> tp = 10, 20, 30
>>> tp
(10, 20, 30)
If you want to create a tuple consisting of one element, be sure to enter, (comma) at the end. Otherwise, you are entering a single value.
>>> tp=10,
>>> tp
(10,)
Tuples also allow you to assign values to multiple variables at the same time.
>>> x, y, z = (100, 200, 300)
>>> x,y,z
(100, 200, 300)
>>> x
100
When assigning values to multiple variables, an error will occur if the number of elements is not the same.
So what's the difference between a list and a tuple? Enter the following code in the ** Python Console **. Display the contents of the variable ** T ** once and then execute.
>>> T
('Ibaraki Prefecture', 'Tochigi Prefecture', 'Gunma Prefecture', 'Saitama', 'Tokyo', 'Chiba', 'Kanagawa Prefecture')
>>> T[1]='AAA'
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
As you can see from the code above, ** tuples do not allow you to change elements. ** ** You can also see that the method cannot manipulate the value.
>>> T.clear()
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'clear'
To put it a little technically, lists are ** mutable ** data structures because they can change elements, and tuples are ** immutable ** data structures because tuples can't change element values. There is a thing.
So you might wonder if tuples wouldn't be used because they can't change the value of an element. However, the fact that this value cannot be changed is often used in practice.
For example, in the previous example, the prefecture name of the Kanto region of the prefecture was substituted for the tuple ** T **, but these prefecture names are basically unchanged. (It is unlikely that the place name of the prefecture will change.)
If this is stored in a list, it is possible to accidentally change the state name. Tuples are often used to prevent such mistakes.
This time we dealt with a data structure called tuple, but did you understand the difference from the list? It may be difficult to see the intended use of tuples as you learn them, but it would be nice if you could understand that they are used when you do not want to change the value of an element.
Recommended Posts