Lessons
Courses

Tuple in Python [ English ]

1. Introduction

A tuple in Python is a collection of items stored in a single variable, similar to a list. However, the key difference is:

Tuples are immutable (cannot be changed after creation)


2. Characteristics of Tuple

  • Ordered (elements have fixed positions)
  • Immutable (cannot modify elements)
  • Allows duplicate values
  • Can store different data types

3. Creating a Tuple

Syntax

tuple_name = (elements)

Example

numbers = (1, 2, 3)names = ("Amit", "Rahul", "Priya")mixed = (10, "Python", 3.5)

Single Element Tuple

t = (5,)

👉 Comma is required, otherwise it is not considered a tuple.


4. Accessing Elements

Indexing

numbers = (10, 20, 30) print(numbers[0])print(numbers[1])

Output

1020

Negative Indexing

print(numbers[-1])

Output

30

5. Slicing a Tuple

numbers = (1, 2, 3, 4, 5) print(numbers[1:4])

Output

(2, 3, 4)

6. Immutability of Tuple

numbers = (1, 2, 3) numbers[1] = 10 # Error

Explanation

  • Tuples cannot be modified after creation.

7. Tuple Operations

Concatenation

a = (1, 2)b = (3, 4) print(a + b)

Repetition

print((1, 2) * 2)

Membership

print(2 in (1, 2, 3))

8. Looping Through a Tuple

numbers = (1, 2, 3) for n in numbers: print(n)

9. Tuple Packing and Unpacking

Packing

t = 1, 2, 3

Unpacking

a, b, c = (1, 2, 3) print(a, b, c)

10. Nested Tuple

t = ((1, 2), (3, 4)) print(t[0][1])

Output

2

11. Built-in Functions for Tuple

FunctionDescription
len()Returns number of elements
max()Returns maximum value
min()Returns minimum value
sum()Returns sum of elements

12. Difference Between List and Tuple

FeatureListTuple
MutabilityMutableImmutable
Syntax[ ]( )
PerformanceSlowerFaster
UsageChangeable dataFixed data

13. Advantages of Tuple

  • Faster than lists
  • Protects data (immutable)
  • Can be used as dictionary keys
  • Uses less memory

14. Summary

  • A tuple is an ordered and immutable collection

  • Created using parentheses ( )

  • Supports:

    • indexing
    • slicing
    • iteration
  • Cannot be modified after creation

  • Useful for fixed and secure data