Chapter 10: Tuple and Dictionary

Program 10–1 This is a program to create a nested tuple to store roll number, name and marks of students
a=eval(input("Enter student details"))
print("roll","name","marks")
for i in a:
print(i[0],i[1],i[2])
Output:
============= RESTART: C:\Users\vijay\OneDrive\Desktop\3.py ============
Enter student details((101,"stu1",89),(102,"stu2",78))
roll name marks
101 stu1 89
102 stu2 78
Program 10–2 Write a program to swap two numbers without using a temporary variable.
a=int(input("Enter first value"))
b=int(input("Enter second value"))
print("Actual tuple",(a,b))
(a,b)=(b,a)
print("After swap",(a,b))
======== RESTART: C:\Users\RAINBOW\Videos\1.py ========
Enter first value10
Enter second value20
Actual tuple (10, 20)
After swap (20, 10)
program 10–3 Write a program to compute the area and circumference of a circle using a function.
r=int(input("Enter radius"))
area=3.14*r*r
circum=2*3.14*r
print("area",area)
print("circumference",round(circum,2))
OUtput:
======== RESTART: C:\Users\RAINBOW\Videos\1.py ========
Enter radius10
area 314.0
circumference 62.8
Program 10–4 Write a program to input n numbers from the user. Store these numbers in a tuple. Print the maximum and minimum number from this tuple.
n=int(input("Enter max elements"))
a=()
for i in range(n):
b=int(input("Enter the num"))
a=a+(b,)
print("max",max(a))
print("min",min(a))
Output:
======== RESTART: C:\Users\RAINBOW\Videos\1.py ========
Enter max elements4
Enter the num10
Enter the num2
Enter the num3
Enter the num1
max 10
min 1
Program 10–5 Create a dictionary ‘ODD’ of odd numbers between 1 and 10, where the key is the decimal number and the value is the corresponding number in words. Perform the following operations on this dictionary:
(a) Display the keys
(b) Display the values
© Display the items
(d) Find the length of the dictionary
(e) Check if 7 is present or not
(f) Check if 2 is present or not
(g) Retrieve the value corresponding to the key 9
(h) Delete the item from the dictionary corresponding to the key 9
odd={1:"one",3:"three",5:"five",7:"seven",9:"nine"}
print(odd.keys())
print(odd.values())
print(odd.items())
print("length",len(odd))
if 7 in odd:
print("7 present")
else:
print(" 7 is not there")
if 2 in odd:
print("2 is present")
else:
print("2 is absent")
print(odd[9])
del odd[9]
print("after del",odd)
Output:
======== RESTART: C:\Users\RAINBOW\Videos\1.py ========
dict_keys([1, 3, 5, 7, 9])
dict_values(['one', 'three', 'five', 'seven', 'nine'])
dict_items([(1, 'one'), (3, 'three'), (5, 'five'), (7, 'seven'), (9, 'nine')])
length 5
7 present
2 is absent
nine
after del {1: 'one', 3: 'three', 5: 'five', 7: 'seven'}
Program 10–6 Write a program to enter names of employees and their salaries as input and store them in a dictionary.
a=int(input("Enter no of emp available"))
b={}
for i in range(a):
empn=input("ENter name of emp")
sal=int(input("Enter the sal"))
b[empn]=sal
print("Name","Sal")
for j in b:
print(j,b[j])
Output:
======== RESTART: C:\Users\RAINBOW\Videos\1.py ========
Enter no of emp available3
ENter name of empemp1
Enter the sal10
ENter name of empemp2
Enter the sal11
ENter name of empemp3
Enter the sal12
Name Sal
emp1 10
emp2 11
emp3 12
Program 10–7 Write a program to count the number of times a character appears in a given string.
a=input("Enter a string: ")
b={}
for i in a:
temp=a.count(i)
b[i]=temp
for key in b:
print(key,":",b[key])
Output:
Enter a string: helloworld
h : 1
e : 1
l : 3
o : 2
w : 1
r : 1
d : 1
Program 10–8 Write a function to convert a number entered by the user into its corresponding number in words. For example, if the input is 876 then the output should be ‘Eight Seven Six’
n={"0":"Zero","1":"One","2":"Two","3":"Three","4":"Four","5":"Five","6":"Six","7":"Seven","8":"Eight","9":"Nine"}
num=input("Enter any number: ")
for i in num:
print(n[i],end=" ")
Output:
= RESTART: C:/Users/Raibow 6/Desktop/1.py
Enter any number: 123
One Two Three
Exercise Questions and Answers:
- 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()]
a=eval(input("Enter the email ids of the students"))
name=domain=()
for i in a:
b=i.split("@")
name=name+(b[0],)
domain=domain+(b[1],)
print("email ids",a)
print("user names",name)
print("domain",domain)
Output:
= RESTART: C:/Users/rainb/OneDrive/Documents/Desktop/1.py
Enter the email ids of the students"abc@gmail.com","xyz@yahoo.com"
email ids ('abc@gmail.com', 'xyz@yahoo.com')
user names ('abc', 'xyz')
domain ('gmail.com', 'yahoo.com')
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.
a=int(input("Enter the max number of students"))
b=()
for i in range(a):
temp=input("Enter the name ")
b=b+(temp,)
print(b)
c=input("Enter the name to search")
if c in b:
print("student found")
else:
print("student name not found")
Output:
========= RESTART: C:\Users\rainb\OneDrive\Documents\Desktop\1.py ========
Enter the max number of students3
Enter the name stu2
Enter the name stu3
Enter the name stu4
('stu2', 'stu3', 'stu4')
Enter the name to searchstu1
student name not found
3. Write a Python program to find the highest 2 values in a dictionary.
a={1:10,2:20,4:45,3:12}
b=a.values()
c=sorted(b)
print(c[-1],c[-2])
Output:
= RESTART: C:\Users\vijay\OneDrive\Desktop\3.py
45 20
4. 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}
a=input("Enter the string")
b={}
for i in a:
b[i]=a.count(i)
for j in b:
print(j,":",b[j])
Output:
============= RESTART: C:\Users\vijay\OneDrive\Desktop\3.py ============
Enter the stringw3resource
w : 1
3 : 1
r : 2
e : 2
s : 1
o : 1
u : 1
c : 1
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:
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
a=eval(input("Enter frnd name and num in dictionary"))
print("Dictionary",a)
b=eval(input("Enter name and num to add"))
a.update(b)
print("modified",a)
c=input("Enter frnd name to delete")
del a[c]
print("after del",a)
d=eval(input("Enter name of frnd to update num"))
a.update(d)
print("updated",a)
e=input("Enter frnd name to search")
if e in a:
print("name found")
else:
print("not found")
print(sorted(a))
Output:
============= RESTART: C:\Users\vijay\OneDrive\Desktop\3.py ============
Enter frnd name and num in dictionary{"frnd1":123,"frnd2":456,"frnd3":789}
Dictionary {'frnd1': 123, 'frnd2': 456, 'frnd3': 789}
Enter name and num to add{"frnd4":159}
modified {'frnd1': 123, 'frnd2': 456, 'frnd3': 789, 'frnd4': 159}
Enter frnd name to deletefrnd4
after del {'frnd1': 123, 'frnd2': 456, 'frnd3': 789}
Enter name of frnd to update num{"frnd2":111}
{'frnd1': 123, 'frnd2': 111, 'frnd3': 789}
Enter frnd name to searchfrnd2
name found
['frnd1', 'frnd2', 'frnd3']