Humanities/Arts Exam  >  Humanities/Arts Notes  >  Computer Science for Class 11  >  NCERT Textbook: Tuples and Dictionaries

NCERT Textbook: Tuples and Dictionaries | Computer Science for Class 11 - Humanities/Arts PDF Download

Download, print and study this document offline
Please wait while the PDF view is loading
 Page 1


10.1 Introduct Ion to t uples A tuple is an ordered sequence of elements of different 
data types, such as integer, float, string, list or even a 
tuple. Elements of a tuple are enclosed in parenthesis 
(round brackets) and are separated by commas. Like list 
and string, elements of a tuple can be accessed using 
index values, starting from 0. 
“Computers are to computing 
as instruments are to music. 
Software is the score whose 
interpretations amplifies our 
reach and lifts our spirits. 
Leonardo da Vinci called music 
the shaping of the invisible, and 
his phrase is even more apt as a 
description of software.”
– A Kay
Chapter 10
Tuples and Dictionaries
In this chapter
 » Introduction to 
Tuples 
 » Tuple Operations
 » Tuple Methods and 
Built-in Functions
 » Tuple Assignment
 » Nested Tuples
 » Tuple Handling
 » Introduction to 
Dictionaries
 » Dictionaries are 
Mutable
 » Dictionary 
Operations
 » Traversing a 
Dictionary
 » Dictionary Methods 
and Built-in 
Functions
 » Manipulating 
Dictionaries
Example 10.1 
#tuple1 is the tuple of integers
>>> tuple1 = (1,2,3,4,5)
>>> tuple1
(1, 2, 3, 4, 5)
#tuple2 is the tuple of mixed data types
>>> tuple2 =('Economics',87,'Accountancy',89.6)
>>> tuple2
('Economics', 87, 'Accountancy', 89.6)
#tuple3 is the tuple with list as an element
>>> tuple3 = (10,20,30,[40,50])
>>> tuple3
(10, 20, 30, [40, 50])
#tuple4 is the tuple with tuple as an element
>>> tuple4 = (1,2,3,4,5,(10,20))
>>> tuple4
(1, 2, 3, 4, 5, (10, 20))
If there is only a single element in a tuple then the 
element should be followed by a comma. If we assign the 
value without comma it is treated as integer. It should 
be noted that a sequence without parenthesis is treated 
as tuple by default.
#incorrect way of assigning single element to 
#tuple
 #tuple5 is assigned a single element
>>> tuple5 = (20)
Ch 10.indd   207 08-Apr-19   12:28:09 PM
2024-25
Page 2


10.1 Introduct Ion to t uples A tuple is an ordered sequence of elements of different 
data types, such as integer, float, string, list or even a 
tuple. Elements of a tuple are enclosed in parenthesis 
(round brackets) and are separated by commas. Like list 
and string, elements of a tuple can be accessed using 
index values, starting from 0. 
“Computers are to computing 
as instruments are to music. 
Software is the score whose 
interpretations amplifies our 
reach and lifts our spirits. 
Leonardo da Vinci called music 
the shaping of the invisible, and 
his phrase is even more apt as a 
description of software.”
– A Kay
Chapter 10
Tuples and Dictionaries
In this chapter
 » Introduction to 
Tuples 
 » Tuple Operations
 » Tuple Methods and 
Built-in Functions
 » Tuple Assignment
 » Nested Tuples
 » Tuple Handling
 » Introduction to 
Dictionaries
 » Dictionaries are 
Mutable
 » Dictionary 
Operations
 » Traversing a 
Dictionary
 » Dictionary Methods 
and Built-in 
Functions
 » Manipulating 
