Chapter 10 Tuples and Dictionaries
Chapter 10 Tuples and Dictionaries
If there is only a single element in a tuple, the element should be followed by a comma. Without a
comma, it is treated as an integer. Sequences without parentheses are treated as tuples by default.
Correct way of assigning a single element:
[ ]: tuple5 = (20,)
print(tuple5) # Output: (20,)
print(type(tuple5)) # Output: <class 'tuple'>
[ ]: seq = 1, 2, 3
print(seq) # Output: (1, 2, 3)
print(type(seq)) # Output: <class 'tuple'>
1
Chapter 10 Tuples and Dictionaries
10.1.1 Accessing Elements in a Tuple Elements of a tuple can be accessed using indexing
and slicing.
Examples:
10.1.2 Tuple is Immutable Tuples are immutable, meaning their elements cannot be changed
after creation. Any attempt to modify an element results in an error.
Example:
[ ]: tuple1 = (1, 2, 3, 4, 5)
# tuple1[4] = 10 # Raises TypeError
10.2 Tuple Operations 10.2.1 Concatenation Tuples can be joined using the concatenation
operator (+).
Examples:
[ ]: tuple1 = (1, 3, 5, 7, 9)
tuple2 = (2, 4, 6, 8, 10)
print(tuple1 + tuple2) # Output: (1, 3, 5, 7, 9, 2, 4, 6, 8, 10)
10.2.2 Repetition The repetition operation (*) is used to repeat elements of a tuple.
Examples:
tuple2 = ("Hello",)
print(tuple2 * 4) # Output: ('Hello', 'Hello', 'Hello', 'Hello')
10.2.3 Membership The in operator checks if an element is present in the tuple, and the not in
operator checks if an element is absent.
Examples:
10.3 Tuple Methods and Built-in Functions Python provides various methods and functions
to work with tuples.
[ ]:
10.4 Tuple Assignment Tuples support a useful feature called tuple assignment, allowing mul-
tiple variables to be assigned values from a tuple simultaneously.
Example:
10.5 Nested Tuples A tuple inside another tuple is called a nested tuple.
Program 10-1: Creating a Nested Tuple
[ ]: st = ((101, "Aman", 98), (102, "Geet", 95), (103, "Sahil", 87), (104, "Pawan",␣
↪79))
[ ]: def circle(r):
area = 3.14 * r * r
circumference = 2 * 3.14 * r
return (area, circumference)
)
print("\nArea:", area)
print("Circumference:", circumference)
[ ]: D = {}
print(D) # Output: {}
10.7.2 Changing, Adding, and Deleting Dictionary Elements Dictionaries are mutable
Dictionary elements can be modified or deleted using keys.
Examples:
# Changing a element
Dict['N'] = 92
print(Dict) # Output: {'M': 77, 'N': 92, 'O': 79}
#Deleting an element
del Dict['M']
print(Dict) # Output: {'N': 92, 'O': 79, 'P': 80}
# del Dict['M'] # Raises KeyError
Dict.clear()
print(Dict) # Output: {}
[ ]: dict1 = {'Mohan':95,'Ram':89,'Suhel':92,'Sangeeta':85}
print('Suhel' in dict1)
dict1 = {'Mohan':95,'Ram':89,'Suhel':92,'Sangeeta':85}
print('Suhel' not in dict1)
[ ]: dict1 = {'Mohan':95,'Ram':89,'Suhel':92,'Sangeeta':85}
#method 1
for key in dict1:
print(key,':',dict1[key])
#method 2
for key,value in dict1.items():
print(key,':',value)
10.8 Dictionary Methods and Built-in Functions Python provides various methods and
functions to work with dictionaries.
[ ]:
Program 10-6 Write a program to enter names of employees and their salaries as input and store
them in a dictionary.
Exercises 1. 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 the 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().
[ ]:
2. 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.
[ ]:
[ ]:
[ ]:
5. 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:
• Display the name and phone number of all your friends
• Add a new key-value pair in this dictionary and display the modified dictionary
• Delete a particular friend from the dictionary
• Modify the phone number of an existing friend
• Check if a friend is present in the dictionary or not
• Display the dictionary in sorted order of names
[ ]: