Python Tuples Explained: A Beginner’s Guide to Immutable Data in Python


📘 Tuples in Python – A Complete Guide
INTRODUCTION
In this article, we are going to learn about the concept of tuples in Python. As we discussed in the earlier article about lists, we can think of tuples as lists — but unlike lists, tuples are immutable (not changeable).
WHAT IS A TUPLE?
Tuples are very useful in real-world applications. You might wonder: Why are tuples useful if we cannot change them?
Well, sometimes we want data that must remain fixed. For example, think about your Google account. You use this account to log in or sign up for different platforms. If that account were to be modified, you could lose access to those platforms.
That’s where tuples come in — they allow us to store unchangeable, reliable data.
To create a tuple, we assign values inside parentheses ()
to a variable. Similar to lists, tuples can hold multiple data types.
CREATING TUPLES
We can create a tuple with:
no elements,
a single element, or
multiple elements.
Empty Tuple
tuple1 = ()
print(tuple1)
# Output: ()
Single Element
tuple2 = (5,)
print(tuple2)
# Output: (5,)
⚠️ Note: Don’t forget the comma after the single element. If you write (5)
, Python treats it as just the number 5
, not a tuple.
Multiple Elements
tuple3 = (1, 2, 3, 4, 5)
print(tuple3)
# Output: (1, 2, 3, 4, 5)
ACCESSING ELEMENTS
Just like lists, tuples support indexing. The first element has index 0
.
a = ("a", "b", 11, "d", 0, "f")
print(a) # ('a', 'b', 11, 'd', 0, 'f')
print(a[0]) # a
print(a[-1]) # f
print(a[1:3]) # ('b', 11)
IMMUTABLE
Unlike lists, tuples are immutable — we cannot modify them once created.
a = ("a", "b", 11, "d", 0, "f")
a[0] = 1 # ❌ Error
TUPLE OPERATIONS
1. Concatenation
Combine two or more tuples:
tuple1 = ("a", "e")
tuple2 = ("i", "o", "u")
vowels = tuple1 + tuple2
print(vowels)
# Output: ('a', 'e', 'i', 'o', 'u')
2. Repetition
Repeat elements:
a = ("tuple",) * 3
print(a)
# Output: ('tuple', 'tuple', 'tuple')
3. Membership
Check if an element exists:
a = (1, 2, 3, 4, 5)
print(6 in a) # False
print(3 in a) # True
TUPLE METHODS
Tuples have only two built-in methods:
1. count()
Returns how many times an element appears.
a = ("m", "e", "o", "e", "z")
print(a.count("m")) # 1
print(a.count("e")) # 2
2. index()
Returns the position of an element (first occurrence).
a = ("m", "e", "o", "e", "z")
print(a.index("o")) # 2
print(a.index("e")) # 1
WHEN TO USE TUPLES?
When you need fixed data that should not change.
Tuples can be used as dictionary keys (lists can’t).
Useful for storing GPS coordinates like latitude and longitude (which should never change).
TUPLE UNPACKING
Tuple unpacking allows assigning values to multiple variables in one line.
person = ("John", 20, "Degree Student")
name, age, job = person
print(name) # John
print(age) # 20
print(job) # Degree Student
It can even work with flexible unpacking:
numbers = (100, 101, 108, 130, 15)
a, *b, c = numbers
print(a) # 100
print(b) # [101, 108, 130]
print(c) # 15
LISTS VS TUPLES
Feature | List (✅ Mutable) | Tuple (❌ Immutable) |
Mutability | ✅ Mutable | ❌ Immutable |
Performance | 🐢 Slower | ⚡ Faster |
Use Cases | Dynamic data | Fixed data |
PRACTICE
Try the following exercises:
Create a tuple of length 9 with any elements.
Retrieve the 4th element.
Retrieve elements from index 3 to 7.
Return the index of any element.
Use the
count()
method on any element.
💡 For extra clarity: repeat the same with a list, then convert it into a tuple and compare.
CONCLUSION
Thank you for reading this article! 🙌
If you found it helpful, consider subscribing to the newsletter and sharing it with friends who might benefit from it. I’d also love to hear your thoughts, ideas, or feedback — feel free to reach out anytime.
Subscribe to my newsletter
Read articles from Tammana Prajitha directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