Dictionaries
Example 10.1 
#tuple1 is the tuple of integers
>>> tuple1 = (1,2,3,4,5)
>>> tuple1
(1, 2, 3, 4, 5)
#tuple2 is the tuple of mixed data types
>>> tuple2 =('Economics',87,'Accountancy',89.6)
>>> tuple2
('Economics', 87, 'Accountancy', 89.6)
#tuple3 is the tuple with list as an element
>>> tuple3 = (10,20,30,[40,50])
>>> tuple3
(10, 20, 30, [40, 50])
#tuple4 is the tuple with tuple as an element
>>> tuple4 = (1,2,3,4,5,(10,20))
>>> tuple4
(1, 2, 3, 4, 5, (10, 20))
If there is only a single element in a tuple then the 
element should be followed by a comma. If we assign the 
value without comma it is treated as integer. It should 
be noted that a sequence without parenthesis is treated 
as tuple by default.
#incorrect way of assigning single element to 
#tuple
 #tuple5 is assigned a single element
>>> tuple5 = (20)
Ch 10.indd   207 08-Apr-19   12:28:09 PM
2024-25
Computer SCien Ce – Cla SS xi 208
>>> tuple5
20
>>>type(tuple5)  #tuple5 is not of type tuple
<class 'int'>    #it is treated as integer 
#Correct Way of assigning single element to 
#tuple
#tuple5 is assigned a single element
>>> tuple5 = (20,) #element followed by comma
>>> tuple5
(20,)
>>>type(tuple5)    #tuple5 is of type tuple
<class 'tuple'>
#a sequence without parentheses is treated as 
#tuple by default
>>> seq = 1,2,3     #comma separated elements
>>> type(seq)       #treated as tuple
<class 'tuple'>
>>> print(seq)      #seq is a tuple
(1, 2, 3)
10.1.1 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.
>>> tuple1 = (2,4,6,8,10,12)       
#initializes a tuple tuple1
#returns the first element of tuple1
>>> tuple1[0]                      
2
#returns fourth element of tuple1
>>> tuple1[3]                      
8
#returns error as index is out of range
>>> tuple1[15]                     
IndexError: tuple index out of range
#an expression resulting in an integer index
>>> tuple1[1+4]              
12
#returns first element from right
>>> tuple1[-1]                     
12
10.1.2 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. 
>>> tuple1 = (1,2,3,4,5)
We generally use list 
to store elements of 
the same data types 
whereas we use tuples 
to store elements of 
different data types.
Ch 10.indd   208 08-Apr-19   12:28:09 PM
2024-25
Page 3


10.1 Introduct Ion to t uples A tuple is an ordered sequence of elements of different 
data types, such as integer, float, string, list or even a 
tuple. Elements of a tuple are enclosed in parenthesis 
(round brackets) and are separated by commas. Like list 
and string, elements of a tuple can be accessed using 
index values, starting from 0. 
“Computers are to computing 
as instruments are to music. 
Software is the score whose 
interpretations amplifies our 
reach and lifts our spirits. 
Leonardo da Vinci called music 
the shaping of the invisible, and 
his phrase is even more apt as a 
description of software.”
– A Kay
Chapter 10
Tuples and Dictionaries
In this chapter
 » Introduction to 
Tuples 
 » Tuple Operations
 » Tuple Methods and 
Built-in Functions
 » Tuple Assignment
 » Nested Tuples
 » Tuple Handling
 » Introduction to 
Dictionaries
 » Dictionaries are 
Mutable
 » Dictionary 
Operations
 » Traversing a 
Dictionary
 » Dictionary Methods 
and Built-in 
Functions
 » Manipulating 
Dictionaries
Example 10.1 
#tuple1 is the tuple of integers
>>> tuple1 = (1,2,3,4,5)
>>> tuple1
(1, 2, 3, 4, 5)
#tuple2 is the tuple of mixed data types
>>> tuple2 =('Economics',87,'Accountancy',89.6)
>>> tuple2
('Economics', 87, 'Accountancy', 89.6)
#tuple3 is the tuple with list as an element
>>> tuple3 = (10,20,30,[40,50])
>>> tuple3
(10, 20, 30, [40, 50])
#tuple4 is the tuple with tuple as an element
>>> tuple4 = (1,2,3,4,5,(10,20))
>>> tuple4
(1, 2, 3, 4, 5, (10, 20))
If there is only a single element in a tuple then the 
element should be followed by a comma. If we assign the 
value without comma it is treated as integer. It should 
be noted that a sequence without parenthesis is treated 
as tuple by default.
#incorrect way of assigning single element to 
#tuple
 #tuple5 is assigned a single element
