Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Tuples

A tuple in Python is an ordered, immutable collection of elements. Unlike lists, tuples cannot be modified (no adding, removing, or changing elements after creation). Tuples are useful for storing data that should not change.

1. Creating a Tuple

Tuples are created using parentheses () with elements separated by commas.

# Creating a tuple of numbers
numbers = (1, 2, 3, 4, 5)

# Creating a tuple of strings
fruits = ("apple", "banana", "cherry")

# Creating a mixed tuple
mixed_tuple = (10, "Python", 3.14, True)

print(numbers)      # Output: (1, 2, 3, 4, 5)
print(fruits)       # Output: ('apple', 'banana', 'cherry')
print(mixed_tuple)  # Output: (10, 'Python', 3.14, True)

Note: Tuples can also be created without parentheses (using just commas):

tuple1 = 1, 2, 3
print(tuple1)  # Output: (1, 2, 3)

Single-element Tuple:
To create a tuple with one element, add a comma after the element:

single_element_tuple = (10,)  #  Correct
not_a_tuple = (10)  #  This is just an integer

print(type(single_element_tuple))  # Output: <class 'tuple'>
print(type(not_a_tuple))           # Output: <class 'int'>


2. Accessing Tuple Elements

Like lists, tuple elements are indexed (starting from 0).

fruits = ("apple", "banana", "cherry")

print(fruits[0])   # Output: apple
print(fruits[1])   # Output: banana
print(fruits[-1])  # Output: cherry (Last element)

Tuple Slicing:

numbers = (10, 20, 30, 40, 50)

print(numbers[1:4])  # Output: (20, 30, 40) (From index 1 to 3)
print(numbers[:3])   # Output: (10, 20, 30) (First 3 elements)
print(numbers[2:])   # Output: (30, 40, 50) (From index 2 to end)
print(numbers[::-1]) # Output: (50, 40, 30, 20, 10) (Reversed)

3. Tuple is Immutable

Tuples cannot be modified after creation.

fruits = ("apple", "banana", "cherry")

# Trying to change an element (this will cause an error)
# fruits[1] = "grape"  #  TypeError: 'tuple' object does not support item assignment

However, you can convert a tuple to a list, modify it, and convert it back:

fruits = ("apple", "banana", "cherry")

# Convert tuple to list
fruits_list = list(fruits)
fruits_list[1] = "grape"  # Modify element
fruits = tuple(fruits_list)  # Convert back to tuple

print(fruits)  # Output: ('apple', 'grape', 'cherry')

4. Tuple Operations

Concatenation (+):
Tuples can be combined to create new tuples.

a = (1, 2, 3)
b = (4, 5, 6)

result = a + b
print(result)  # Output: (1, 2, 3, 4, 5, 6)

Repetition (*):
Repeats the elements of a tuple.

x = (10, 20)
print(x * 3)  # Output: (10, 20, 10, 20, 10, 20)

Checking if an item exists (in):

fruits = ("apple", "banana", "cherry")

print("banana" in fruits)  # Output: True
print("grape" in fruits)   # Output: False

5. Tuple Methods

Tuples have fewer methods than lists because they are immutable.

MethodDescription
count(x)Returns the number of times x appears in the tuple
index(x)Returns the first index of x in the tuple

Example:

numbers = (1, 2, 3, 2, 4, 2)

print(numbers.count(2))  # Output: 3 (Number of times 2 appears)
print(numbers.index(3))  # Output: 2 (Index of first occurrence of 3)

6. Tuple Packing & Unpacking

Packing:
Assigning multiple values into a tuple.

person = ("Alice", 25, "Engineer")
print(person)  # Output: ('Alice', 25, 'Engineer')

Unpacking:
Extracting values from a tuple into separate variables.

name, age, profession = person

print(name)       # Output: Alice
print(age)        # Output: 25
print(profession) # Output: Engineer

Using * for multiple values:

numbers = (1, 2, 3, 4, 5)

a, *b, c = numbers
print(a)  # Output: 1
print(b)  # Output: [2, 3, 4] (List of middle elements)
print(c)  # Output: 5

7. Nested Tuples

Tuples can contain other tuples.

nested_tuple = ((1, 2, 3), ("a", "b", "c"))

print(nested_tuple[0])     # Output: (1, 2, 3)
print(nested_tuple[1][2])  # Output: c (Accessing nested element)

    Leave a Reply

    Your email address will not be published.

    Need Help?