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

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

Q1: 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))
Ans:
2

(ii) print(tuple1.count(45))
Ans:
3

(iii) print(tuple1 + tuple2)
Ans: 
(23, 1, 45, 67, 45, 9, 55, 45, 100, 200)

(iv) print(len(tuple2))
Ans:
2

(v) print(max(tuple1))
Ans:
67

(vi) print(min(tuple1))
Ans:
1

(vii) print(sum(tuple2))
Ans:
300

(viii) print ( sorted ( tuple1 ) )
print(tuple1)
Ans:
[1, 9, 23, 45, 45, 45, 55, 67]
(23, 1, 45, 67, 45, 9, 55, 45)

Q2: 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”))
Ans:
Patna

(ii) print(stateCapital.keys())
Ans:
dict_keys([‘AndhraPradesh’, ‘Bihar’, ‘Maharashtra’, ‘Rajasthan’])

(iii) print(stateCapital.values())
Ans: 
dict_values([‘Hyderabad’, ‘Patna’, ‘Mumbai’, ‘Jaipur’])

(iv) print(stateCapital.items())
Ans:
dict_items([(‘AndhraPradesh’, ‘Hyderabad’), (‘Bihar’, ‘Patna’), (‘Maharashtra’, ‘Mumbai’), (‘Rajasthan’, ‘Jaipur’)])

(v) print(len(stateCapital))
Ans:
4

(vi) print(“Maharashtra” in stateCapital)
Ans: 
True

(vii) print(stateCapital.get(“Assam”))
Ans: 
None

(viii) del stateCapital[“AndhraPradesh”]
print(stateCapital)
Ans: 
{‘Bihar’: ‘Patna’, ‘Maharashtra’: ‘Mumbai’, ‘Rajasthan’: ‘Jaipur’}

Q3: “Lists and Tuples are ordered”. Explain.
Ans:
The elements in the list and tuples are accessed by their position which is called indexing. Element at position 1(One) has index value 0 (zero), therefore, list and tuples are called ordered data type.

Q4: With the help of an example show how can you return more than one value from a function.
Ans:
 

def multivalue(a, b, c):

   a = a + 2

   b = b + 3

   c = c + 4

   return (a, b, c)

p = 8

q = 9

r = 10

p, q, r = multivalue(p, q, r)
print(p)
print(q)
print(r)

OUTPUT:
10
12
14

Q5: What advantages do tuples have over lists?
Ans: 
Advantages of tuples over list are
1. Tuples are faster than list (due to their fixed size)
2. Tuples can be used as keys of dictionary. (due to their immutable nature)
3. Elements of tuples can not be deleted by mistake. ( due to their immutable nature)

Q6: When to use tuple or dictionary in Python. Give some examples of programming situations mentioning their usefulness.
Ans: 
Main feature of tuple is that it is immutable so we use tuples in those programs where we want that data/elements should not change/edited during program like we use tuples to store name of the days, months in a program.
A dictionary is used to store related values like employee id, employee name and salary. Here id will act as a key while name and salary act as value.

Q7: Prove with the help of an example that the variable is rebuilt in case of immutable data types.
Ans:

T1 = (1,2,3,4,5)
T1[2] = 45
When we execute the above code, the following error occurs which shows that we can not change the value of tuple. (since it is immutable)
TypeError: ‘tuple’ object does not support item assignment
So if we want to change the value of tuple we have to rebuilt it. Rebuilt means to assign the value again like
T1 = (1, 2, 45, 4, 5)

Q8: TypeError occurs while statement 2 is running.
Give reason. How can it be corrected?
tuple1 = (5) #statement 1
len(tuple1) #statement 2
Ans:
As tuple1 variable is of integer type (you can verify by writing print(type(tuple1))) not of type ‘tuple’ and len function returns the length of any sequence like list, tuple, string etc. So the statement 2 is returning error.

Programming Problems

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()]
Ans: 

n=int(input("Enter how many email id to enter  : "))
t1=()
un = ()
dom = ()
for i in range(n):
   em = input("Enter email id  : ")
   t1 = t1 + (em,)

for i in t1:
   a = i.split('@')
   un = un + (a[0],)
   dom = dom + (a[1],)
print("Original Email id's are  : " , t1)
print("Username are  : " , un)
print("Doman name are  : " , dom)

OUTPUT : 
Enter how many email id to enter  : 2
Enter email id  : csip@gmail.com
Enter email id  : bike@yahoo.com
Original Email id's are  :  ('csip@gmail.com', 'bike@yahoo.com')
Username are  :  ('csip', 'bike')
Doman name are  :  ('gmail.com', 'yahoo.com')

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

