TUPLE
A tuple is an ordered collection of elements that can contain multiple data types, such as integers, floats, strings, lists, or even other tuples. Tuples are immutable, meaning their elements cannot be changed after creation. Tuples are defined using tuple()
or round brackets ()
.
Examples of Tuples
# Creating a tuple with integers
tuple1 = (89, 91, 88)
print(tuple1)
# Output: (89, 91, 88)
# Creating an empty tuple
tuple_marks = ()
# Or
tuple_marks = tuple()
# Creating a tuple with mixed values
tuple2 = (23, 45, 'Delhi')
print(tuple2)
# Output: (23, 45, 'Delhi')
# Creating a nested tuple
tuple3 = (23, 45, (4, 6), [2, -30])
print(tuple3)
# Output: (23, 45, (4, 6), [2, -30])
Tuples are Immutable
Once a tuple is created, its elements cannot be changed. Attempting to modify a tuple will raise a TypeError
.
tuple1 = (2, 4, 6)
# Attempt to change an element
tuple1[1] = 40
# Output: TypeError: 'tuple' object does not support item assignment
Creating a Tuple with a Single Element
If a tuple contains only one element, a comma ,
must follow the element. Without the comma, it is treated as the type of the single element.
Example:
# Without a comma (not a tuple)
single_tuple = (23)
print(type(single_tuple)) # Output: <class 'int'>
# With a comma (valid tuple)
single_tuple = (23,)
print(type(single_tuple)) # Output: <class 'tuple'>
Working with Tuples
Tuples support many of the same operations as lists, including accessing elements, concatenation, replication, and slicing.
1. Accessing Elements
Elements in a tuple can be accessed using their index, starting from 0
.
tuple1 = (10, 20, 30, 40)
print(tuple1[1]) # Output: 20
print(tuple1[-1]) # Output: 40
2. Concatenation
Tuples can be concatenated using the +
operator.
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
result = tuple1 + tuple2
print(result) # Output: (1, 2, 3, 4, 5, 6)
3. Replication
Tuples can be replicated using the *
operator.
tuple1 = (7, 8)
print(tuple1 * 3) # Output: (7, 8, 7, 8, 7, 8)
4. Slicing
Tuples support slicing to extract a subset of elements.
tuple1 = (10, 20, 30, 40, 50, 60)
# Slicing with positive indices
print(tuple1[1:4]) # Output: (20, 30, 40)
# Slicing with negative indices
print(tuple1[-4:-1]) # Output: (30, 40, 50)
# Slicing with a step value
print(tuple1[0:6:2]) # Output: (10, 30, 50)
Key Notes:
- Tuples are immutable, meaning elements cannot be modified after creation.
- Tuples are defined using parentheses
()
or thetuple()
function. - A single-element tuple must include a trailing comma
,
to be recognized as a tuple.
Try It Yourself
Problem 1: Create and Access a Tuple
Create a tuple with your 5 favorite colors and access the second and last colors.
Show Code
colors = ('Red', 'Blue', 'Green', 'Yellow', 'Purple')
print(colors[1]) # Output: Blue
print(colors[-1]) # Output: Purple
Problem 2: Concatenate and Slice Tuples
Create two tuples: one with 3 numbers and another with 3 fruits. Concatenate them and slice the resulting tuple to display the first 4 elements.
Show Code
numbers = (1, 2, 3)
fruits = ('Apple', 'Banana', 'Cherry')
# Concatenate tuples
combined = numbers + fruits
print(combined[:4]) # Output: (1, 2, 3, 'Apple')
Problem 3: Check Membership in a Tuple
Create a tuple of 5 cities and check if a specific city is present using the in
operator.
Show Code
cities = ('Delhi', 'Mumbai', 'Chennai', 'Kolkata', 'Bangalore')
print('Chennai' in cities) # Output: True
print('Pune' in cities) # Output: False