Tuples and Dictionary
Aayush
Tuple:
# To read email ids of n number of students and store them in a tuple. Create
two new tuples, one to store usernames and second to store domain names.
n=int(input('Enter the number of emails: '))
mail=()
dom=()
user=()
for i in range(n):
address=input('Enter an email address: ')
mail+=(address,)
for j in mail:
j=j.split('@')
user+=(j[0],)
dom+=(j[1],)
print('Email addresses: ', mail)
print('Usernames: ', user)
print('Domains: ', dom)
Output: Enter the number of emails: 3
Enter an email address: naresh123@gmail.com
Enter an email address: techhelp@protonmail.com
Enter an email address: alanturing@yahoo.com
Email addresses: ('naresh123@gmail.com', 'techhelp@protonmail.com',
'alanturing@yahoo.com')
Usernames: ('naresh123', 'techhelp', 'alanturing')
Domains: ('gmail.com', 'protonmail.com', 'yahoo.com')
#To input names of n students, store them in a tuple, input a name from the
user and find if this student is present in the tuple or not.
n=int(input('Enter the number of students: '))
student=()
for i in range(n):
name=input('Enter a name: ')
student+=(name,)
namecheck=input('Enter a name to check: ')
if namecheck in student:
print(namecheck, 'is present in the tuple.')
else:
print(namecheck, 'is not present in the tuple.')
Output: Enter the number of students: 3
Enter a name: Nitin
Enter a name: Naresh
Enter a name: Aayush
Enter a name to check: Harshit
Harshit is not present in the tuple.
#To input n numbers from the user, store these numbers in a tuple, and print the
max and min number from this tuple.
n=int(input('Enter length of tuple: '))
tup=()
for i in range(n):
num=eval(input('Enter a number: '))
tup+=(num,)
print('Maximum number: ', max(tup))
print('Minimum number: ', min(tup))
Output: Enter length of tuple: 4
Enter a number: 12
Enter a number: -3.14
Enter a number: 0
Enter a number: 0.99
Maximum number: 12
Minimum number: -3.14
Dictionary:
#To input friend's names and their phone numbers and store them in the
dictionary as the key-value pair.
n=int(input('Enter number of items: '))
d={}
for i in range(n):
name=input('Enter a name: ')
num=int(input('Enter their phone no.: '))
d[name]=num
print(d)
Output: Enter number of items: 3
Enter a name: Richard Feynman
Enter their phone no.: 9712845296
Enter a name: Friedrich Nietszche
Enter their phone no.: 9890234761
Enter a name: Leonhard Euler
Enter their phone no.: 9920389261
{'Feynman': 9712845296, 'Nietszche': 9890234761, 'Euler': 9920389261}
#a. Display the name and phone number for all your friends.
print('Names:', d.keys())
print('Numbers:', d.values())
Output: Names: dict_keys(['Feynman', 'Nietszche', 'Euler'])
Numbers: dict_values([9712845296, 9890234761, 9920389261])
#b. Add a new key-value pair in this dictionary and display the modified
dictionary.
d['Lussac']=9410456321
print('Modified dictionary:', d)
Output: Modified dictionary: {'Feynman': 9712845296, 'Nietszche': 9890234761,
'Euler': 9920389261, 'Lussac': 9410456321}
#c. Enter the name whose phone number you want to modify. Display the
modified /dictionary.
mod_name=input('Enter the name to access: ')
mod_num=int(input('Enter the new number: '))
d[mod_name]=mod_num
print('Modified dictionary:', d)
Output: Enter the name to access: Euler
Enter the new number: 9920389361
Modified dictionary: {'Feynman': 9712845296, 'Nietszche': 9890234761, 'Euler':
9920389361}
#d. Enter a friend's name and check if a friend is present in the dictionary or not.
mod_name=input('Enter the name to access: ')
if mod_name in d:
print(mod_name, 'is in the dictionary.')
else:
print(mod_name, 'is not in the dictionary.')
Output: Enter the name to access: Leibniz
Leibniz is not in the dictionary.
#e. Display the dictionary in sorted order of names.
print('Sorted order: ', dict(sorted(d.items())))
Output: Sorted order: {'Euler': 9920389261, 'Feynman': 9712845296, 'Nietszche':
9890234761}
#To find the highest 2 values in a dictionary.
n=int(input('Enter number of items: '))
d={}
for i in range(n):
key=input('Enter a key: ')
val=eval(input('Enter a value: '))
d[key]=val
d=sorted(d.values(), reverse=True)
print('Highest two values: ', d[0], 'and', d[1])
Output: Enter number of items: 3
Enter a key: A
Enter a value: -0.3
Enter a key: B
Enter a value: 1.4
Enter a key: C
Enter a value: 0.0
Highest two values: 1.4 and 0.0
#To create a dictionary from a string and track the count of the characters.
st=input('Enter a string: ')
d={}
for i in st:
val=st.count(i)
d[i]=val
print('Dictionary: ', d)
Output: Enter a string: Roblox
Dictionary: {'R': 1, 'o': 2, 'b': 1, 'l': 1, 'x': 1}
#Create a dictionary 'ODD' of odd numbers between 1 to 10, where the key is
the number and the value is the corresponding number in words.
ODD={1:'ONE' ,3:'THREE' ,5:'FIVE' ,7:'SEVEN' ,9:'NINE'}
#a. Display the keys.
keys=ODD.keys()
print(keys)
Output: dict_keys([1, 3, 5, 7, 9])
#b. Display the values.
val=ODD.values()
print(val)
Output: dict_values(['ONE', 'THREE', 'FIVE', 'SEVEN', 'NINE'])
#c. Display the items.
item=ODD.items()
print(item)
Output: dict_items([(1, 'ONE'), (3, 'THREE'), (5, 'FIVE'), (7, 'SEVEN'), (9, 'NINE')])
d. Find the length of the Dictionary.
print('Length of dictionary: ', len(ODD))
Output: Length of dictionary: 5
e. Check if 7 is present or not.
if 7 in ODD:
print('7 is present.')
else:
print('7 is not present.')
Output: 7 is present.
f.Check if 2 is present or not.
if 2 in ODD:
print('2 is present.')
else:
print('2 is not present.')
Output: 2 is not present.
g. Retrieve the value corresponding to the key 9.
print('Value: ', ODD[9])
Output: Value: NINE
h. Delete the item from the Dictionary ODD, corresponding to the key 9.
del ODD[9]
print('Modified dictionary: ',ODD)
Output: Modified dictionary: {1: 'ONE', 3: 'THREE', 5: 'FIVE', 7: 'SEVEN'}
#To convert a number entered by the user into its corresponding number in
words.
numdata={'1':'One', '2':'Two', '3':'Three', '4':'Four', '5':'Five', '6':'Six', '7':'Seven',
'8':'Eight', '9':'Nine', '0':'Zero'}
num=int(input('Enter a number: '))
st=str(num)
new_st = ''
for ch in st:
new_st += numdata[ch] + ' '
print('Number in words: ', new_st.strip())
Output: Enter a number: 23
Number in words: Two Three
#To create a dictionary which stores names of employees and their salary.
n=int(input('Enter number of items: '))
d={}
for i in range(n):
name=input('Enter name of employee: ')
salary=int(input('Enter their salary: '))
d[name]=salary
print('Dictionary: ', d)
Output: Enter number of items: 4
Enter name of employee: Naresh
Enter their salary: 200000
Enter name of employee: Vineeta
Enter their salary: 1500000
Enter name of employee: Kunwar
Enter their salary: 320000
Enter name of employee: Ronit
Enter their salary: 2500000
Dictionary: {'Naresh': 200000, 'Vineeta': 1500000, 'Kunwar': 320000, 'Ronit':
2500000}