Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📦 Unit 2 – Strings, Lists, Tuples & Dictionaries  ·  Lesson 23

Tuples: Operations & Methods

What is a tuple?

A tuple is an ordered, immutable sequence of items written in round brackets ( ). It is like a list that is "locked" after creation — perfect for data that should not change, such as the days of the week, the date of birth or the GPS location of your school.

days →Mon0-5Tue1-4Wed2-3Thu3-2Fri4-1
A tuple has indexes like a list, but its items cannot be replaced

Creating tuples

Python
t = (10, 20, 30, 40)
print(t[1], t[-1], t[1:3], len(t), type(t))
one = (5,)            # comma makes it a tuple
not_tuple = (5)       # just the number 5 in brackets
print(type(one), type(not_tuple))
p = 1, 2, 3           # brackets are optional (packing)
print(p, type(p))
20 40 (20, 30) 4 <class 'tuple'> <class 'tuple'> <class 'int'> (1, 2, 3) <class 'tuple'>

Tuples are immutable

Python
t = (1, 2)
t[0] = 9
TypeError: 'tuple' object does not support item assignment

You also cannot append, insert or remove. You can delete the whole tuple with del t, or make a new tuple using +. If a tuple contains a mutable item like a list, that inner list can still change:

Python
t = ([1, 2], 3)
t[0].append(9)       # the list inside can still change
print(t)
([1, 2, 9], 3)

Operations, methods and functions

Python
t = (10, 20, 30, 40)
print((1, 2) + (3, 4), ('ab',) * 3, 20 in t)
x = (3, 7, 1, 7, 9)
print(x.count(7), x.index(9))
print(len(x), max(x), min(x), sum(x))
print(sorted(x))                      # returns a LIST
print(tuple('hello'), tuple([1, 2]))
(1, 2, 3, 4) ('ab', 'ab', 'ab') True 2 4 5 9 1 27 [1, 3, 7, 7, 9] ('h', 'e', 'l', 'l', 'o') (1, 2)
FeatureExample
Indexing / slicingt[0], t[-1], t[1:3], t[::-1]
Concatenation / repetitiont1 + t2, t * 3
Membershipx in t
Methodst.count(x), t.index(x)
Functionslen, max, min, sum, sorted (gives list), tuple()

Tuple assignment (packing and unpacking)

Packing puts many values into one tuple (p = 1, 2, 3). Unpacking takes them out into separate variables. The number of variables must equal the number of items, otherwise Python raises a ValueError ("too many values to unpack").

Python
a, b, c = (4, 5, 6)                 # unpacking
print(a, b, c)
name, roll, marks = 'Ishan', 12, 88.5
print(name, roll, marks)
x, y = 10, 20
x, y = y, x                         # swap = tuple packing + unpacking
print(x, y)
4 5 6 Ishan 12 88.5 20 10

Nested tuples

Python
students = (('Riya', 91), ('Aman', 85), ('Zoya', 78))
print(students[1][0], students[0][1])
for name, m in students:
    print(f'{name:<6}{m}')
Aman 91 Riya 91 Aman 85 Zoya 78

Tuple vs list

List [ ]

  • Mutable – can add, remove, change
  • Many methods (append, sort …)
  • Use for data that changes: cart items, marks entry

Tuple ( )

  • Immutable – fixed after creation
  • Only count() and index()
  • Faster, safer; can be a dictionary key
  • Use for fixed data: months, (x, y) points
Choose by whether data will change

Solved programs

1. Input numbers into a tuple and find statistics

Python
n = int(input('How many numbers? '))
t = ()
for i in range(n):
    t = t + (int(input('Number: ')),)    # build a new tuple each time
print('Tuple:', t)
print('Max:', max(t), '| Min:', min(t), '| Mean:', sum(t) / len(t))
How many numbers? 3 Number: 14 Number: 9 Number: 25 Tuple: (14, 9, 25) Max: 25 | Min: 9 | Mean: 16.0

2. Function returning a tuple

Python
def circle(r):
    area = 3.14 * r * r
    circumference = 2 * 3.14 * r
    return area, circumference       # a tuple

result = circle(5)
print(result, type(result))
a, c = circle(5)
print('Area =', a, '| Circumference =', c)
(78.5, 31.400000000000002) <class 'tuple'> Area = 78.5 | Circumference = 31.400000000000002

The long decimal comes from float approximation; use round(c, 2) to show 31.4.

Summary & practice

  • Tuple = ordered, immutable, in ( ); single item needs a comma.
  • Supports indexing, slicing, +, *, in, len, max, min, sum.
  • Methods: count(), index(). sorted() returns a list.
  • Packing/unpacking makes swapping and multiple returns easy.
✅ Practice

Predict: t = (2, 4, 6, 8); print(t[1:3] * 2, t.index(6), 8 in t). Write a program to find the second largest element of a tuple and to count how many elements are even.

Answer: (4, 6, 4, 6) 2 True

Frequently Asked Questions

How do I create a tuple with only one element?
Put a comma after the element: t = (5,). Without the comma, (5) is just the integer 5.
Why use a tuple instead of a list?
Tuples cannot be changed, so they protect fixed data such as days of the week or coordinates, are slightly faster, and can be used as dictionary keys.
Which methods does a tuple have?
Only two: count(x) and index(x). Functions like len(), max(), min(), sum() and sorted() also work on tuples.
← Class 11 Computer Science
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topicDictionaries: Keys, Values & Methods Your progress: 23 / 29 (79%) · Lesson 24 / 29 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free