Chapter 10 Tuples and Dictionaries in Python NCERT Solution

Share with others

Tuples and Dictionaries in Python

Tuples and Dictionaries in Python

Tuples are immutable sequences, i.e., we cannot change the elements of a tuple once it is created.

Elements of a tuple are put in round brackets separated by commas.

If a sequence has comma separated elements without parentheses, it is also treated as a tuple.

Tuples are ordered sequences as each element has a fixed position.

Indexing is used to access the elements of the tuple; two way indexing holds in dictionaries as
 in strings and lists.

Operator ‘+’ adds one sequence (string, list, tuple) to the end of other.

Operator ‘*’ repeats a sequence (string, list, tuple) by specified number of times

Membership operator ‘in’ tells if an element is present in the sequence or not and ‘not in’ does the opposite.

Tuple manipulation functions are: len(), tuple(), count(), index(), sorted(), min(), max(),sum().

Dictionary is a mapping (non-scalar) data type. It is an unordered collection of key-value pair; key value pair are put inside curly braces.

Each key is separated from its value by a colon.

Keys are unique and act as the index.

Keys are of immutable type but values can be mutable

Tuples and Dictionaries in Python

Tuples and Dictionaries in Python

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))

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)

Tuples and Dictionaries in Python

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”))

ii. print(stateCapital.keys())

iii. print(stateCapital.values())

iv. print(stateCapital.items())

v. print(len(stateCapital))

vi print(“Maharashtra” in stateCapital)

vii. print(stateCapital.get(“Assam”))

viii. del stateCapital[“AndhraPradesh”]
print(stateCapital)

NOTE : Part viii will return an error as there is a space between Andhra Pradesh in question and there is no such key in given dictionary. I removed the space and written output accordingly

Tuples and Dictionaries in Python

Q3. “Lists and Tuples are ordered”. Explain.

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

Q5. What advantages do tuples have over lists?

Tuples and Dictionaries in Python

Q6. When to use tuple or dictionary in Python. Give some examples of programming situations mentioning their usefulness.

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

Tuples and Dictionaries in Python

Q8. TypeError occurs while statement 2 is running.
Give reason. How can it be corrected?

tuple1 = (5) #statement 1
len(tuple1) #statement 2

PROGRAMMING PROBLEMS

Tuples and Dictionaries in Python

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

Tuples and Dictionaries in Python

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 and Dictionaries in Python

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


 

Tuples and Dictionaries in Python

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}

Tuples and Dictionaries in Python

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

Tuples and Dictionaries in Python

Tuples and Dictionaries in Python


Disclaimer : I tried to give you the correct “Tuples and Dictionaries in Python Chapter 10” , but if you feel that there is/are mistakes in the “Tuples and Dictionaries in Python Chapter 10 given above, you can directly contact me at csiplearninghub@gmail.com.


Tuples and Dictionaries in Python

Class 12 Computer Science Sample Paper 2020-2021.

Class 12 Computer Science Sample Paper Marking Scheme

Class 12 Computer Science Test Series


Share with others

Leave a Reply

error: Content is protected !!