Dict
Dict
Dict
--------------------------
We can use List,Tuple and Set to represent a group of individual objects as a
single entity. If we want to represent a group of objects as key-value pairs then
we should go for dict data type (Dictionary).
Ex:
rollno----name
phone number--address
ipaddress---domain name
syntax:
d={key:value, key:value}
Note: We can prevent this by checking whether key is already available or not by
using in operator.
Ex:
if 400 in d:
print(d[400])
Q. Write a program to enter name and percentage marks in a dictionary and display
information on the screen.
4.
d.get(key) If the key is available then returns the corresponding value otherwise
returns None. It wont raise any error.
d.get(key,defaultvalue) If the key is available then returns the corresponding
value otherwise returns default value.
Note: get()To get the value associated with the key
5. pop():
Syntax:
d.pop(key) It removes the entry associated with the specified key and returns the
corresponding value If the specified key is not available then we will get KeyError
6. popitem(): The popitem() method removes the item (key-value) that was last
inserted into the dictionary and returns it.
d={}
If the dictionary is empty then we will get KeyError
print(d.popitem()) ==>KeyError: 'popitem(): dictionary is empty'
11. setdefault(): If the key is already available then this function returns the
corresponding value. If the key is not available then the specified key-value will
be added as new item to the dictionary.
Syntax:
d.setdefault(k,v)
12. update(): The update() method inserts the specified items to the dictionary.
The specified items can be a dictionary, or an iterable object with key value
pairs.
Syntax
dictionary.update(iterable)
Ex:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car.update({"color": "White"})
print(car)
Ex:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car.update({"color": "White",'price':10000000})
print(car)
13. fromkeys() Method: The fromkeys() method returns a dictionary with the
specified keys and the specified value.
Syntax
dict.fromkeys(keys, value)
Ex:
x = ('key1', 'key2', 'key3')
thisdict = dict.fromkeys(x)
print(thisdict)
Parameter
keys: Required. An iterable specifying the keys of the new dictionary
value: Optional. The value for all keys. Default value is None
14. max():
15. min():
16. count():
17: sorted():
Q. Write a program to take dictionary from the keyboard and print the sum of
values?
d=eval(input("Enter dictionary:"))
s=sum(d.values())
print("Sum= ",s)
Q. Write a program to accept student name and marks from the keyboard and creates a
dictionary. Also display student marks by taking student name as input?
-----------------------------------------------------------------------------------
-----------------------------------------
Practice Codes:
-----------------------------------------------------------------------------------
-----------------------------------------
6. Write a Python script to generate and print a dictionary that contains a number
(between 1 and n) in the form (x, x*x).
Sample Dictionary ( n = 5)
Expected Output : {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Sol:
n=int(input("Input a number "))
d = dict()
for x in range(1,n+1):
d[x]=x*x
print(d)
7. Write a Python script to print a dictionary where the keys are numbers between 1
and 15 (both included) and the values are the square of the keys.
Sample Dictionary
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12:
144, 13: 169, 14: 196, 15: 225}
Sol:
d=dict()
for x in range(1,16):
d[x]=x**2
print(d)