


Khusboo Tayal
Tuples are one of the fundamental data structures in Python. Similar to lists, tuples store a collection of items, but unlike lists, tuples are immutable—which means you cannot change, add, or remove elements after the tuple is created.
In this guide, you'll learn what tuples are, how to use them, when to use them over lists, and explore practical examples.
A tuple is a built-in Python data type used to store multiple items in a single variable. It is defined by placing elements inside parentheses (), separated by commas.
my_tuple = ("apple", "banana", "cherry")
print(my_tuple) # Output: ('apple', 'banana', 'cherry')mixed_tuple = (1, "hello", 3.14, True)
You can create a tuple in several ways:
fruits = ("apple", "banana", "mango")numbers = 1, 2, 3
single_item = ("apple",)
print(type(single_item)) # Output: <class 'tuple'>Without the comma, Python treats it as a string or other type.
You can access tuple elements using indexing or slicing.
my_tuple = ("red", "green", "blue")
print(my_tuple[0]) # Output: red
print(my_tuple[-1]) # Output: bluecolors = ("red", "green", "blue", "yellow", "orange")
print(colors[1:4]) # Output: ('green', 'blue', 'yellow')Although tuples are immutable, they support a few useful methods:
t = (1, 2, 3, 2, 2, 4) print(t.count(2)) # Output: 3
print(t.index(4)) # Output: 5
Tuple unpacking allows you to assign each item of a tuple to a separate variable.
person = ("Alice", 25, "Engineer")
name, age, profession = person
print(name) # Output: Alice
print(profession) # Output: EngineerYou can loop through a tuple using a for loop:
cities = ("Delhi", "Paris", "London")
for city in cities:
print(city)coordinates = (10.0, 20.0)
locations = {coordinates: "City Center"}Tuples can contain other tuples (or other sequences):
nested = ((1, 2), (3, 4), (5, 6)) print(nested[1]) # Output: (3, 4) print(nested[1][0]) # Output: 3
Although tuples themselves are immutable, if they contain mutable elements like lists, those elements can be modified.
t = (1, 2, [3, 4]) t[2][0] = 99 print(t) # Output: (1, 2, [99, 4])
You can convert a list to a tuple and vice versa.
my_list = [1, 2, 3] my_tuple = tuple(my_list) print(my_tuple) # Output: (1, 2, 3) new_list = list(my_tuple) print(new_list) # Output: [1, 2, 3]
Tuples are an efficient and secure way to store data that should not be changed. Their immutability and performance advantages make them ideal for fixed data, function returns, and dictionary keys.
Learning when to use tuples over lists will make your Python code more robust and maintainable.
Q1: Can I change the elements of a tuple?
No. Tuples are immutable. Once created, you cannot change their elements directly.
Q2: Can a tuple contain other tuples or lists?
Yes. Tuples can contain any data type, including other tuples and lists.
Q3: Are tuples faster than lists?
Yes, tuples are slightly faster and more memory-efficient than lists, especially for fixed-size collections.
Subscribe to our newsletter to get the latest Python tutorials, project ideas, and updates right to your inbox.
#python #pythontuple #tuplesinpython #immutabledatatypes #pythonforbeginners #pythontutorial #learnpython #pythondata