Lesson 28 of 70 – Python Tuples
40%

Python Tuples

A tuple is a collection used to store multiple values in a single variable. Tuples are similar to lists, but they are immutable, which means their elements cannot be changed after the tuple is created.

Note: Tuples are usually written using parentheses ( ).
What is a Tuple?

A tuple is an ordered collection of values.

numbers = (10, 20, 30, 40)

print(numbers)
Output:
(10, 20, 30, 40)
Creating a Tuple
fruits = ("Apple", "Banana", "Mango")

print(fruits)
Output:
('Apple', 'Banana', 'Mango')
Tuple with Different Data Types

A tuple can contain values of different data types.

data = (10, "Python", 25.5, True)

print(data)
Output:
(10, 'Python', 25.5, True)
Tuple with Duplicate Values

Tuples allow duplicate values.

numbers = (10, 20, 10, 30, 20)

print(numbers)
Output:
(10, 20, 10, 30, 20)
Tuple Indexing

Tuple elements are accessed using indexes. Indexing starts from 0.

names = ("Amit", "Rahul", "Priya")

print(names[0])
print(names[1])
print(names[2])
Output:
Amit
Rahul
Priya
Negative Indexing

Tuples also support negative indexing.

names = ("Amit", "Rahul", "Priya")

print(names[-1])
print(names[-2])
Output:
Priya
Rahul
Tuple Length

The len() function returns the number of elements in a tuple.

numbers = (10, 20, 30, 40)

print(len(numbers))
Output:
4
Tuple Slicing

Tuple slicing is used to get a portion of a tuple.

numbers = (10, 20, 30, 40, 50)

print(numbers[1:4])
Output:
(20, 30, 40)
Tuple Immutability

Tuples are immutable. This means we cannot change an existing tuple element.

numbers = (10, 20, 30)

numbers[1] = 50
Result:
TypeError: 'tuple' object does not support item assignment

Unlike lists, tuple elements cannot be directly changed.

Single Item Tuple

A single-item tuple requires a trailing comma.

number = (10,)

print(type(number))
Output:
<class 'tuple'>

Without the comma, (10) is simply an integer expression.

Tuple Without Parentheses

Python also allows tuples to be created without parentheses using comma-separated values.

numbers = 10, 20, 30

print(numbers)
Output:
(10, 20, 30)
Tuple Constructor

The tuple() function can be used to create a tuple from another iterable.

numbers = tuple([10, 20, 30])

print(numbers)
Output:
(10, 20, 30)
Convert String to Tuple
text = "Python"

letters = tuple(text)

print(letters)
Output:
('P', 'y', 't', 'h', 'o', 'n')
Check Item in Tuple

The in operator checks whether a value exists in a tuple.

fruits = ("Apple", "Banana", "Mango")

print("Banana" in fruits)
print("Orange" in fruits)
Output:
True
False
Loop Through a Tuple
fruits = ("Apple", "Banana", "Mango")

for fruit in fruits:
    print(fruit)
Output:
Apple
Banana
Mango
Tuple with While Loop
numbers = (10, 20, 30)

i = 0

while i < len(numbers):
    print(numbers[i])
    i += 1
Output:
10
20
30
Tuple Concatenation

The + operator can combine two tuples.

tuple1 = (10, 20)
tuple2 = (30, 40)

result = tuple1 + tuple2

print(result)
Output:
(10, 20, 30, 40)
Repeating a Tuple

The * operator can repeat a tuple.

numbers = (1, 2)

print(numbers * 3)
Output:
(1, 2, 1, 2, 1, 2)
Nested Tuple

A tuple can contain other tuples.

students = (
    ("Amit", 20),
    ("Rahul", 21),
    ("Priya", 19)
)

print(students)
Output:
(('Amit', 20), ('Rahul', 21), ('Priya', 19))
Accessing Nested Tuple
students = (
    ("Amit", 20),
    ("Rahul", 21)
)

print(students[0][0])
print(students[1][1])
Output:
Amit
21
Tuple Unpacking

Tuple unpacking allows us to assign tuple elements to multiple variables.

student = ("Rahul", 20, "Python")

name, age, course = student

print(name)
print(age)
print(course)
Output:
Rahul
20
Python
Using asterisk in Tuple Unpacking

The * operator can collect multiple remaining values during unpacking.

numbers = (10, 20, 30, 40, 50)

first, *middle, last = numbers

print(first)
print(middle)
print(last)
Output:
10
[20, 30, 40]
50
Tuple Methods

Tuples have fewer methods than lists because tuples are immutable.

The two main tuple methods are:

  • count() – counts occurrences of a value.
  • index() – returns the index of a value.
numbers = (10, 20, 10, 30)

print(numbers.count(10))
print(numbers.index(20))
Output:
2
1
Tuple count()

The count() method returns how many times a value occurs in a tuple.

numbers = (10, 20, 10, 30, 10)

print(numbers.count(10))
Output:
3
Tuple index()

The index() method returns the index of the first occurrence of a value.

fruits = ("Apple", "Banana", "Mango")

print(fruits.index("Mango"))
Output:
2
List vs Tuple
List Tuple
Uses [ ] Uses ( )
Mutable Immutable
Has many modification methods Has fewer methods
Suitable for changeable collections Suitable for fixed collections
When Should You Use a Tuple?

Tuples are useful when the collection should not be modified after creation.

For example:

coordinates = (25.5941, 85.1376)

rgb = (255, 255, 255)

student_id = ("ST101", "Rahul")

These values can be represented as fixed groups of related data.

Key Points
  • A tuple is an ordered collection.
  • Tuples are usually created using parentheses ( ).
  • Tuples are immutable.
  • Tuples allow duplicate values.
  • Tuples support indexing and slicing.
  • Negative indexing is supported.
  • Tuples can contain different data types.
  • Tuples can contain other tuples.
  • Tuples support unpacking.
  • The main tuple methods are count() and index().
  • Use tuples when data should remain unchanged.

🧠 Quick Quiz

Question: Which statement about Python tuples is correct?