In Python, tuples are ordered collections, similar to lists, but unlike lists, they are immutable, meaning their elements cannot be changed after the tuple is created. Python provides several built-in methods that allow you to interact with tuples. In this lesson, you’ll learn about some common tuple methods.
You can create tuples by placing elements inside parentheses ()
.
numbers = (1, 2, 3, 4, 5)
fruits = ('apple', 'banana', 'cherry')
You can access individual elements in a tuple using indexing (0-based index). The index starts from 0
for the first element and -1
for the last element.
fruits = ('apple', 'banana', 'cherry')
print(fruits[0]) # Output: apple
print(fruits[-1]) # Output: cherry (last element)
fruits[0]
accesses the first element of the tuple.fruits[-1]
accesses the last element of the tuple.You can extract a part of a tuple using slicing. Slicing uses the format tuple[start:end:step]
.
numbers = (1, 2, 3, 4, 5)
sub_tuple = numbers[1:4] # Extract elements from index 1 to 3
print(sub_tuple) # Output: (2, 3, 4)
numbers[1:4]
extracts elements from index 1
to 3
(not including index 4
).Tuples have fewer built-in methods compared to lists since they are immutable. However, there are some key methods that you can use:
len()
The len()
function returns the number of elements in the tuple.
numbers = (1, 2, 3, 4, 5)
print(len(numbers)) # Output: 5
count()
The count()
method returns the number of occurrences of a specified element in the tuple.
fruits = ('apple', 'banana', 'cherry', 'banana')
count = fruits.count('banana')
print(count) # Output: 2
index()
The index()
method returns the index of the first occurrence of a specified element in the tuple. If the element is not found, it raises a ValueError
.
fruits = ('apple', 'banana', 'cherry')
index = fruits.index('banana')
print(index) # Output: 1
fruits.index('banana')
returns the index of the first occurrence of 'banana'
in the tuple.Tuples can also contain other tuples, and this is known as nested tuples.
nested_tuple = (1, 2, (3, 4, 5), 6)
print(nested_tuple[2]) # Output: (3, 4, 5)
print(nested_tuple[2][1]) # Output: 4
nested_tuple[2]
accesses the third element, which is another tuple (3, 4, 5)
.nested_tuple[2][1]
accesses the second element of the nested tuple, which is 4
.You can assign individual elements of a tuple to variables directly, which is known as tuple unpacking.
fruits = ('apple', 'banana', 'cherry')
a, b, c = fruits
print(a) # Output: apple
print(b) # Output: banana
print(c) # Output: cherry
a, b, c = fruits
unpacks the tuple into individual variables a
, b
, and c
.You can concatenate tuples using the +
operator and repeat them using the *
operator.
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
# Concatenation
new_tuple = tuple1 + tuple2
print(new_tuple) # Output: (1, 2, 3, 4, 5, 6)
# Repetition
repeated_tuple = tuple1 * 2
print(repeated_tuple) # Output: (1, 2, 3, 1, 2, 3)
Tuples can be compared using comparison operators (==
, !=
, <
, >
, etc.). The comparison is done element by element.
tuple1 = (1, 2, 3)
tuple2 = (1, 2, 3)
tuple3 = (4, 5, 6)
print(tuple1 == tuple2) # Output: True
print(tuple1 < tuple3) # Output: True
Since tuples are immutable, you cannot change their elements after they have been created. However, you can perform operations that create new tuples, such as concatenation and repetition.
Objective: Practice using tuple methods to manipulate and interact with tuples.
Create a Python File:
Create a file named tuple_methods_practice.py
.
Count Occurrences:
Count how many times the element 5
appears in a tuple.
Example:
numbers = (1, 5, 2, 5, 3, 5)
print(numbers.count(5))
Find Index:
Find the index of the element 'banana'
in a tuple.
Example:
fruits = ('apple', 'banana', 'cherry')
print(fruits.index('banana'))
Tuple Unpacking: Unpack the tuple into three variables and print each variable.
Example:
fruits = ('apple', 'banana', 'cherry')
a, b, c = fruits
print(a)
print(b)
print(c)
Concatenate Tuples: Concatenate two tuples into one.
Example:
tuple1 = (1, 2)
tuple2 = (3, 4)
result = tuple1 + tuple2
print(result)
Repeat a Tuple: Repeat a tuple a specified number of times.
Example:
tuple1 = (1, 2)
result = tuple1 * 3
print(result)