|

Tuples in Python Notes – Class 11 CS (083) | CBSE Aligned

Score high with Tuples in Python Notes of Class 11 CS. Fully aligned with the latest CBSE syllabus with Clear, pointwise quick revision and practical examples!

These notes not only provide excellent concept clarity but also support practical learning through examples and programming-based explanations. Fully aligned with the latest CBSE curriculum, they cover all the essential topics you need for theory, revision, and practical preparation.

Whether you’re revising for exams, strengthening your understanding, or practicing programs, these notes help you learn confidently and effectively without feeling overwhelmed.

Introduction to Tuples in Python

  • A tuple is an ordered collection of elements.
  • A tuple can store different data types such as integers, floats, strings, lists, or even another tuple.
  • Elements in a tuple are enclosed within parentheses ( ) and separated by commas.
  • Like lists and strings, tuple elements are accessed using index numbers starting from 0.
  • Tuples are generally used to store data that should not be changed frequently.
  • Tuples are immutable.

Examples of Tuples

# Tuple of integers
tuple1 = (1, 2, 3, 4, 5)

# Tuple of mixed data types
tuple2 = (“Rohan”, 16, 89.5, “Science”)

# Tuple containing a list
tuple3 = (10, 20, 30, [40, 50])

# Tuple containing another tuple
tuple4 = (“Cricket”, “Football”, (“Gold”, “Silver”))

Single Element Tuple

  • If a tuple contains only one element, a comma, must be placed after the element.
  • Parentheses are optional in Python tuples.

#Correct way – single element tuple

value2 = (25,)
print(value2)
print(type(value2))

#Tuple without parentheses

value3 = 10, 20, 30
print(value3)
print(type(value3))

We generally use list to store elements of the same data types whereas we use tuples to store elements of different data types.

Accessing Elements in a Tuple

  • Elements of a tuple can be accessed using indexing.
  • Index numbers start from 0.
  • Negative indexing is used to access elements from the end.

# Creating a tuple
colors = (“Red”, “Green”, “Blue”, “Yellow”, “Black”)

# Accessing first element
print(colors[0])

# Accessing third element
print(colors[2])

# Index out of range
# print(colors[10])

# Using expression as index
print(colors[1 + 2])

# Accessing last element using negative index
print(colors[-1])

Output

Red
Blue
Yellow
Black

Tuple is Immutable

  • Tuples are immutable, which means their elements cannot be changed after creation.
  • Trying to modify a tuple element gives an error.
  • However, if a tuple contains a mutable object like a list, the list elements can be changed.

# Creating a tuple
student = (“Aman”, 15, “Science”, 85)

# Trying to change an element
# student[3] = 90

# Tuple containing a list
details = (“Books”, “Pens”, [10, 20])

# Modifying list element inside tuple
details[2][1] = 25

print(details)

Output

(‘Books’, ‘Pens’, [10, 25])

Tuple Operations – Concatenation

  • Tuples can be joined using the + operator.
  • This operation is called concatenation.
  • Concatenation creates a new tuple containing elements of both tuples.
  • The + operator can also be used to extend an existing tuple.

# Concatenating two tuples
fruits1 = (“Apple”, “Banana”, “Mango”)
fruits2 = (“Orange”, “Grapes”, “Papaya”)
result = fruits1 + fruits2
print(result)
# Extending an existing tuple
numbers = (1, 2, 3)
# Adding single element
numbers = numbers + (4,)
print(numbers)
# Adding multiple elements
numbers = numbers + (5, 6, 7)
print(numbers)

Output

(‘Apple’, ‘Banana’, ‘Mango’, ‘Orange’, ‘Grapes’, ‘Papaya’)
(1, 2, 3, 4)
(1, 2, 3, 4, 5, 6, 7)

Tuple Operations – Repetition

  • The * operator is used to repeat tuple elements.
  • The first operand must be a tuple and the second operand must be an integer.

# Repeating tuple elements
colors = (“Red”, “Blue”)
print(colors * 3)
# Tuple with single element
message = (“Welcome”,)
print(message * 4)

Output

(‘Red’, ‘Blue’, ‘Red’, ‘Blue’, ‘Red’, ‘Blue’)
(‘Welcome’, ‘Welcome’, ‘Welcome’, ‘Welcome’)

Tuple Operations – Membership

  • The in operator checks whether an element is present in a tuple.
  • The not in operator checks whether an element is absent from a tuple.

# Membership operators
subjects = (“Math”, “Science”, “English”)
print(“Science” in subjects)
print(“History” not in subjects)

Output

True
True

Slicing in Tuples

  • Slicing is used to access a group of elements from a tuple.
  • The syntax for slicing is:

tuple[start : stop : step]

  • start → starting index
  • stop → ending index (excluded)
  • step → difference between indexes

# Creating a tuple
numbers = (5, 10, 15, 20, 25, 30, 35, 40)
# Elements from index 2 to 5
print(numbers[2:6])
# Complete tuple
print(numbers[0:len(numbers)])
# Slice from beginning
print(numbers[:4])
# Slice till end
print(numbers[3:])
# Step size 2
print(numbers[0:len(numbers):2])
# Negative indexing
print(numbers[-5:-2])
# Tuple in reverse order
print(numbers[::-1])

Output

(15, 20, 25, 30)
(5, 10, 15, 20, 25, 30, 35, 40)
(5, 10, 15, 20)
(20, 25, 30, 35, 40)
(5, 15, 25, 35)
(20, 25, 30)
(40, 35, 30, 25, 20, 15, 10, 5)