>>> tuple5 = (20)
Ch 10.indd   207 08-Apr-19   12:28:09 PM
2024-25
Computer SCien Ce – Cla SS xi 208
>>> tuple5
20
>>>type(tuple5)  #tuple5 is not of type tuple
<class 'int'>    #it is treated as integer 
#Correct Way of assigning single element to 
#tuple
#tuple5 is assigned a single element
>>> tuple5 = (20,) #element followed by comma
>>> tuple5
(20,)
>>>type(tuple5)    #tuple5 is of type tuple
<class 'tuple'>
#a sequence without parentheses is treated as 
#tuple by default
>>> seq = 1,2,3     #comma separated elements
>>> type(seq)       #treated as tuple
<class 'tuple'>
>>> print(seq)      #seq is a tuple
(1, 2, 3)
10.1.1 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.
>>> tuple1 = (2,4,6,8,10,12)       
#initializes a tuple tuple1
#returns the first element of tuple1
>>> tuple1[0]                      
2
#returns fourth element of tuple1
>>> tuple1[3]                      
8
#returns error as index is out of range
>>> tuple1[15]                     
IndexError: tuple index out of range
#an expression resulting in an integer index
>>> tuple1[1+4]              
12
#returns first element from right
>>> tuple1[-1]                     
12
10.1.2 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. 
>>> tuple1 = (1,2,3,4,5)
We generally use list 
to store elements of 
the same data types 
whereas we use tuples 
to store elements of 
different data types.
Ch 10.indd   208 08-Apr-19   12:28:09 PM
2024-25
Tuples and d ic Tionaries 209
>>> tuple1[4] = 10
TypeError: 'tuple' object does not support 
item assignment
However an element of a tuple may be of mutable type, 
e.g., a list.
#4th element of the tuple2 is a list
>>> tuple2 = (1,2,3,[8,9])    
#modify the list element of the tuple tuple2
>>> tuple2[3][1] = 10           
#modification is reflected in tuple2
>>> tuple2                       
(1, 2, 3, [8, 10])           
10.2 t uple o perat Ions 10.2.1 Concatenation
Python allows us to join tuples using concatenation 
operator depicted by symbol +. We can also create a new 
tuple which contains the result of this concatenation 
operation.
>>> tuple1 = (1,3,5,7,9)
>>> tuple2 = (2,4,6,8,10)
>>> tuple1 + tuple2                
#concatenates two tuples
(1, 3, 5, 7, 9, 2, 4, 6, 8, 10)
>>> tuple3 = ('Red','Green','Blue')
>>> tuple4 = ('Cyan', 'Magenta', 'Yellow' 
,'Black')
#tuple5 stores elements of tuple3 and tuple4
>>> tuple5 = tuple3 + tuple4
>>> tuple5
('Red','Green','Blue','Cyan','Magenta', 
'Yellow','Black')
Concatenation operator can also be used for 
extending an existing tuple. When we extend a tuple 
using concatenation a new tuple is created.
>>> tuple6 = (1,2,3,4,5)
#single element is appended to tuple6
>>> tuple6 = tuple6 + (6,)      
>>> tuple6
(1, 2, 3, 4, 5, 6)
#more than one elements are appended
>>> tuple6 = tuple6 + (7,8,9)    
>>> tuple6
(1, 2, 3, 4, 5, 6, 7, 8, 9)
v List is mutable but 
tuple is immutable. 
So iterating through 
a tuple is faster as 
compared to a list. 
v If we have data that 
does not change 
then storing this 
data in a tuple will 
make sure that 
it is not changed 
accidentally.
Ch 10.indd   209 08-Apr-19   12:28:09 PM
2024-25
Page 4