Ans: 
(a)

def searchname(t1,nm):

   for a in t1:

     if a == nm:

       print(nm , " Name is present in Tuple")

       return

   print("Name not found")

n = int(input("How many names you want to enter  : "))

name = ()

for i in range(n):

   nm = input("Enter name  : ")

   name = name + (nm,)

nm = input("Enter name to search  : ")

searchname(name, nm)

OUTPUT
How many names you want to enter  : 3
Enter name  : Amit
Enter name  : Anuj
Enter name  : Ashish
Enter name to search  : Anuj
Anuj  Name is present in Tuple

(b)

n = int(input("How many names you want to enter  : "))

name = ()

for i in range(n):

   nm = input("Enter name  : ")

   name = name + (nm,)

nm = input("Enter name to search  : ")

if nm in name:

   print(nm," Name is present in Tuple")

else:

     print(nm," Name is not present in Tuple")

OUTPUT:
How many names you want to enter  : 3
Enter name  : Amit
Enter name  : Anuj
Enter name  : Ashu
Enter name to search  : Ashu
Ashu  Name is present in Tuple

Q3: Write a Python program to find the highest 2 values in a dictionary.
Ans: 

D = {'A' : 23, 'B' : 56, 'C' : 29, 'D' : 42, 'E' : 78 }

val = [ ]

v = D.values()

for i in v:

   val.append(i)

val.sort()

print("Two maximum values are")

print(val[-1])

print(val[-2])

OUTPUT:
Two maximum values are
 78
 56

Q4: Write a Python program to create a dictionary from a string.
Note: Track the count of the letters from the string.
Sample string : ‘w3resource’
Expected output : {‘3’: 1, ‘s’: 1, ‘r’: 2, ‘u’: 1, ‘w’: 1, ‘c’: 1,’e’: 2, ‘o’: 1}
Ans: 

str = input("Enter any String")
D = { }

for i in str:

   if i not in D:

     D[ i ] = str.count(i)

print(D)

OUTPUT :
Enter any String  : amitamit
{'a' : 2, 'm' : 2, 'i' : 2, 't' : 2}

Q5: Write a program to input your friends’ names and their Phone Numbers and store them in the dictionary as the key-value pair.
Perform the following operations on the dictionary:
(a) Display the name and phone number of all your friends
(b) Add a new key-value pair in this dictionary and display the modified dictionary
(c) Delete a particular friend from the dictionary
(d) Modify the phone number of an existing friend
(e) Check if a friend is present in the dictionary or not
(f) Display the dictionary in sorted order of names

Ans: 

friends = { }
while True:
     print("1. Add New Contact")

     print("2. Display all ")

     print("3. Delete any contact")

     print("4. Modify/Change Phone Number")

     print("5. Check if a friend is present or not")

     print("6. Display in sorted order of names")

     print("7. Exit")

     ch = int(input("Enter your choice  : "))

     if ch == 1 :

       nm = input("Enter name  : ")

       ph = input("Enter phone number  : ")

       friends[ph] = nm

       print("Record Added")

     elif ch == 2 :

       for i in friends :

         print("Name  : " , friends[i])

         print("Contact number : ", i)

     elif ch == 3 :

       c = input("Enter the phone number to delete")

       if c in friends:

         del friends[c]

       else:

         print("No Record found")

     elif ch == 4:

      p = input("Enter the contact number to be modified")

      if p in friends:

        n = input("Enter the new name")

        friends[p] = n

        print("Record Modified")

      else:

        print("No Record Found")

     elif ch == 5:

       ph = input("Enter the contact number to check")

       if ph in friends:

         print("Record Present")

       else :

         print("Record not found")

     elif ch == 6:

       for i in sorted(friends.values()):

         print(i)

     elif ch == 7:

       break

     else:

       print("Enter Value between 0 and 7")

The document Tuples and Dictionaries NCERT Solutions | Computer Science for Class 11 - Humanities/Arts is a part of the Humanities/Arts Course Computer Science for Class 11.
All you need of Humanities/Arts at this link: Humanities/Arts
33 docs|11 tests
Related Searches

Previous Year Questions with Solutions

,

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

,

Summary

,

Semester Notes

,

Extra Questions

,

MCQs

,

video lectures

,

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

,

shortcuts and tricks

,

mock tests for examination

,

Exam

,

Objective type Questions

,

pdf

,

past year papers

,

practice quizzes

,

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

,

Viva Questions

,

ppt

,

Important questions

,

Sample Paper

,

study material

,

Free

;