Tuple Methods and Built-in Functions

Python provides several built-in functions and methods to work with tuples.

Function / MethodDescriptionExample
len()Returns the number of elements in a tuplenums = (10, 20, 30, 40) print(len(nums))
Output: 4
tuple()Creates an empty tuple or converts a sequence into a tupleletters = tuple(“Python”) print(letters) numbers = tuple([1, 2, 3]) print(numbers)
Output: (‘P’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’) (1, 2, 3)
count()Returns the number of times an element appears in a tuplevalues = (5, 10, 5, 20, 5) print(values.count(5))
Output: 3
index()Returns the index of the first occurrence of an elementcolors = (“Red”, “Blue”, “Green”) print(colors.index(“Blue”))
Output: 1
sorted()Returns a sorted list from tuple elementsnames = (“Riya”, “Aman”, “Kabir”) print(sorted(names))
Output: [‘Aman’, ‘Kabir’, ‘Riya’]
min()Returns the smallest element of the tuplemarks = (78, 45, 90, 67) print(min(marks))
Output: 45
max()Returns the largest element of the tuplemarks = (78, 45, 90, 67) print(max(marks))
Output: 90
sum()Returns the sum of numeric elements in a tuplemarks = (78, 45, 90, 67) print(sum(marks))
Output: 280

Tuple Assignment

  • Tuple assignment allows multiple variables to receive values from a tuple in a single statement.
  • The number of variables on the left side must be equal to the number of tuple elements on the right side.

Example 1: Assigning Tuple Values

# Assigning values to variables
(student, marks) = (“Riya”, 95)
print(student)
print(marks)

# Assigning values from another tuple
record = (“Aman”, 12, “Science”)
(name, roll_no, stream) = record

print(name)
print(roll_no)
print(stream)

Output

Riya
95
Aman
12
Science

Example 2: Error in Tuple Assignment

(a, b, c) = (10, 20)

Output

ValueError: not enough values to unpack

Example 3: Using Expressions in Tuple Assignment

(num1, num2) = (5 + 10, 15 + 5)

print(num1)
print(num2)

Output

15
20

Nested Tuples

  • A tuple inside another tuple is called a nested tuple.
  • Nested tuples are useful for storing related records together.

Program: Storing and Displaying Student Records

# Program to store and display student records using nested tuples

students = (
    (101, “Riya”, 92),
    (102, “Aman”, 88),
    (103, “Kabir”, 95),
    (104, “Neha”, 81)
)

print(“S_No\tRoll_No\tName\tMarks”)
for i in range(len(students)):
    print(i + 1, “\t”, students[i][0], “\t”, students[i][1], “\t”, students[i][2])

Output

S_No    Roll_No    Name    Marks
1       101        Riya    92
2       102        Aman    88
3       103        Kabir   95
4       104        Neha    81

Tuple Handling

Program 1: Swap Two Numbers Without Using Temporary Variable

# Program to swap two numbers

num1 = int(input(“Enter the first number: “))
num2 = int(input(“Enter the second number: “))

print(“\nNumbers before swapping:”)
print(“First Number:”, num1)
print(“Second Number:”, num2)

(num1, num2) = (num2, num1)

print(“\nNumbers after swapping:”)
print(“First Number:”, num1)
print(“Second Number:”, num2)

Output

Enter the first number: 5
Enter the second number: 10

Numbers before swapping:
First Number: 5
Second Number: 10

Numbers after swapping:
First Number: 10
Second Number: 5

Program 2: Compute Area and Circumference of a Circle

# Function to compute area and circumference of a circle

def circle(r):
    area = 3.14 * r * r
    circumference = 2 * 3.14 * r
       return (area, circumference)

radius = int(input(“Enter radius of circle: “))
area, circumference = circle(radius)
print(“Area of circle is:”, area)
print(“Circumference of circle is:”, circumference)

Output

Enter radius of circle: 5
Area of circle is: 78.5
Circumference of circle is: 31.4

Program 3: Find Minimum, Maximum and Mean of Values Stored in a Tuple

# Program to find minimum, maximum and mean of tuple elements

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

minimum = min(numbers)
maximum = max(numbers)
mean = sum(numbers) / len(numbers)

print(“Tuple elements are:”, numbers)
print(“Minimum value:”, minimum)
print(“Maximum value:”, maximum)
print(“Mean value:”, mean)

Output

Tuple elements are: (10, 20, 30, 40, 50)

Minimum value: 10
Maximum value: 50
Mean value: 30.0

Program 4: Linear Search on a Tuple

# Program for linear search on a tuple

numbers = (12, 45, 67, 23, 89, 45)
search = int(input(“Enter number to search: “))
found = False

for i in range(len(numbers)):
    if numbers[i] == search:
        print(“Element found at index:”, i)
        found = True
        break

if found == False:
    print(“Element not found”)

Output

Enter number to search: 67
Element found at index: 2

Program 5: Count Frequency of Elements in a Tuple

# Program to count frequency of an element in a tuple

numbers = (10, 20, 10, 30, 40, 10, 20)
element = int(input(“Enter element to count frequency: “))
frequency = numbers.count(element)
print(“Tuple elements are:”, numbers)
print(“Frequency of”, element, “is:”, frequency)

Output

Enter element to count frequency: 10
Tuple elements are: (10, 20, 10, 30, 40, 10, 20)
Frequency of 10 is: 3

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *