A tuple is a collection used to store multiple values in a single variable. Tuples are similar to lists, but they are immutable, which means their elements cannot be changed after the tuple is created.
A tuple is an ordered collection of values.
numbers = (10, 20, 30, 40)
print(numbers)
fruits = ("Apple", "Banana", "Mango")
print(fruits)
A tuple can contain values of different data types.
data = (10, "Python", 25.5, True)
print(data)
Tuples allow duplicate values.
numbers = (10, 20, 10, 30, 20)
print(numbers)
Tuple elements are accessed using indexes. Indexing starts from 0.
names = ("Amit", "Rahul", "Priya")
print(names[0])
print(names[1])
print(names[2])
Tuples also support negative indexing.
names = ("Amit", "Rahul", "Priya")
print(names[-1])
print(names[-2])
The len() function returns the number of elements in a tuple.
numbers = (10, 20, 30, 40)
print(len(numbers))
Tuple slicing is used to get a portion of a tuple.
numbers = (10, 20, 30, 40, 50)
print(numbers[1:4])
Tuples are immutable. This means we cannot change an existing tuple element.
numbers = (10, 20, 30)
numbers[1] = 50
Unlike lists, tuple elements cannot be directly changed.
A single-item tuple requires a trailing comma.
number = (10,)
print(type(number))
Without the comma, (10) is simply an integer expression.
Python also allows tuples to be created without parentheses using comma-separated values.
numbers = 10, 20, 30
print(numbers)
The tuple() function can be used to create a tuple from another iterable.
numbers = tuple([10, 20, 30])
print(numbers)
text = "Python"
letters = tuple(text)
print(letters)
The in operator checks whether a value exists in a tuple.
fruits = ("Apple", "Banana", "Mango")
print("Banana" in fruits)
print("Orange" in fruits)
fruits = ("Apple", "Banana", "Mango")
for fruit in fruits:
print(fruit)
numbers = (10, 20, 30)
i = 0
while i < len(numbers):
print(numbers[i])
i += 1
The + operator can combine two tuples.
tuple1 = (10, 20)
tuple2 = (30, 40)
result = tuple1 + tuple2
print(result)
The * operator can repeat a tuple.
numbers = (1, 2)
print(numbers * 3)
A tuple can contain other tuples.
students = (
("Amit", 20),
("Rahul", 21),
("Priya", 19)
)
print(students)
students = (
("Amit", 20),
("Rahul", 21)
)
print(students[0][0])
print(students[1][1])
Tuple unpacking allows us to assign tuple elements to multiple variables.
student = ("Rahul", 20, "Python")
name, age, course = student
print(name)
print(age)
print(course)
The * operator can collect multiple remaining values during unpacking.
numbers = (10, 20, 30, 40, 50)
first, *middle, last = numbers
print(first)
print(middle)
print(last)
Tuples have fewer methods than lists because tuples are immutable.
The two main tuple methods are:
numbers = (10, 20, 10, 30)
print(numbers.count(10))
print(numbers.index(20))
The count() method returns how many times a value occurs in a tuple.
numbers = (10, 20, 10, 30, 10)
print(numbers.count(10))
The index() method returns the index of the first occurrence of a value.
fruits = ("Apple", "Banana", "Mango")
print(fruits.index("Mango"))
| List | Tuple |
|---|---|
| Uses [ ] | Uses ( ) |
| Mutable | Immutable |
| Has many modification methods | Has fewer methods |
| Suitable for changeable collections | Suitable for fixed collections |
Tuples are useful when the collection should not be modified after creation.
For example:
coordinates = (25.5941, 85.1376)
rgb = (255, 255, 255)
student_id = ("ST101", "Rahul")
These values can be represented as fixed groups of related data.
Question: Which statement about Python tuples is correct?