Tuples in Python
A tuple is a collection used to store multiple values in a single variable.
A tuple is similar to a list, but the main difference is that a tuple cannot be changed after it is created.
Example
student = ("Ram", 20, "CSE")
Here, student is a tuple containing three values.
1. Creating a Tuple
Tuples are usually created using parentheses ().
numbers = (10, 20, 30, 40) names = ("Ram", "Shyam", "Hari")
A tuple can also contain different data types:
student = ("Ram", 20, 85.5, True)
2. Tuple Indexing
Like lists, tuple indexes start from 0.
fruits = ("Apple", "Banana", "Mango")
Apple Banana Mango 0 1 2
Example:
print(fruits[0]) print(fruits[1])
Output:
Apple Banana
3. Negative Indexing
Tuples also support negative indexing.
fruits = ("Apple", "Banana", "Mango") print(fruits[-1]) print(fruits[-2])
Output:
Mango Banana
4. Tuple Slicing
You can extract part of a tuple using slicing.
numbers = (10, 20, 30, 40, 50) print(numbers[1:4])
Output:
(20, 30, 40)
Other examples:
print(numbers[:3]) print(numbers[2:]) print(numbers[:])
5. Tuples Are Immutable
The most important property of a tuple is that it cannot be changed after creation.
For example:
numbers = (10, 20, 30) numbers[0] = 100
This produces an error because tuple items cannot be modified.
If you need to change the data, you can create a new tuple:
numbers = (10, 20, 30) numbers = (100, 20, 30) print(numbers)
Output:
(100, 20, 30)
6. Tuple with One Item
There is an important rule when creating a tuple with only one item.
This is not a tuple:
number = (10)
It is an integer.
To create a one-item tuple, add a comma:
number = (10,)
Check:
print(type(number))
Output:
<class 'tuple'>
7. Tuple Without Parentheses
Python also allows you to create a tuple without parentheses.
numbers = 10, 20, 30
This is a tuple.
print(type(numbers))
Output:
<class 'tuple'>
However, using parentheses is usually clearer for beginners:
numbers = (10, 20, 30)
8. Length of a Tuple
Use len():
numbers = (10, 20, 30, 40) print(len(numbers))
Output:
4
9. Checking Items
Use in:
fruits = ("Apple", "Banana", "Mango") print("Apple" in fruits)
Output:
True
Using not in:
print("Orange" not in fruits)
Output:
True
10. Tuple Methods
Tuples have only a few built-in methods because they cannot be modified.
count()
Counts how many times a value appears.
numbers = (10, 20, 10, 30, 10) print(numbers.count(10))
Output:
3
index()
Returns the position of an item.
fruits = ("Apple", "Banana", "Mango") print(fruits.index("Banana"))
Output:
1
11. Loop Through a Tuple
You can use a for loop:
fruits = ("Apple", "Banana", "Mango") for fruit in fruits: print(fruit)
Output:
Apple Banana Mango
12. Tuple Unpacking
You can assign tuple values to multiple variables.
student = ("Ram", 20, "CSE") name, age, course = student print(name) print(age) print(course)
Output:
Ram 20 CSE
The number of variables should normally match the number of values.
13. Nested Tuples
A tuple can contain another tuple.
students = ( ("Ram", 20), ("Shyam", 21), ("Hari", 19) )
Access an item:
print(students[0])
Output:
("Ram", 20)
Access a specific value:
print(students[0][0])
Output:
Ram
14. Converting List to Tuple
Use tuple():
numbers = [10, 20, 30] numbers = tuple(numbers) print(numbers)
Output:
(10, 20, 30)
15. Converting Tuple to List
Use list():
numbers = (10, 20, 30) numbers = list(numbers) print(numbers)
Output:
[10, 20, 30]
This is useful when you need to modify tuple data.
Tuple vs List
| Feature | List | Tuple |
|---|---|---|
| Brackets | [] | () |
| Ordered | Yes | Yes |
| Mutable | Yes | No |
| Allows duplicates | Yes | Yes |
| Indexing | Yes | Yes |
| Slicing | Yes | Yes |
append() | Yes | No |
remove() | Yes | No |
count() | Yes | Yes |
index() | Yes | Yes |
Example
List:
students = ["Ram", "Shyam", "Hari"]
Tuple:
students = ("Ram", "Shyam", "Hari")
Use a list when the data needs to change.
Use a tuple when the data should remain unchanged.
Simple Definition
Tuple = An ordered and immutable collection of values in Python.