Python Tutorials - Herong's Tutorial Examples - v2.15, by Herong Yang
Data Type - 'tuple' for Immutable List
This section describes the 'tuple' data type, which stores a sequence of references to data objects of any data types. 'tuple' data objects are immutable.
What Is the "tuple" Data Type? "tuple" is the immutable list data type in Python. Each element in a "tuple" object stores a reference to another data object.
"tuple" data type has the following main Features.
1. "tuple" data objects can be created in several ways:
Here are some examples on how to create "tuple" objects:
>>> (9, 1, 1) (9, 1, 1) >>> 9, 1, 1 (9, 1, 1) # a singleton tuple within parentheses >>> (9,) (9,) # a singleton tuple without parentheses >>> 9, (9,) # this is not a tuple. it's an integer in parentheses. >>> (9) 9 >>> ('nine', 'one', 'one') ('nine', 'one', 'one') # elements are of different data types >>> ('nine', b'one', 1) ('nine', b'one', 1)
2. "tuple" data type provides an array-style expression to access the element of a given position.
>>> x = ('nine', b'one', 1) >>> type(x) <class 'tuple'> >>> e = x[0] >>> e 'nine' >>> type(e) <class 'str'>
3. "tuple" data type is an immutable data type. Once a "tuple" data object is created to store a sequence of references, this sequence will never change.
>>> x = ('nine', b'one', 1) >>> e = x[0] >>> e 'nine' >>> x[0] = 'ten' TypeError: 'tuple' object does not support item assignment
Don't get confused about "tuple" immutability and "tuple" variable re-assignment capability. A "tuple" variable can be re-assigned with different "tuple" objects many times.
# new object is created for (9, 1, 1) and assigned to x >>> x = (9, 1, 1) >>> id(x) 4459103168 # assign the same object to y >>> y = x >>> id(y) 4459103168 # new object is created for ('nine', 'one', 'one') and assigned to x >>> x = ('nine', 'one', 'one') >>> id(x) 4458939456 # the first object for (9, 1, 1) is still there in memory >>> y (9, 1, 1) >>> id(y) 4459103168
3. Because "tuple" data type is immutable, an object created for a given object sequence is not allowed to change its value. This allow "tuple" objects to be cached in memory and reused later whenever are needed again. Reusing objects will reduce execution time and memory consumption.
However on my macOS computer, "tuple" objects are not cached and reused at all. The code example below shows you that there are 4 "tuple" objects created for 4 requests of (9,1,1).
>>> x = (9, 1, 1) >>> y = (9, 1, 1) >>> (id(x), id(y), id((9, 1, 1)), id(tuple([9, 1, 1]))) (4458939136, 4458939456, 4459103168, 4458811648)
5. "tuple" objects support the following operations:
Syntax Operation Note ---------- ------------- ------------------ x + y Concatenation x * n Repetition n is an "int" n * x Repetition Same as x * n s in x Look up s is an object s not in x Look up Same as !(s in x)
6. Some built-in functions are provided for "tuple" objects.
7. Some instance methods are provided for "tuple" objects.
Table of Contents
Common Features of All Data Types
Data Type - NoneType for Nothing
Data Type - 'bool' for Boolean Values
Data Type - 'int' for Integer Values
Data Type - 'float' for Real Numbers
Data Type - 'bytes' for Byte Sequence
Data Type - 'str' for Character String
►Data Type - 'tuple' for Immutable List
Data Type - 'list' for Mutable List
Data Type - 'set' for Unordered Collection
Data Type - 'dict' for Dictionary Table
Variables, Operations and Expressions
Function Statement and Function Call
List, Set and Dictionary Comprehensions
Packages and Package Directories
"pathlib" - Object-Oriented Filesystem Paths
"pip" - Package Installer for Python
SciPy.org - Python Libraries for Science
pandas - Data Analysis and Manipulation
Anaconda - Python Environment Manager