Introduction
A tuple is a collection used to store multiple elements int a single variable. Each item in a tuple has an index. starting from 0 for the first element, 1 for the second and so on.
Tuple elements are immutable, ordered and supports duplicate values.
As Tuples are immutable, it cannot be modified once created, meaning elements can not be added, removed or changed.
Tuples are defined using round brackets ().
The len() function is used to find the number of elements in a tuple.
# Defining tuple
directions = ("up", "down", "left", "right")
# Display tuple
print("Print the tuple >>")
print(directions)
# Finding the number of elements in tuple
print("Print the length of tuple >>")
print(len(directions))
# single element tuple
single_element_tuple = ("blog",)
print(single_element_tuple)
# Empty tuple
empty_tuple = ()
print(empty_tuple)
Tuple Type:
- In Python, tuples are internally recognized as objects of type: tuple.
# Define a tuple
directions = ("up", "down", "left", "right")
# Check the type of tuple
print(type(directions))
# Output <class 'tuple'>
Tuples with different data types
- A tuple can store elements of various data types, including strings, integers, boolean values.
# Tuple with String Values
tuple_alphabetics = ("east", "west", "north", "south")
# Tuple with Numbers / Integer values
tuple_integers = (10,11,100,111,1000, 1111)
# Tuple with Boolean values
tuple_booleans = (True, False, False, True)
# Tuple can have mixed data types too
mixed_tuple = (11, "Mix", False, "Match")
print(tuple_alphabetics)
# Output : ("east", "west", "north", "south")
print(tuple_integers)
# Output : (10,11,100,111,1000, 1111)
print(tuple_booleans)
# Output : (True, False, False, True)
print(mixed_tuple)
# Output : (11, "Mix", False, "Match")
Tuple Constructor
- Tuples can also be created using the tuple() constructor
direction_tuple = tuple(("up", "down", "left", "right"))
print(direction_tuple)
# Output ('up', 'down', 'left', 'right')
Finding Max & Min value of tuple
numbers_tuple = (1990, 1991, 1992, 2001, 2002, 2020, 1947)
print("Highest number >> ", max(numbers_tuple))
# Output : Highest number >> 2020
print("Lowest number >> ", min(numbers_tuple))
# Output : Lowest number >> 1947
print("no of elements in tuple >>", len(numbers_tuple))
# Output: no of elements in tuple >> 7
Accessing Tuple elements :
alphabetic_tuple = ('a','b','c','d','e')
print(alphabetic_tuple[0]) # Output: 'a' – first element
print(alphabetic_tuple[-1]) # Output: 'e' – last element
print(alphabetic_tuple[1:4]) # Output: ('b', 'c', 'd') – slicing
Slicing of Tuple:
- Tupple supports slicing, allowing extraction of a subset of elements
- syntax:
- tuple[start : end]
- Omitting the start index begins the slice from the first element
- Omitting the end index extends the slice to the last element
- Negative slicing allows selection from the end.
print(alphabetic_tuple[:4]) # Output: ('a', 'b', 'c', 'd')
print(alphabetic_tuple[-4:-2]) # Output: ('b', 'c')
Tuple Operations: Repetition and Concatenation
tuple1 = (1, 2, 9)
tuple2 = (7, 8)
print(tuple1 + tuple2) # Output: (1, 2, 9, 7, 8)
print(tuple1 * 2) # (1, 2, 9, 1, 2, 9)
You can also check if any number or String exist in Tuple or not.
Checking element existence in tuple
print("2 in tuple1 >> ",2 in tuple1) # Output: 2 in tuple1 >> True
print("10 not in tuple2 >>",10 not in tuple2) # Output: 10 not in tuple2 >> True
Immutability of Tuple:
As you know, Tuple is immutable so you can not modify tuple.
tuple1 = (1,2,3)
tuple1[2] = 10
Error:
tuple1[2] = 10
~~~~~~^^^
TypeError: 'tuple' object does not support item assignment
However, if a tuple contains mutable elements (like lists), those inner elements can be modified:
tupleContainsMuttableElement = (1, [2, 3])
tupleContainsMuttableElement[1][0] = 99
print("Modified tuple when element is List", tupleContainsMuttableElement)
# Output: Modified tuple when element is List (1, [99, 3])
Tuple Methods
print(tMethods.count(2)) # Output: 2 — counts occurrences
print(tMethods.index(3)) # Output: 3 — returns the first index of value 3
Tuple Unpacking:
Python allows easy unpacking of tuples into variables:
individual = ("Umang", 35, "Lead Software Engineer")
name, age, profession = individual
print("name>> ", name) # name>> Umang
print("age>> ", age) # age>> 35
print("profession>>", profession) # profession>> Lead Software Engineer
You can also use the * operator to capture multiple values:
numbers = (1, 2, 10, 9, 8)
a, *b, c = numbers
print(a, b, c) # 1 [2, 10, 9] 8
Nesting and Tuple of Tuples
nested = ((1, 2), (3, 4), (5, 6))
print("Nested Tuple>> ", nested[1][0]) # Output: Nested Tuple>> 3
Why Use Tuples?
- Faster than lists (since they’re immutable)
- Safe from accidental modification
- Can be used as keys in dictionaries (lists cannot)
- Useful for returning multiple values from functions
How to return multiple values:
def min_max(nums):
return (min(nums), max(nums))
result = min_max([3, 1, 9, 6])
print("Min & Max values>> ", result) # Output: Min & Max values>> (1, 9)
Summary
Tuples are an efficient and secure way to store fixed collections of data. Their immutability makes them ideal for representing constant sets of values, like coordinates, database records, or return values from functions.