10.1 Introduct Ion to t uples A tuple is an ordered sequence of elements of different 
data types, such as integer, float, string, list or even a 
tuple. Elements of a tuple are enclosed in parenthesis 
(round brackets) and are separated by commas. Like list 
and string, elements of a tuple can be accessed using 
index values, starting from 0. 
“Computers are to computing 
as instruments are to music. 
Software is the score whose 
interpretations amplifies our 
reach and lifts our spirits. 
Leonardo da Vinci called music 
the shaping of the invisible, and 
his phrase is even more apt as a 
description of software.”
– A Kay
Chapter 10
Tuples and Dictionaries
In this chapter
 » Introduction to 
Tuples 
 » Tuple Operations
 » Tuple Methods and 
Built-in Functions
 » Tuple Assignment
 » Nested Tuples
 » Tuple Handling
 » Introduction to 
Dictionaries
 » Dictionaries are 
Mutable
 » Dictionary 
Operations
 » Traversing a 
Dictionary
 » Dictionary Methods 
and Built-in 
Functions
 » Manipulating 
Dictionaries
Example 10.1 
#tuple1 is the tuple of integers
>>> tuple1 = (1,2,3,4,5)
>>> tuple1
(1, 2, 3, 4, 5)
#tuple2 is the tuple of mixed data types
>>> tuple2 =('Economics',87,'Accountancy',89.6)
>>> tuple2
('Economics', 87, 'Accountancy', 89.6)
#tuple3 is the tuple with list as an element
>>> tuple3 = (10,20,30,[40,50])
>>> tuple3
(10, 20, 30, [40, 50])
#tuple4 is the tuple with tuple as an element
>>> tuple4 = (1,2,3,4,5,(10,20))
>>> tuple4
(1, 2, 3, 4, 5, (10, 20))
If there is only a single element in a tuple then the 
element should be followed by a comma. If we assign the 
value without comma it is treated as integer. It should 
be noted that a sequence without parenthesis is treated 
as tuple by default.
#incorrect way of assigning single element to 
#tuple
 #tuple5 is assigned a single element
