NCERT Solutions Class 11, Computer Science, Chapter- 10, Tuples and Dictionaries
Exercise
1. Consider the following tuples, tuple1 and tuple2:
tuple1 = (23,1,45,67,45,9,55,45)
tuple2 = (100,200)
Find the output of the following statements:
(i) print(tuple1.index(45))
Solution:
The 'index()' function returns the index of the first occurrence of the element in a tuple.
OUTPUT:
2
(ii) print(tuple1.count(45))
Solution:
The 'count()' function returns the number of times the given element appears in the tuple.
OUTPUT: 3
(iii) print(tuple1 + tuple2)
Solution:
The '+' operator concatenates the two tuples.
OUTPUT:
(23, 1, 45, 67, 45, 9, 55, 45, 100, 200)
(iv) print(len(tuple2))
Solution:
The 'len()' function returns the number of elements in the given tuple.
OUTPUT: 2
(v) print(max(tuple1))
Solution:
The 'max()' function returns the largest element of the tuple.
OUTPUT: 67
(vi) print(min(tuple1))
Solution:
The 'min()' function returns the smallest element of the tuple.
OUTPUT: 1
(vii) print(sum(tuple2))
Solution:
The 'sum()' function returns the sum of all the elements of the tuple.
OUTPUT:
300
(viii) print ( sorted( tuple1 ) )
print(tuple1)
Solution:
The 'sorted()' function takes an element in the tuple and returns a new sorted list. It doesn’t make any changes to the original tuple. Hence, print(tuple1) will print the original tuple1 i.e. (23, 1, 45, 67, 45, 9, 55, 45)
OUTPUT:
[1, 9, 23, 45, 45, 45, 55, 67]
(23, 1, 45, 67, 45, 9, 55, 45)
2. Consider the following dictionary stateCapital:
stateCapital = {"AndhraPradesh":"Hyderabad", "Bihar":"Patna","Maharashtra":"Mumbai", "Rajasthan":"Jaipur"}
Find the output of the following statements:
(i) print(stateCapital.get("Bihar"))
Solution:
Patna: 'get()' function returns the value corresponding to the key passed as an argument.
(ii) print(stateCapital.keys())
Solution:
dict_keys(['AndhraPradesh', 'Bihar', 'Maharashtra', 'Rajasthan']): 'keys()' function returns the list of the keys in the dictionary.
(iii) print(stateCapital.values())
Solution:
dict_values(['Hyderabad', 'Patna', 'Mumbai', 'Jaipur']): 'values()' function returns the list of the values in the dictionary.
(iv) print(stateCapital.items())
Solution:
dict_items([('AndhraPradesh', 'Hyderabad'), ('Bihar', 'Patna'), ('Maharashtra', 'Mumbai'), ('Rajasthan', 'Jaipur')]): 'items()' function returns the list of tuples in key value pair.
(v) print(len(stateCapital))
Solution:
4: 'len()' functions return the length or number of key: value pairs of the dictionary passed as the argument.
(vi) print("Maharashtra" in stateCapital)
Solution:
True: 'in' is a membership operator which returns true if a key is present in the dictionary.
(vii) print(stateCapital.get("Assam"))
Solution:
None: 'get()' function returns 'None' if the key is not present in the dictionary.
(viii) del stateCapital["Rajasthan"] print(stateCapital)
Solution:
del stateCapital["Andhra Pradesh"] will return an error as the key ‘Andhra Pradesh’ is not present in the dictionary. (‘Andhra Pradesh’ and ‘AndhraPradesh’ are different). However, if the key is present it will delete the key: value pair from the dictionary. If the statement is del stateCapital["AndhraPradesh"], print(stateCapital) will then print the remaining key: value pair. {'Bihar': 'Patna', 'Maharashtra': 'Mumbai', 'Rajasthan': 'Jaipur'}
3. “Lists and Tuples are ordered”. Explain.
Solution:
In Lists and Tuples, the items are retained in the order in which they are inserted. The elements can always be accessed based on their position. The element at the position or ‘index’ 0 will always be at index 0. Therefore, the Lists and Tuples are said to be ordered collections.
4. With the help of an example show how can you return more than one value from a function.
Solution:
In a tuple, more than one value can be returned from a function by ‘packing’ the values. The tuple can be then ‘unpacked’ into the variables by using the same number of variables on the left-hand side as there are elements in a tuple.
Example:
Program:
#Function to compute area and perimeter of the square.
def square(r):
area = r * r
perimeter = 4 * r
#Returns a tuple having two elements area and perimeter, i.e. two variables are packed in a tuple
return (area, perimeter)
#end of function
side = int(input("Enter the side of the square: "))
#The returned value from the function is unpacked into two variables area and perimeter
area, perimeter = square(side)
print("Area of the square is:",area)
print("The perimeter of the square is:",perimeter)
OUTPUT:
Enter the side of the square: 4
Area of the square is: 16
The perimeter of the square is: 16
5. What advantages do tuples have over lists?
Solution:
The advantages of tuples over the lists are as follows:
-
Tuples are faster than lists.
-
Tuples make the code safe from any accidental modification. If data is needed in a program that is not supposed to be changed, then it is better to put it in ‘tuples’ than in ‘list’.
-
Tuples can be used as dictionary keys if it contains immutable values like strings, numbers, or other tuples. ‘Lists’ can never be used as dictionary keys as ‘lists’ are mutable.
6. When to use tuple or dictionary in Python. Give some examples of programming situations mentioning their usefulness.
Solution:
Tuples are used to store the data which is not intended to change during the course of the execution of the program. For example, if the name of months is needed in a program, then the same can be stored in the tuple as generally, the names will either be iterated for a loop or referenced sometimes during the execution of the program.
Dictionary is used to store associative data like the student's roll no. and the student's name. Here, roll no. will act as a key to find the corresponding student's name. The position of the data doesn’t matter as the data can easily be searched by using the corresponding key.
7. Prove with the help of an example that the variable is rebuilt in case of immutable data types.
Solution:
When a variable is assigned to the immutable data type, the value of the variable cannot be changed in place. Therefore, if we assign any other value to the variable, the interpreter creates a new memory location for that value and then points the variable to the new memory location. This is the same process in which we create a new variable. Thus, it can be said that the variable is rebuilt in case of immutable data types on every assignment.
Program to represent the same:
#Define a variable with immutable datatypes as value
var = 50
#Checking the memory location of the variable
print("Before: ",id(var))
#Assign any other value
var = 51
#Checking the memory location of the variable
print("After: ",id(var))
OUTPUT:
Before: 10916064
After: 10916096
It can be seen that the memory location a variable is pointing at after the assignment is different. The variable is entirely new and it can be said that the variable is rebuilt.