Python Tuples

1. What is a Tuple?

A tuple is a collection of items, just like a list.

But there is one big difference 👇
👉 Tuples cannot be changed.

Once a tuple is created, you cannot add, remove, or change items.

2. Creating a Tuple

Tuples are written using round brackets ( ).

Example


 

fruits = ("apple", "banana", "orange")
print(fruits)

3. Tuple with One Item (Important)

To create a tuple with one item, you must add a comma.

Example

single = ("apple",)
print(type(single))

❌ Without comma, it becomes a string.

4. Access Tuple Items

Tuples use index numbers, just like lists.

Index starts from 0.

Example

colors = ("red", "blue", "green")

print(colors[0])
print(colors[2])

5. Negative Indexing

Example

print(colors[-1])

6. Loop Through a Tuple

Example

for color in colors:
print(color)

7. Check Item Exists

Example

print("blue" in colors)

8. Tuple Length

Example

print(len(colors))

9. Tuple Cannot Be Changed (Very Important)

❌ Wrong Example

colors[1] = "yellow"

❌ Error: tuple does not support item assignment.

10. Change Tuple (Trick Method)

To change a tuple:

  1. Convert to list

  2. Change the list

  3. Convert back to tuple

Example

colors = ("red", "blue", "green")

temp = list(colors)
temp[1] = "yellow"
colors = tuple(temp)

print(colors)

11. Add Items to Tuple (Trick)

Example

fruits = ("apple", "banana")

fruits = fruits + ("orange",)
print(fruits)

12. Remove Items from Tuple (Trick)

Example

fruits = ("apple", "banana", "orange")

temp = list(fruits)
temp.remove("banana")
fruits = tuple(temp)

print(fruits)

13. Tuple Methods

Tuples have only two methods.

count()

nums = (1, 2, 2, 3)
print(nums.count(2))

index()

print(nums.index(3))

14. Why Use Tuples?

✔ Data should not change
✔ Faster than lists
✔ Protect important data

15. Common Beginner Mistakes

❌ Forgetting Comma in Single Tuple

item = ("apple")

✔ Correct:

item = ("apple",)

16. Simple Practice Examples

Example 1: Student Details

student = ("Amit", 20, "Delhi")
print(student)

Example 2: Loop Tuple

for item in student:
print(item)

Example 3: Check Length

print(len(student))

17. Summary (Python Tuples)

✔ Tuples store many items
✔ Tuples cannot be changed
✔ Use index to access items
✔ Only count() and index() methods
✔ Safe for fixed data

📘 Perfect for Beginner eBook

This chapter is ideal for:

  • Python beginner books

  • School & college students

  • Self-learners

If you want next, I can write:

  • Tuple Exercises

  • Sets (easy)

  • Dictionaries (easy)

  • Comparison: List vs Tuple

Just tell me 😊

You may also like...