>>> tuple5 = (20)
Ch 10.indd   207 08-Apr-19   12:28:09 PM
2024-25
Computer SCien Ce – Cla SS xi 208
>>> tuple5
20
>>>type(tuple5)  #tuple5 is not of type tuple
<class 'int'>    #it is treated as integer 
#Correct Way of assigning single element to 
#tuple
#tuple5 is assigned a single element
>>> tuple5 = (20,) #element followed by comma
>>> tuple5
(20,)
>>>type(tuple5)    #tuple5 is of type tuple
<class 'tuple'>
#a sequence without parentheses is treated as 
#tuple by default
>>> seq = 1,2,3     #comma separated elements
>>> type(seq)       #treated as tuple
<class 'tuple'>
>>> print(seq)      #seq is a tuple
(1, 2, 3)
10.1.1 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.
>>> tuple1 = (2,4,6,8,10,12)       
#initializes a tuple tuple1
#returns the first element of tuple1
>>> tuple1[0]                      
2
#returns fourth element of tuple1
>>> tuple1[3]                      
8
#returns error as index is out of range
>>> tuple1[15]                     
IndexError: tuple index out of range
#an expression resulting in an integer index
>>> tuple1[1+4]              
12
#returns first element from right
>>> tuple1[-1]                     
12
10.1.2 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. 
>>> tuple1 = (1,2,3,4,5)
We generally use list 
to store elements of 
the same data types 
whereas we use tuples 
to store elements of 
different data types.
Ch 10.indd   208 08-Apr-19   12:28:09 PM
2024-25
Tuples and d ic Tionaries 209
>>> tuple1[4] = 10
TypeError: 'tuple' object does not support 
item assignment
However an element of a tuple may be of mutable type, 
e.g., a list.
#4th element of the tuple2 is a list
>>> tuple2 = (1,2,3,[8,9])    
#modify the list element of the tuple tuple2
>>> tuple2[3][1] = 10           
#modification is reflected in tuple2
>>> tuple2                       
(1, 2, 3, [8, 10])           
10.2 t uple o perat Ions 10.2.1 Concatenation
Python allows us to join tuples using concatenation 
operator depicted by symbol +. We can also create a new 
tuple which contains the result of this concatenation 
operation.
>>> tuple1 = (1,3,5,7,9)
>>> tuple2 = (2,4,6,8,10)
>>> tuple1 + tuple2                
#concatenates two tuples
(1, 3, 5, 7, 9, 2, 4, 6, 8, 10)
>>> tuple3 = ('Red','Green','Blue')
>>> tuple4 = ('Cyan', 'Magenta', 'Yellow' 
,'Black')
#tuple5 stores elements of tuple3 and tuple4
>>> tuple5 = tuple3 + tuple4
>>> tuple5
('Red','Green','Blue','Cyan','Magenta', 
'Yellow','Black')
Concatenation operator can also be used for 
extending an existing tuple. When we extend a tuple 
using concatenation a new tuple is created.
>>> tuple6 = (1,2,3,4,5)
#single element is appended to tuple6
>>> tuple6 = tuple6 + (6,)      
>>> tuple6
(1, 2, 3, 4, 5, 6)
#more than one elements are appended
>>> tuple6 = tuple6 + (7,8,9)    
>>> tuple6
(1, 2, 3, 4, 5, 6, 7, 8, 9)
v List is mutable but 
tuple is immutable. 
So iterating through 
a tuple is faster as 
compared to a list. 
v If we have data that 
does not change 
then storing this 
data in a tuple will 
make sure that 
it is not changed 
accidentally.
Ch 10.indd   209 08-Apr-19   12:28:09 PM
2024-25
Computer SCien Ce – Cla SS xi 210
10.2.2 Repetition
Repetition operation is depicted by the symbol *. It is 
used to repeat elements of a tuple. We can repeat the 
tuple elements. The repetition operator requires the first 
operand to be a tuple and the second operand to be an 
integer only.
>>> tuple1 = ('Hello','World')
>>> tuple1 * 3
('Hello', 'World', 'Hello', 'World', 'Hello', 
'World')
#tuple with single element
>>> tuple2 = ("Hello",)    
>>> tuple2 * 4
('Hello', 'Hello', 'Hello', 'Hello')
10.2.3 Membership
The in operator checks if the element is present in the 
tuple and returns True, else it returns False. 
>>> tuple1 = ('Red','Green','Blue')
>>> 'Green' in tuple1
True
The not in operator returns True if the element is 
not present in the tuple, else it returns False.
>>> tuple1 = ('Red','Green','Blue')
>>> 'Green' not in tuple1
False
10.2.4 Slicing
Like string and list, slicing can be applied to tuples also.
#tuple1 is a tuple
>>> tuple1 = (10,20,30,40,50,60,70,80)
#elements from index 2 to index 6  
>>> tuple1[2:7]                         
(30, 40, 50, 60, 70)
#all elements of tuple are printed
>>> tuple1[0:len(tuple1)]               
(10, 20, 30, 40, 50, 60, 70, 80)
#slice starts from zero index
>>> tuple1[:5]            
(10, 20, 30, 40, 50)
#slice is till end of the tuple
>>> tuple1[2:]    
(30, 40, 50, 60, 70, 80)
Ch 10.indd   210 08-Apr-19   12:28:09 PM
2024-25
Page 5


