sec02 - tuple Overview

tuple is a data structure that allows you to store multiple values in a single variable. It is similar to a list, but unlike lists, its elements cannot be added to or modified. Since elements cannot be changed, the data in a tuple remains exactly as it was when created. While the inability to edit is a disadvantage, it offers the advantage of faster processing speed compared to lists.

Aside from the immutability of elements, tuples behave similarly to lists. If you haven’t studied lists yet or only vaguely remember them, it is recommended to review lists first.

スポンサーリンク

Tuple Initialization

Basic Tuple Initialization

Let’s start with the simplest way to initialize a tuple. Write the code as follows:

num_tuple = ()

Assign parentheses () to a variable using the equals sign. This creates a tuple with 0 elements.

This example demonstrates initializing a tuple with 0 elements for illustrative purposes. However, tuples cannot be modified later to add elements, so initializing with 0 elements has limited practical use. (Using the + operator can combine elements from other tuples, but this generates a new tuple without modifying the original tuple.)

Tuples with Multiple Elements

A tuple can contain one or more elements. You can initialize tuples as follows:

numbers = (1, 2, 3, 4, 5)
primes = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29)
alphabets = ('a', 'b', 'c', 'd', 'e')
cities = ('Tokyo', 'New York', 'Paris', 'Rio de Janeiro', 'Cairo')

The code above initializes the following tuples: (1) a tuple of integers, (2) a tuple of prime numbers, (3) a tuple of alphabet letters, and (4) a tuple of city names.

Each element in a tuple is separated by a comma (,).

Initializing a Tuple with a Single Element

If a tuple contains only one element, a comma (,) must follow the element.

num_tuple = (1,)     # Add a comma when there is only one element
num_tuple = (1)      # Without the comma, it is treated as the integer `1` instead of a tuple
num_tuple = (1, 2)   # For two or more elements, write it like a list

Since tuples are defined using parentheses, this rule distinguishes them from numerical expressions and other operations.

Other Initialization Methods

Elements separated by commas are recognized as a tuple even without surrounding parentheses.

num_tuple = 1,       # Comma required if there is only one element
num_tuple = 1, 2, 3

Tuples with Multiple Data Types

While tuples are easier to understand and manage when they contain a single data type, such as numbers only or strings only, you can include multiple data types in a single tuple, as shown below.

mixed = (1, 'two', 3.0, [4], (5, 5.5))

The tuple above contains the following data types: int, string, float, list, and tuple. Python tuples make it easy to manage multiple types of data in a single structure.

Accessing Tuple Elements

To access elements in a tuple, use [n] just like with lists or strings. Let’s access the elements of the "mixed" variable.

mixed = (1, 'two', 3.0, [4], (5, 5.5))
print(mixed[0])  # 1
print(mixed[1])  # 'two'
print(mixed[2])  # 3.0
print(mixed[3])  # [4]
print(mixed[4])  # (5, 5.5)

Using indexes, you can retrieve elements one at a time.

(Tuple slicing will be covered in a dedicated lesson.)

スポンサーリンク

Tuple Length (len() Function)

To determine the number of elements in a tuple, use the len() function.

mixed = (1, 'two', 3.0, [4], (5, 5.5))
print(len(mixed))  # 5

Nested Tuples

In Python, tuples can contain other tuples or data structures as elements. Embedding one structure within another is called nesting, which can be done to any depth.

Nested tuples can be initialized as follows:

tuple_2D = (
    (1, 2, 3),
    [4, 5, 6]
)

The "tuple_2D" variable contains both tuple and list elements.

Accessing each element of "tuple_2D" is done the same way as with lists: write the indexes in sequence to reach the desired element (e.g., tuple_2D[n][m]).

tuple_2D = (
    (1, 2, 3),
    [4, 5, 6]
)
print(tuple_2D)        # ((1, 2, 3), [4, 5, 6])
print(tuple_2D[0])     # (1, 2, 3)
print(tuple_2D[0][1])  # 2
print(tuple_2D[1])     # [4, 5, 6]
print(tuple_2D[1][2])  # 6
スポンサーリンク