
Table of Contents(目次)
Editing tuples
Tuples cannot be edited. Therefore, even if you try to overwrite a value by specifying an index like var[n] = data
, it will result in an error. This is also true when specifying a range using slicing.

+ Operator
Tuples can concatenate elements using the +
operator. The concatenation with +
extracts elements from each tuple and creates a new tuple. Therefore, the original tuple is not edited.
Just like with strings and lists, operations with different data types are not allowed. When using the + operator, the right and left sides must always be tuples.
Run the following code to check its behavior.
sample_tuple = ('a', 'b', 'c')
new_tuple = sample_tuple + (1, 2, 3)
print(new_tuple) # ('a', 'b', 'c', 1, 2, 3)
print(sample_tuple) # ('a', 'b', 'c') # The original tuple does not change
The code above combines the tuple ('a', 'b', 'c')
assigned to the variable "sample_tuple" with the tuple (1, 2, 3)
using the +
operator, generating a new tuple. The generated tuple is assigned to the variable "new_tuple".
The newly created tuple (('a', 'b', 'c', 1, 2, 3)
) stores the elements of the two original tuples in order. The contents of the variable "sample_tuple" remain unchanged.
* Operator
Using the *
operator allows you to create a new tuple with the elements of the original tuple repeated a specified number of times. This behaves in the same way as repeating strings or lists.
Run the following code to check its behavior.
sample_tuple = ('a', 'b', 'c')
new_tuple = sample_tuple * 4
print(new_tuple) # ('a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c')
print(sample_tuple) # ('a', 'b', 'c') # The original tuple does not change
A new tuple with the elements of ('a', 'b', 'c')
repeated 4 times is created and assigned to the variable "new_tuple".
Overview of tuple methods
Because tuples cannot be edited, there are no methods for editing their elements.
The only methods provided for tuples are count()
and index()
. The usage of these two methods is the same as for lists.
index()
The index()
method searches for an element equal to the specified value and returns the index of the first match if it exists. If no element equal to the specified value exists in the tuple, it raises an error.
There are three syntaxes: passing only the element to search for, specifying the element and a start position, and specifying the element along with start and end positions.
index(x)
- Searches for a value equal to
x
across the entire tuple
- Searches for a value equal to
index(x, start)
- Searches for a value equal to
x
in the rangetuple[start:]
- Searches for a value equal to
index(x, start, end)
- Searches for a value equal to
x
in the rangetuple[start:end]
- Searches for a value equal to
The optional arguments "start" and "end" are interpreted as slice notation and specify the search range within the tuple. The returned value represents the index of the entire tuple, not its relative position from “start”.
First, let's check code that does not specify "start" or "end".
sample_tuple = ('a', 'b', 'c', 'a')
result = sample_tuple.index('b')
print(result) # 1 (index() returns the position of the element.)
The result of executing the above code will be "1". It returns the index of the first 'b'
found.
Next, let's check the code specifying only "start".
sample_tuple = ('a', 'b', 'c', 'b')
result = sample_tuple.index('b', 2)
print(result) # 3 (index() returns the position of the element.)
In this code, the search is performed in the range sample_tuple[2:]
. Therefore, the first 'b'
([1]) is outside the search range, and the second 'b'
([3]) is matched. The execution result is “3”.
Next, let's check the code specifying both "start" and "end".
sample_tuple = ('a', 'b', 'c', 'b')
result = sample_tuple.index('b', 2, 3)
print(result) # error (index() raises an error if the element is not found within the specified range.)
In this code, the search is performed in the range sample_tuple[2:3]
. The first 'b'
([1]) is outside the search range, and the second 'b'
([3]) is also out of range, resulting in an error. (Slices do not include the element specified by "end".)
If you want to search up to the last element, omit specifying "end".
count()
The count()
method returns the number of elements in the tuple that are equal to the specified value.
sample_tuple = ('a', 'b', 'c', 'a')
result = sample_tuple.count('a')
print(result) # 2
In this code, since there are two 'a'
elements in the tuple, the result is "2".
The count()
method returns "0" instead of an error if no elements exist.
sample_tuple = ('a', 'b', 'c', 'a')
result = sample_tuple.count('z')
print(result) # 0