10.1 Introduct Ion to t uples A tuple is an ordered sequence of elements of different 
data types, such as integer, float, string, list or even a 
tuple. Elements of a tuple are enclosed in parenthesis 
(round brackets) and are separated by commas. Like list 
and string, elements of a tuple can be accessed using 
index values, starting from 0. 
“Computers are to computing 
as instruments are to music. 
Software is the score whose 
interpretations amplifies our 
reach and lifts our spirits. 
Leonardo da Vinci called music 
the shaping of the invisible, and 
his phrase is even more apt as a 
description of software.”
– A Kay
Chapter 10
Tuples and Dictionaries
In this chapter
 » Introduction to 
Tuples 
 » Tuple Operations
 » Tuple Methods and 
Built-in Functions
 » Tuple Assignment
 » Nested Tuples
 » Tuple Handling
 » Introduction to 
Dictionaries
 » Dictionaries are 
Mutable
 » Dictionary 
Operations
 » Traversing a 
Dictionary
 » Dictionary Methods 
and Built-in 
Functions
 » Manipulating 
Dictionaries
Example 10.1 
#tuple1 is the tuple of integers
>>> tuple1 = (1,2,3,4,5)
>>> tuple1
(1, 2, 3, 4, 5)
#tuple2 is the tuple of mixed data types
>>> tuple2 =('Economics',87,'Accountancy',89.6)
>>> tuple2
('Economics', 87, 'Accountancy', 89.6)
#tuple3 is the tuple with list as an element
>>> tuple3 = (10,20,30,[40,50])
>>> tuple3
(10, 20, 30, [40, 50])
#tuple4 is the tuple with tuple as an element
>>> tuple4 = (1,2,3,4,5,(10,20))
>>> tuple4
(1, 2, 3, 4, 5, (10, 20))
If there is only a single element in a tuple then the 
element should be followed by a comma. If we assign the 
value without comma it is treated as integer. It should 
be noted that a sequence without parenthesis is treated 
as tuple by default.
#incorrect way of assigning single element to 
#tuple
 #tuple5 is assigned a single element
