[Python] Chapter 04-05 Various data structures (tuple creation and features)

[Python] Chapter 04-05 Creating Tuples

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.

Creating tuples

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.

Features of tuples

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.

Finally

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.

Return to [Table of Contents Link]

Recommended Posts

[Python] Chapter 04-05 Various data structures (tuple creation and features)
[Python] Chapter 04-01 Various data structures (list creation and element retrieval)
[Python] [Supplement] Chapter 04-08 Various data structures (creating and manipulating sets)
[Python] Chapter 04-04 Various data structures (see list)
[Python] Chapter 04-02 Various data structures (list manipulation)
[Python] Chapter 04-07 Various data structures (dictionary manipulation)
[Python] [Supplement] Chapter 04-09 Various data structures (set theory and operations in sets)
Python for Data Analysis Chapter 4
Python for Data Analysis Chapter 2
python> tuple> data, address = s.recvfrom (10000)
Python for Data Analysis Chapter 3
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part1-
Memo "Chapter 5 – Dictionaries and Structuring Data"
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part2-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part4-
Python data structures learned with chemoinformatics
Hashing data in R and Python
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part3-
Data pipeline construction with Python and Luigi
Python Application: Data Visualization Part 3: Various Graphs
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.1-8.2.5)
Ant book in python: Sec. 2-4, data structures
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.3-8.3.6.1)
Python data structure and internal implementation ~ List ~
[Introduction to Python3 Day 19] Chapter 8 Data Destinations (8.4-8.5)
[Introduction to Python3 Day 18] Chapter 8 Data Destinations (8.3.6.2 to 8.3.6.3)
Python data structure and operation (Python learning memo ③)
[Python] Various data processing using Numpy arrays
Easily graph data in shell and Python
[Python Iroha] Difference between List and Tuple
Compress python data and write to sqlite
[Python] Various combinations of strings and values
Exchange encrypted data between Python and C #
[Introduction to Python3 Day 12] Chapter 6 Objects and Classes (6.3-6.15)
Python variables and data types learned in chemoinformatics
Receive and display HTML form data in Python
[Python] Swapping rows and columns in Numpy data
[Python] How to read data from CIFAR-10 and CIFAR-100
[Python] Conversion memo between time data and numerical data
[Introduction to Python3 Day 22] Chapter 11 Concurrency and Networking (11.1 to 11.3)
[Introduction to Python3 Day 11] Chapter 6 Objects and Classes (6.1-6.2)
[Python] Chapter 02-01 Basics of Python programs (operations and variables)
[Python for Hikari] Chapter 09-02 Classes (Creating and instantiating classes)
[Python] How to use hash function and tuple.
[Django3] Environment construction and various settings summary [Python3]
Python Application: Data Handling Part 2: Parsing Various Data Formats
Try importing MLB data on Mac and Python
cv2 functions and data types (OpenCV python bindings)