Skip to content

Commit fc0e752

Browse files
committed
completing Dictionary
1 parent cba1cce commit fc0e752

6 files changed

+188
-0
lines changed

Dictionary/Practice.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
salary_data = {
2+
"John": 7000,
3+
"Rahul": 10000,
4+
"Manoj": 500000,
5+
"Raj": 80000,
6+
"Aman": 40000,
7+
}
8+

Dictionary/dictionary _copy.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
user = {
2+
"name" : "john",
3+
"age": 40,
4+
"password": "pass123"
5+
}
6+
7+
# both are same
8+
user_data = user.copy()
9+
# user_data = dict(user_data)
10+
11+
user_data["age"] = 41
12+
13+
print(user_data["age"]) # 41
14+
print(user["age"]) # 40

Dictionary/dictionary.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# salary_data = dict(
2+
# John= 7000,
3+
# Rahul= 10000,
4+
# Manoj= 500000,
5+
# Raj= 80000,
6+
# Aman= 40000
7+
# )
8+
9+
salary_data = {
10+
"John": 7000,
11+
"Rahul": 10000,
12+
"Manoj": 500000,
13+
"Raj": 80000,
14+
"Aman": 40000,
15+
}
16+
17+
# printing key of a dict
18+
for key in salary_data:
19+
print(key)
20+
21+
for key in salary_data.keys():
22+
print(key)
23+
24+
# printing values of a dict
25+
for key in salary_data:
26+
print(salary_data[key])
27+
28+
for val in salary_data.values():
29+
print(val)
30+
31+
# printing both item
32+
for key,val in salary_data.items():
33+
print(f"{key}:{val}")

Dictionary/dictionary_methods.py

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
user = {
2+
"name" : "john",
3+
"age": 40,
4+
"password": "pass123"
5+
}
6+
7+
# copy() : Returns a copy of the dictionary
8+
user_copy = user.copy()
9+
print(user_copy)
10+
11+
# clear() : Removes all the elements from the dictionary
12+
user_copy.clear()
13+
print(user_copy)
14+
15+
# fromkeys() Returns a dictionary with the specified keys and value
16+
keys = ('name','age','password')
17+
val = None
18+
19+
user_dict = dict.fromkeys(keys,val)
20+
print(user_dict)
21+
22+
# or
23+
24+
keys = ('name','age','password')
25+
user_dict = dict.fromkeys(keys)
26+
print(user_dict)
27+
28+
# get() Returns the value of the specified key
29+
print(f"name : {user.get('name')}")
30+
print(f"age : {user.get('age')}")
31+
32+
33+
# items() Returns a list containing a tuple for each key value
34+
for key,val in user.items():
35+
print(f"{key} : {val}")
36+
37+
38+
# keys() Returns a list containing the dictionary's keys
39+
print("printing all key")
40+
for key in user.keys():
41+
print(key)
42+
43+
44+
# pop() Removes the element with the specified key
45+
print("using pop() : ")
46+
print(user_dict)
47+
user_dict.pop("age")
48+
print(user_dict)
49+
50+
51+
# popitem() Removes the last inserted key-value pair
52+
print("using popitem() : ")
53+
print(user_dict)
54+
user_dict.popitem()
55+
print(user_dict)
56+
57+
58+
# setdefault() Returns the value of the specified key. If the key does not exist: insert the key, with the specified value
59+
user.setdefault("is_admin",False)
60+
print(user)
61+
62+
# update() Updates the dictionary with the specified key-value pairs
63+
# uodate existing value
64+
user.update({"is_admin":True})
65+
print(user)
66+
# help in adding new item
67+
user.update({"is_logged_in":True})
68+
print(user)
69+
70+
71+
# values() Returns a list of all the values in the dictionary
72+
73+
for val in user.values():
74+
print(val)

Dictionary/dictionary_operations.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
employees_db = {
2+
"101": "Rajesh Kumar",
3+
"102": "Sita Patel",
4+
"103": "Amit Sharma",
5+
"104": "Priya Nair",
6+
"105": "Vikram Singh",
7+
"106": "Neha Gupta",
8+
"107": "Arjun Mehta",
9+
"108": "Anjali Rao"
10+
}
11+
# access item
12+
# access item with its key
13+
print(employees_db["101"])
14+
15+
# change value
16+
employees_db["101"] = "Raj kumar"
17+
print(employees_db["101"])
18+
19+
employees_db.update({"101":"Rajesh kumar"})
20+
print(employees_db["101"])
21+
22+
23+
# add item
24+
employees_db["109"] = "Muskan kumari"
25+
print(employees_db["109"])
26+
27+
employees_db.update({"110":"Puja kumari"})
28+
print(employees_db["110"])
29+
30+
# Remove item
31+
employees_db.pop("110") # remove user with id 110
32+
employees_db.popitem() # remove user with id 109 or last one
33+
34+
print(employees_db)

Dictionary/nested_dictionary.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
last_login= {
2+
"01-01-2000": {
3+
"location": "london",
4+
"time": "5pm",
5+
"session_time": "45m"
6+
},
7+
"02-01-2000":{
8+
"location": "london",
9+
"time": "8pm",
10+
"session_time": "15m"
11+
},
12+
}
13+
14+
print(last_login["01-01-2000"]["session_time"])
15+
16+
for key,val in last_login.items():
17+
print(f"Date ( {key} ) : {val}")
18+
19+
print("-"*32)
20+
21+
for key1,val1 in val.items():
22+
print(f"{key1} : {val1}")
23+
24+
print("-"*32)
25+

0 commit comments

Comments
 (0)