>>> tuple5 = (20)
Ch 10.indd   207 08-Apr-19   12:28:09 PM
2024-25
Computer SCien Ce – Cla SS xi 208
>>> tuple5
20
>>>type(tuple5)  #tuple5 is not of type tuple
<class 'int'>    #it is treated as integer 
#Correct Way of assigning single element to 
#tuple
#tuple5 is assigned a single element
>>> tuple5 = (20,) #element followed by comma
>>> tuple5
(20,)
>>>type(tuple5)    #tuple5 is of type tuple
<class 'tuple'>
#a sequence without parentheses is treated as 
#tuple by default
>>> seq = 1,2,3     #comma separated elements
>>> type(seq)       #treated as tuple
<class 'tuple'>
>>> print(seq)      #seq is a tuple
(1, 2, 3)
10.1.1 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.
>>> tuple1 = (2,4,6,8,10,12)       
#initializes a tuple tuple1
#returns the first element of tuple1
>>> tuple1[0]                      
2
#returns fourth element of tuple1
>>> tuple1[3]                      
8
#returns error as index is out of range
>>> tuple1[15]                     
IndexError: tuple index out of range
#an expression resulting in an integer index
>>> tuple1[1+4]              
12
#returns first element from right
>>> tuple1[-1]                     
12
10.1.2 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. 
>>> tuple1 = (1,2,3,4,5)
We generally use list 
to store elements of 
the same data types 
whereas we use tuples 
to store elements of 
different data types.
Ch 10.indd   208 08-Apr-19   12:28:09 PM
2024-25
Tuples and d ic Tionaries 209
>>> tuple1[4] = 10
TypeError: 'tuple' object does not support 
item assignment
However an element of a tuple may be of mutable type, 
e.g., a list.
#4th element of the tuple2 is a list
>>> tuple2 = (1,2,3,[8,9])    
#modify the list element of the tuple tuple2
>>> tuple2[3][1] = 10           
#modification is reflected in tuple2
>>> tuple2                       
(1, 2, 3, [8, 10])           
10.2 t uple o perat Ions 10.2.1 Concatenation
Python allows us to join tuples using concatenation 
operator depicted by symbol +. We can also create a new 
tuple which contains the result of this concatenation 
operation.
>>> tuple1 = (1,3,5,7,9)
>>> tuple2 = (2,4,6,8,10)
>>> tuple1 + tuple2                
#concatenates two tuples
(1, 3, 5, 7, 9, 2, 4, 6, 8, 10)
>>> tuple3 = ('Red','Green','Blue')
>>> tuple4 = ('Cyan', 'Magenta', 'Yellow' 
,'Black')
#tuple5 stores elements of tuple3 and tuple4
>>> tuple5 = tuple3 + tuple4
>>> tuple5
('Red','Green','Blue','Cyan','Magenta', 
'Yellow','Black')
Concatenation operator can also be used for 
extending an existing tuple. When we extend a tuple 
using concatenation a new tuple is created.
>>> tuple6 = (1,2,3,4,5)
#single element is appended to tuple6
>>> tuple6 = tuple6 + (6,)      
>>> tuple6
(1, 2, 3, 4, 5, 6)
#more than one elements are appended
>>> tuple6 = tuple6 + (7,8,9)    
>>> tuple6
(1, 2, 3, 4, 5, 6, 7, 8, 9)
v List is mutable but 
tuple is immutable. 
So iterating through 
a tuple is faster as 
compared to a list. 
v If we have data that 
does not change 
then storing this 
data in a tuple will 
make sure that 
it is not changed 
accidentally.
Ch 10.indd   209 08-Apr-19   12:28:09 PM
2024-25
Computer SCien Ce – Cla SS xi 210
10.2.2 Repetition
Repetition operation is depicted by the symbol *. It is 
used to repeat elements of a tuple. We can repeat the 
tuple elements. The repetition operator requires the first 
operand to be a tuple and the second operand to be an 
integer only.
>>> tuple1 = ('Hello','World')
>>> tuple1 * 3
('Hello', 'World', 'Hello', 'World', 'Hello', 
'World')
#tuple with single element
>>> tuple2 = ("Hello",)    
>>> tuple2 * 4
('Hello', 'Hello', 'Hello', 'Hello')
10.2.3 Membership
The in operator checks if the element is present in the 
tuple and returns True, else it returns False. 
>>> tuple1 = ('Red','Green','Blue')
>>> 'Green' in tuple1
True
The not in operator returns True if the element is 
not present in the tuple, else it returns False.
>>> tuple1 = ('Red','Green','Blue')
>>> 'Green' not in tuple1
False
10.2.4 Slicing
Like string and list, slicing can be applied to tuples also.
#tuple1 is a tuple
>>> tuple1 = (10,20,30,40,50,60,70,80)
#elements from index 2 to index 6  
>>> tuple1[2:7]                         
(30, 40, 50, 60, 70)
#all elements of tuple are printed
>>> tuple1[0:len(tuple1)]               
(10, 20, 30, 40, 50, 60, 70, 80)
#slice starts from zero index
>>> tuple1[:5]            
(10, 20, 30, 40, 50)
#slice is till end of the tuple
>>> tuple1[2:]    
(30, 40, 50, 60, 70, 80)
Ch 10.indd   210 08-Apr-19   12:28:09 PM
2024-25
Tuples and d ic Tionaries 211
#step size 2
>>> tuple1[0:len(tuple1):2]      
(10, 30, 50, 70)
#negative indexing
>>> tuple1[-6:-4]                     
(30, 40)
#tuple is traversed in reverse order
>>> tuple1[::-1]                      
(80, 70, 60, 50, 40, 30, 20, 10)
10.3 t uple Methods and Bu Ilt -In Funct Ions Python provides many functions to work on tuples. Table 
10.1 list some of the commonly used tuple methods and 
built-in functions.
Table 10.1  Built-in functions and methods for tuples
Method Description Example
len() Returns the length or the number of 
elements of the tuple passed as the 
argument
>>> tuple1 = (10,20,30,40,50)
>>> len(tuple1)
5
tuple() Creates an empty tuple if no argument 
is passed
Creates a tuple if a sequence is 
passed as argument
>>> tuple1 = tuple()
>>> tuple1
( )
>>> tuple1 = tuple('aeiou')#string
>>> tuple1
('a', 'e', 'i', 'o', 'u')
>>> tuple2 = tuple([1,2,3]) #list
>>> tuple2
(1, 2, 3)
>>> tuple3 = tuple(range(5))
>>> tuple3
(0, 1, 2, 3, 4)
count() Returns the number of times the 
given element appears in the tuple
>>> 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
>>> tuple1 = (10,20,30,40,50)
>>> tuple1.index(30)
2
>>> tuple1.index(90)
ValueError: tuple.index(x): x not 
in tuple
Ch 10.indd   211 08-Apr-19   12:28:09 PM
2024-25
Read More
33 docs|11 tests

