Tuples in Python Class 11 Notes

Teachers and Examiners (CBSESkillEduction) collaborated to create the Tuples in Python Class 11 Notes. All the important Information are taken from the NCERT Textbook Computer Science (083) class 11.

Tuples in Python Class 11 Notes

Introduction to Tuples

An ordered collection of components of various data kinds, such as integer, float, string, list, is known as a tuple. A tuple’s components are denoted by parenthesis (round brackets) and commas. A tuple’s elements can be retrieved using index values beginning at 0 just like list and string members can.

Example 1 –
>>> tuple1 = (1,2,3,4,5)
>>> tuple1
(1, 2, 3, 4, 5)

Example 2 –
>>> tuple2 =(‘Economics’,87,’Accountancy’,89.6)
>>> tuple2
(‘Economics’, 87, ‘Accountancy’, 89.6)

Example 3 –
>>> tuple3 = (10,20,30,[40,50])
>>> tuple3
(10, 20, 30, [40, 50])

Example 4 –
>>> tuple4 = (1,2,3,4,5,(10,20))
>>> tuple4
(1, 2, 3, 4, 5, (10, 20))

Tuples in Python Class 11 Notes

Accessing Elements in a Tuple

Elements of a tuple can be accessed in the same way as a list or string using indexing and slicing.

Example 1 –
>>> tuple1 = (2,4,6,8,10,12)
>>> tuple1[0]
2

Example 2 –
>>> tuple1 = (2,4,6,8,10,12)
>>> tuple1[3]
8

Example 3 –
>>> tuple1 = (2,4,6,8,10,12)
>>> tuple1[15]
IndexError: tuple index out of range

Example 4 –
>>> tuple1 = (2,4,6,8,10,12)
>>> tuple1[1+4]
12

Example 5 –
>>> tuple1 = (2,4,6,8,10,12)
>>> tuple1[-1]
12

Tuples in Python Class 11 Notes

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.

Example –
>>> tuple1 = (1,2,3,4,5)
>>> tuple1[4] = 10
TypeError: ‘tuple’ object does not support item assignment

Tuple Operations

Concatenation

Python allows us to join tuples using concatenation operator depicted by symbol +.

Example –
>>> tuple1 = (1,3,5,7,9)
>>> tuple2 = (2,4,6,8,10)
>>> tuple1 + tuple2
(1, 3, 5, 7, 9, 2, 4, 6, 8, 10)

Repetition

Repetition operation is depicted by the symbol *. It is used to repeat elements of a tuple.

Example –
>>> tuple1 = (‘Hello’,’World’)
>>> tuple1 * 3
(‘Hello’, ‘World’, ‘Hello’, ‘World’, ‘Hello’, ‘World’)

Tuples in Python Class 11 Notes

Membership

The in operator checks if the element is present in the tuple and returns True, else it returns False.

Example 1 –
>>> tuple1 = (‘Red’,’Green’,’Blue’)
>>> ‘Green’ in tuple1
True

Example 2 –
>>> tuple1 = (‘Red’,’Green’,’Blue’)
>>> ‘Green’ not in tuple1
False

Slicing

Like string and list, slicing can be applied to tuples also.

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

Tuples in Python Class 11 Notes

Tuple Methods and Built-in Functions

Python provides many functions to work on tuples.

len()

Returns the length or the number of elements of the tuple passed as the argument.

Example –
>>> tuple1 = (10,20,30,40,50)
>>> len(tuple1)
5

tuple()

Creates an empty tuple if no argument is passed, if a sequence is passed as argument.

Example 1 –
>>> tuple1 = tuple()
>>> tuple1
( )

Example 2 –
>>> tuple1 = tuple(‘aeiou’)
>>> tuple1
(‘a’, ‘e’, ‘i’, ‘o’, ‘u’)

count()

Returns the number of times the given element appears in the tuple

Example –
>>> tuple1 = (10,20,30,10,40,10,50)
>>> tuple1.count(10)
3
>>> tuple1.count(90)
0

index()

Returns the index of the first occurrence of the element in the given tuple.

Example –
>>> tuple1 = (10,20,30,40,50)
>>> tuple1.index(30)
2
>>> tuple1.index(90)
ValueError: tuple.index(x): x not in tuple

sorted()

Takes elements in the tuple and returns a new sorted list. It should be noted that, sorted() does not make any change to the original tuple.

Example –
>>> tuple1 = (“Rama”,”Heena”,”Raj”, “Mohsin”,”Aditya”)
>>> sorted(tuple1)
[‘Aditya’, ‘Heena’, ‘Mohsin’, ‘Raj’, ‘Rama’]

min()

Returns minimum or smallest element of the tuple.

Example –
>>> tuple1 = (19,12,56,18,9,87,34)
>>> min(tuple1)
9

max()

Returns maximum or largest element of the tuple

Example –
>>> tuple1 = (19,12,56,18,9,87,34)
>>> max(tuple1)
87

sum()

Returns sum of the elements of the tuple.

Example –
>>> tuple1 = (19,12,56,18,9,87,34)
>>> sum(tuple1)
235

Tuple Assignment

Python has a useful feature called assignment of tuples. It enables the assignment of appropriate values to a tuple of variables on the left side of the assignment operator from a tuple on the right side. The left-hand side variables should have the same amount of components as the tuple.

Example 1 –
>>> (num1,num2) = (10,20)
>>> print(num1)
10
>>> print(num2)
20

Example 2 –
>>> record = ( “Pooja”,40,”CS”)
>>> (name,rollNo,subject) = record
>>> name
‘Pooja’
>>> rollNo
40
>>> subject
‘CS’

Nested Tuples

A nested tuple is one that contains another tuple inside of it. Students’ names, roll numbers, and grades (in percentage) are saved in a tuple. We can create a nested tuple to contain the information of many of these students.

Example –
st=((101,”Aman”,98),(102,”Geet”,95),(103,”Sahil”,87),(104,”Pawan”,79))
print(“S_No”,” Roll_No”,” Name”,” Marks”)
for i in range(0,len(st)):
     print((i+1),’\t’,st[i][0],’\t’,st[i][1],’\t’,st[i][2])

Output:
S_No Roll_No Name Marks
1 101 Aman 98
2 102 Geet 95
3 103 Sahil 87
4 104 Pawan 79

Computer Science Class 11 Notes
Computer Science Class 11 MCQ
Computer Science Class 11 NCERT Solutions

Computer

error: Content is protected !!