-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dictionary.py
98 lines (63 loc) · 2.65 KB
/
Dictionary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
myDict={
"name":"Varinder",
"age":27,
"work":"Content creater",
"isStillworking":True,
"Numbers":[1,23,4,56,7]
}
print(myDict) #print all elements of dictionary
print(myDict["age"])#print only age
print(myDict["work"])#print only work
print(myDict["isStillworking"])#print only working?
print(myDict["Numbers"])#print only numbers
#name=key #Varinder=value
# *Data-Types you can put in Dictionary*
# 1. Interger("int")
# 2. Boolen ("bol")
# 3. String-Literal("str")
# 4. List("lst")
# 5. Dictionary("dic")
#Dictionary in Dictionary Example:
myData={
"name":"Varinder",
"age":27,
"Subjects":{"name":"Hindi","marks":75,"ispass":True}
}
print(myData["Subjects"]["name"])#to inside list you have write print like this
myData["Subjects"]["marks"]=100; #you can value very simple manner
print(myData["Subjects"]["marks"])
#what if key value pairs is not present in dictionary
myData["GoodInStudy"]=True #write key-value pair if that is not ther than it will automatically added
# what happen if key is there already than last entry of same key-value will b shown in ou Dictionary
print(myData["GoodInStudy"])
print(myData.keys()) #now with this all keys will print
#to check type on keys
print(type(myData.keys()))
#to convert keys in list
print(list(myData.keys())) #insted "keys" write "list" than you get keys as list
#Print all value of Dictionary
print(myData.values()) #now with this all values will print
#to check type on keys
print(type(myData.values()))
#to convert values in list
print(list(myData.values())) #insted "values" write "list" than you get values as list
print(myData.items())#you get all items in form of tuple
#update key-value pair with new entery
newItem={
"Mac-Book":"New_wish"
} # you can add key-value pairs as much you want
myData.update(newItem) #update new key-value in dictionary
print(myData) #now you can see new entry will be there
#print(myData.get("name"))
#print(myData.get("names")) # you got None
# .get() give value when you write key-name to get value but in case "Key-name" is not present then it show "None"
#print(myData["name"])
#print(myData["names"]) #you got error
# print(myData) in this case if "Key" is not present it will show error
# If user provide input then he get value of input example:
print(myData.keys())
UserEntery=input("Enter to search \n")
#print("Your results",myData[UserEntery])
# But if userEntery word is not in our Distionary then it will throug error but you have to tackle with error how?
# Use ".get()" rather than "[]" syntax
print(myData.get(UserEntery)) # now if input entery is not in Dictionary than user get "None" instead of getting "error"