0% found this document useful (0 votes)
6 views10 pages

Python Tuples: Definition and Operations

The document provides comprehensive notes on tuples in Python, covering their definition, properties, and various operations such as concatenation, repetition, membership, and slicing. It also details built-in functions for tuple manipulation, tuple assignment, and nested tuples, along with example programs for finding minimum, maximum, mean, and counting frequencies. Overall, it serves as a guide for understanding and using tuples effectively in Python programming.

Uploaded by

muthulakshmi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views10 pages

Python Tuples: Definition and Operations

The document provides comprehensive notes on tuples in Python, covering their definition, properties, and various operations such as concatenation, repetition, membership, and slicing. It also details built-in functions for tuple manipulation, tuple assignment, and nested tuples, along with example programs for finding minimum, maximum, mean, and counting frequencies. Overall, it serves as a guide for understanding and using tuples effectively in Python programming.

Uploaded by

muthulakshmi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Tuples in Python Class 11 Notes

Tuples: introduction, indexing, tuple operations (concatenation, repetition, membership and slicing);
built-in functions/methods – len(), tuple(), count(), index(), sorted(), min(), max(), sum(); tuple
assignment, nested tuple; suggested programs: finding the minimum, maximum, mean of values stored
in a tuple; linear search on a tuple of numbers, counting the frequency of elements in a tuple.

Introduction to Tuples
 A Python tuple is a collection of different data types, such as integers,
floats, strings or lists.
 A tuple is an immutable collection of elements.
 A tuple is a sequence of elements enclosed within parentheses ().

Accessing Element in a Tuple:


Individual items in a tuple can be accessed using indexes.
 An index is a unique number given to each item in a tuple.
 Indexing always starts from 0
 Indexes are written within square brackets [ ] after the tuple name.
Example
Example Code Explanation Output
1 tuple1 = Index 0 gives the first 2
(2,4,6,8,10,12) element.
print(tuple1[0])
2 tuple1 = Index 3 refers to the 8
(2,4,6,8,10,12) fourth element.
print(tuple1[3])
3 tuple1 = Index 15 does not exist IndexError: tuple
(2,4,6,8,10,12) (valid indexes are 0–5). index out of range
print(tuple1[15])
4 tuple1 = 1 + 4 = 5, so it accesses 12
(2,4,6,8,10,12) index 5.
print(tuple1[1+4])
5 tuple1 = -1 refers to the last 12
(2,4,6,8,10,12) element of the tuple.
print(tuple1[-1])

Tuple is Immutable
 Tuple is an immutable data type.
 It means that the elements of a tuple cannot be changed after it has
been created.
 An attempt to do this would lead to an error.

Tuple Operations
 Concatenation
 Repetition
 Membership
 Slicing
1. Concatenation (+)
 Python allows us to join tuples using the + operator.

 The result is a new tuple containing elements of both tuples.

Example:
tuple1 = (1, 3, 5, 7, 9)
tuple2 = (2, 4, 6, 8, 10)
print(tuple1 + tuple2)
Output:
(1, 3, 5, 7, 9, 2, 4, 6, 8, 10)

2. Repetition (*)
 The * operator is used to repeat the elements of a tuple

multiple time.
Example:
tuple1 = ('Hello', 'World')
print(tuple1 * 3)
Output:
('Hello', 'World', 'Hello', 'World', 'Hello', 'World')
3. Membership (in, not in)
 in checks if an element is present in a tuple and returns True if

found, otherwise False.


 not in checks if an element is not present in the tuple.

Example 1:
tuple1 = ('Red', 'Green', 'Blue')
print('Green' in tuple1)
Output:
True
Example 2:
tuple1 = ('Red', 'Green', 'Blue')
print('Green' not in tuple1)
Output:
False
4. Slicing
 Like strings and lists, slicing can be applied to tuples to extract a

subset of elements.
 Syntax: tuple[start:end] (end index not included)

Example:
tuple1 = (10, 20, 30, 40, 50, 60, 70, 80)
print(tuple1[2:7])
Output:
(30, 40, 50, 60, 70)

Traversing a Tuple:
Traversing a tuple means accessing each element of the tuple one by
one.
We can use for loops or while loops to traverse a tuple.

1. Using a for loop


Example:
tuple1 = (10, 20, 30, 40, 50)

for item in tuple1:


print(item)
Output:
10
20
30
40
50
Explanation:
The for loop automatically goes through each element in the tuple
one by one.

2. Using a while loop


Example:
tuple1 = (10, 20, 30, 40, 50)
i=0

while i < len(tuple1):


print(tuple1[i])
i += 1
Output:
10 40
20 50
30
Tuple Methods and Built-in Functions
Python provides several built-in functions to work with tuples.

1. len()
 Returns the length or number of elements in a tuple.

Example:
tuple1 = (10, 20, 30, 40, 50)
print(len(tuple1))
Output:
5

2. tuple()
 Creates a tuple from a sequence or an empty tuple if no

argument is passed.
Example 1 – Empty tuple
tuple1 = tuple()
print(tuple1)
Output:
()
Example 2 – From a string
tuple1 = tuple('aeiou')
print(tuple1)
Output:
('a', 'e', 'i', 'o', 'u')

3. count()
 Returns the number of times a specified element appears in

the tuple.
Example:
tuple1 = (10, 20, 30, 10, 40, 10, 50)
print([Link](10))
Output:
3

4. index()
 Returns the index of the first occurrence of an element in the

tuple.
Example:
tuple1 = (10, 20, 30, 10, 40, 10, 50)
print([Link](30))
Output:
2

5. sorted()
 Returns a new sorted list of the tuple elements.

 Note: Does not change the original tuple.

Example:
tuple1 = ("Rama", "Heena", "Raj", "Mohsin", "Aditya")
print(sorted(tuple1))
Output:
['Aditya', 'Heena', 'Mohsin', 'Raj', 'Rama']

6. min()
 Returns the smallest element in the tuple.

Example:
tuple1 = (19, 12, 56, 18, 9, 87, 34)
print(min(tuple1))
Output:9

7. max()
 Returns the largest element in the tuple.

Example:
tuple1 = (19, 12, 56, 18, 9, 87, 34)
print(max(tuple1))
Output:87

8. sum()
 Returns the sum of all elements in the tuple.

Example:
tuple1 = (19, 12, 56, 18, 9, 87, 34)
print(sum(tuple1))
Output:
235

Tuple Assignment
Python allows assigning values from a tuple to variables directly.
 The number of variables on the left-hand side must match the

number of elements in the tuple on the right-hand side.

Example 1
(num1, num2) = (10, 20)
print(num1)
print(num2)
Output:
10
20

Example 2
record = ("Pooja", 40, "CS")
(name, rollNo, subject) = record
print(name)
print(rollNo)
print(subject)
Output:
Pooja
40 CS
Explanation:
 name gets "Pooja", rollNo gets 40, and subject gets "CS"

automatically.

Nested Tuples
A nested tuple is a tuple that contains another tuple inside it.
 Useful for storing structured information, like student records

with multiple attributes.

Example

Output:
S_No Roll_No Name Marks
1 101 Aman 98
2 102 Geet 95
3 103 Sahil 87
4 104 Pawan 79
Explanation:
 Each element of st is a tuple representing a student.

 st[i][0] → Roll No, st[i][1] → Name, st[i][2] → Marks.

 This allows easy access to individual attributes of each student.


Program:
1. Finding Minimum, Maximum, and Mean of Tuple Values
Program
numbers = (10, 25, 7, 30, 15)
print(min(numbers))
print(max(numbers))
mean = sum(numbers) / len(numbers)
print(mean)
Output:
7
30
17.4

2. Linear Search in a Tuple


Linear search checks each element one by one to find a target
value.
Program:
numbers = (10, 25, 7, 30, 15)
target = 30

found = False
for i in range(len(numbers)):
if numbers[i] == target:
found = True
print("Element found at index:", i)
break

if not found:
print("Element not found")

Output:
Element found at index: 3
3. Counting Frequency of Elements in a Tuple
Program:
numbers = (10, 25, 7, 10, 15, 25, 10)
freq = [Link](10)
print("Frequency of 10 is:", freq)
Output:
Frequency of 10 is: 3

You might also like