Tuples in Python Class 11 Important NCERT Notes

Share with others

Tuples in Python Class 11 Notes

Tuples in Python
Tuples in Python

Tuples :

A tuple is an ordered sequence of elements of different data types, such as integer, float, string or list. Elements of a tuple are enclosed in parenthesis(round brackets) and are separated by commas. for example :

>>> a = (1, ‘a’, 7, 6.5) # a is the tuple of mixed data type

>>> b = (2, 4, 6, 8, 10) # b is a tuple of only integers

>>> c = (“English”, “Hindi”, “Math”, “Science”) # c is a tuple of only string

>>> d = (2, 4, 7, [4, 5, 6]) # d is a tuple with list as an element

If there is only a single element in a tuple then the element should be followed by a comma, otherwise it will be treated as integer instead of tuple. for example

Incorrect way of assigning a single elementCorrect way of assigning a single element
>>> a = (2)

Verification :

>>> type(a)
<class ‘int’>
>>> a = (2,)

Verification :

>>> type(a)
<class ‘tuple’>
Tuples in Python

NOTE : A sequence without parentheses is treated as tuple by default

>>> a = 9, 8, 7, 6, 5

>>> type(a)

<class ‘tuple’>

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. for example

>>> a = (‘C’ , ‘ O’ , ‘M’ , ‘P’ , ‘U’ , ‘T’ , ‘E’ , ‘R’)

ELEMENTSCOMPUTER
POSITIVE INDEX VALUE01234567
NEGATIVE INDEX VALUE-8-7-6-5-4-3-2-1
Tuples in Python

>>> a[4] # Output is U (fifth element of tuple)

>>> a[7] # Output is R (Eighth element of tuple)

>>> a[-1] # Output is R (last element of tuple or first element from right)

>>> a[9] # Output is IndexError: tuple index out of range

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. for example :

>>> a = (‘C’ , ‘ O’ , ‘M’ , ‘P’ , ‘U’ , ‘T’ , ‘E’ , ‘R’)

>>> a[2] = ‘S’

TypeError: ‘tuple’ object does not support item assignment.

Difference between List and Tuple :

LISTTUPLE
It is mutable data type.It is an immutable data type.
Elements are enclosed in Square brackets ie [ ] Elements are enclosed in Parenthesis. ie ( )
Iterating through a list is slower as compared to a tupleIterating through a tuple is faster as compared to a list
Tuples in Python

Tuple Operations : Various operations of tuple are done by various operators. like

Operation
Name
DescriptionExample
Concatenation (+)Joining of two or more tuples is called
concatenation


Python allows us to join tuples
using concatenation operator (+).


>>>t1 = (1, 2, 3)
>>>t2 = (8, 9, 11)
>>>t1+t2

OUTPUT :
(1, 2, 3, 8, 9, 11)