FAQs on NCERT Textbook: Tuples and Dictionaries - Computer Science for Class 11 - Humanities/Arts

1. What are tuples in Python and how are they different from lists?
Ans. Tuples in Python are immutable sequences, meaning once they are created, their elements cannot be changed or modified. They are defined using parentheses, e.g., `my_tuple = (1, 2, 3)`. In contrast, lists are mutable and can be modified after creation, defined using square brackets, e.g., `my_list = [1, 2, 3]`. This immutability of tuples makes them more suitable for fixed collections of items, while lists are better for collections that may change.
2. How can I create a dictionary in Python?
Ans. A dictionary in Python is created using curly braces `{}` with key-value pairs separated by colons. For example, `my_dict = {'name': 'Alice', 'age': 25}` creates a dictionary with two entries: one for the name and one for the age. You can also use the `dict()` function, like `my_dict = dict(name='Alice', age=25)`.
3. What are the main use cases for using tuples and dictionaries in Python?
Ans. Tuples are often used for heterogeneous data that should not change, such as coordinates (x, y) or records. They can also be used as keys in dictionaries because they are hashable. Dictionaries, on the other hand, are used for associative arrays where you need to look up values based on unique keys, such as a phone book or settings configuration, allowing for fast retrieval.
4. Can I change the elements of a tuple after creation?
Ans. No, you cannot change the elements of a tuple after it has been created because tuples are immutable. This means you cannot add, remove, or modify any of the elements. If you need a different collection, you would need to create a new tuple with the desired values.
5. How do I access elements in a dictionary?
Ans. You can access elements in a dictionary using their keys. For example, if you have a dictionary `my_dict = {'name': 'Alice', 'age': 25}`, you can access the name by `my_dict['name']` which will return 'Alice'. If you try to access a key that does not exist, Python will raise a `KeyError`. To avoid this, you can use the `get()` method, like `my_dict.get('name')`, which will return `None` if the key is not found.
Related Searches

Extra Questions

,

practice quizzes

,

shortcuts and tricks

,

video lectures

,

Previous Year Questions with Solutions

,

Important questions

,

Free

,

NCERT Textbook: Tuples and Dictionaries | Computer Science for Class 11 - Humanities/Arts

,

Sample Paper

,

Viva Questions

,

pdf

,

NCERT Textbook: Tuples and Dictionaries | Computer Science for Class 11 - Humanities/Arts

,

MCQs

,

NCERT Textbook: Tuples and Dictionaries | Computer Science for Class 11 - Humanities/Arts

,

mock tests for examination

,

study material

,

past year papers

,

ppt

,

Semester Notes

,

Objective type Questions

,

Summary

,

Exam

;