Tuples In python
Understanding Tuples in Python
In Python, a tuple is like a container where you can store multiple items. Imagine a tuple as a box where you can keep different things (numbers, words, or both) together. Once you put something in this box, you can’t change it. That’s what makes tuples immutable (unchangeable).
What is a Tuple?
- A tuple is a collection of items, just like a list, but you cannot change items inside a tuple.
- Tuples are written using parentheses
().
How to Create a Tuple?
Here’s an example of a tuple:
fruits = ("apple", "banana", "cherry")This tuple has three items: "apple", "banana", and "cherry".
Features of Tuples
- Immutable: Once created, you can’t add, remove, or change items in a tuple.
- Ordered: Items in a tuple have a fixed order.
- Can Hold Different Data Types: A tuple can store numbers, words, or even a mix of both.
Example:
my_tuple = (1, "hello", 3.5)Why Use a Tuple?
- Use a tuple when you want to make sure your data can’t be changed.
- They’re faster than lists.
How to Access Items in a Tuple?
You can get items from a tuple using their position, called an index.
Get Avish Joshi’s stories in your inbox
Join Medium for free to get updates from this writer.
Example:
fruits = ("apple", "banana", "cherry")
print(fruits[1]) # Outputs: bananaWhen to Use Tuples?
- When storing data that shouldn’t be changed, like days of the week, months, or coordinates.
Example:
days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")Key Points
- Tuples are great for protecting data.
- Use them when you need a list of items that won’t change.