(#concatenates two tuples)

>>>t1 = (1, 2, 3)
>>>t2 = (8, 9, 11)
>>>t3 = t1+t2

(#Created new tuple)

OUTPUT :
(1, 2, 3, 8, 9, 11)


Concatenation operator can also be
used for extending an existing tuple.

>>>t1 = (1, 2, 3)
>>>t1 = t1 + (7,8,9)
>>>t1


OUTPUT :
(1, 2, 3, 7, 8, 9)
Repetition (*)It is used to repeat elements of a tuple.
Repetition operation is denoted by Symbol (*).
>>>h1 = (‘H’ , ‘M’)
>>>h1 * 3
(‘H’ , ‘M’, ‘H’ , ‘M’, ‘H’ , ‘M’)
MembershipThe ‘in’ operator checks the presence of
element in tuple. If the element is
present it returns True, else it returns False.

The not in operator returns True if the element is
not present in the tuple, else it returns False.
>>>h1 = (‘H’ , ‘M’)
>>>’H’ in h1
True


>>>’m’ not in h1
True
SlicingIt is used to extract one or more elements
from the tuple. Like string and list, slicing
can be applied to tuples also.
>>>t1 = (1, 2, 3, 7, 8, 9)
>>>t1[2:4]
(3, 7)

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

>>>t1[ : 5]
(10, 20, 30, 40, 50)


>>>t1[: : -1]
(80, 70, 60, 50, 40, 30, 20, 10)
Tuples in Python

Tuple Methods and Built-in Functions :

Method NameDescriptionExample
len( )This method returns the length of tuple
or the number of elements in the tuple.
>>>t1 =(10, 20, 30, 40, 50, 60, 70, 80)
>>>len(t1)
8
tuple( )This function creates an empty tuple or
creates a tuple if a sequence is passed
as argument
>>>t1 = tuple()
>>>type(t1)
<class ‘tuple’>

>>>t1 = tuple(‘python’) #string
>>>t1
(‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’)

>>>t1 = tuple([1,2,3]) #list
>>>t2
(1, 2, 3)

>>>t1 = tuple(range(7))
>>>t1
(0, 1, 2, 3, 4, 5, 6)
count( )This function returns the frequency
of an element in the tuple.
>>>t1=tuple(“tuples in python”)
>>>t1.count(‘p’)
2
index( )This function returns the index of the first
occurrence of the element in the given tuple.
>>>t1=tuple(“tuples in python”)
>>>t1.index(‘n’)
8

>>>t1=tuple(“tuples in python”)
>>>t1.index(‘f’)
ValueError: tuple.index(x): x not in tuple
sorted( )This element takes tuple as an argument and returns a sorted list. This function does not make any change in the original tuple.>>>t1 = (‘t’, ‘u’, ‘p’, ‘l’, ‘e’, ‘s’)
>>>sorted(t1)
[‘e’, ‘l’, ‘p’, ‘s’, ‘t’, ‘u’]

>>>t1
(‘t’, ‘u’, ‘p’, ‘l’, ‘e’, ‘s’)
min()This function returns minimum or smallest
element of the tuple.
>>>t1 = (3, 8, 4, 10, 1)
>>>min(t1)
1

>>
>t1 = (‘t’, ‘u’, ‘p’, ‘l’, ‘e’, ‘s’)
>>>min(t1)
‘e’
max( )This function returns maximum or largest
element of the tuple.
>>>t1 = (3, 8, 4, 10, 1)
>>>max(t1)
10


>>>t1 = (‘t’, ‘u’, ‘p’, ‘l’, ‘e’, ‘s’)
>>>max(t1)
‘u’
sum( )This function returns sum of the elements of the
tuple
>>>t1 = (3, 8, 4, 10, 1)
>>>sum(t1)
26
Tuples in Python

Tuple Assignment :

It allows a tuple of variables on the left side of the assignment operator to be assigned respective values from a tuple on the right side. The number of variables on the left should be same as the number of elements in the tuple. for example

>>>(n1,n2) = (5,9)

>>>print(n1)

5

print(n2)

9

>>>(a,b,c,d) = (5,6,8) #values on left side and right side are not equal
ValueError: not enough values to unpack

Nested Tuples :

A tuple inside another tuple is called a nested tuple. In nested tuples we can access the elements in the same way of nested list. for example

>>>t1 = ((“Amit”, 90), (“Sumit”, 75), (“Ravi”, 80))
>>>t1[0]
(‘Amit’, 90)
>>>t1[1]
(‘Sumit’, 75)
>>>t1[1][1]
75


Tuples in Python Class 11 Notes

Practice Questions : Click for complete NCERT Solution

Q1. Consider the following tuples, tuple1 and tuple2: (from NCERT)
tuple1 = (23,1,45,67,45,9,55,45)
tuple2 = (100,200)


Find the output of the following statements:
i. print(tuple1.index(45))

ii. print(tuple1.count(45))

iii. print(tuple1 + tuple2)

iv. print(len(tuple2))

v. print(max(tuple1))

vi print(min(tuple1))

vii. print(sum(tuple2))

viii. print ( sorted ( tuple1 ) )
print(tuple1)

Q2. What advantages do tuples have over lists? (from NCERT)

Tuples in Python Class 11 Notes

Programming Questions (from NCERT)

Click for complete NCERT Solution

Q1. Write a program to read email IDs of n number of students and store them in a tuple. Create two new tuples, one to store only the usernames from the email IDs and second to store domain names from the email ids. Print all three tuples at the end of the
program. [Hint: You may use the function split()]

Q2. Write a program to input names of n students and store them in a tuple. Also, input a name from the user and find if this student is present in the tuple or not.

We can accomplish these by:
(a) writing a user defined function
(b) using the built-in function


Tuples in Python Class 11 Notes

Disclaimer : I tried to give you the correct “Tuples in Python Class 11 NCERT Notes” , but if you feel that there is/are mistakes in “Tuples in Python Class 11 NCERT Notes” given above, you can directly contact me at csiplearninghub@gmail.com. NCERT Book is used as reference to create above “Tuples in Python Class 11 NCERT Notes”

Tuples in Python Class 11 Notes


Class 12 Computer Science Sample Paper 2020-2021.

Class 12 Computer Science Sample Paper Marking Scheme

Class 12 Computer Science Chapter wise MCQ


Share with others

Leave a Reply

%d bloggers like this: