-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDict.py
More file actions
55 lines (31 loc) · 1.53 KB
/
Dict.py
File metadata and controls
55 lines (31 loc) · 1.53 KB
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
"""
In Python, a dictionary (often abbreviated as "dict") is a built-in data type used to store collections of key-value pairs. It is an unordered collection of items, and each item consists of a key and its associated value. Dictionaries are also known as associative arrays or hash maps in other programming languages."""
# Creating a Dictionary:
# You can create a dictionary by enclosing a comma-separated list of key-value pairs within curly braces {}. The key and its associated value are separated by a colon :. For
my_dict = {
"name": "Ruchika",
"age": 22,
"city": "Mumbai"
}
# Accessing Values:
# print(my_dict["name"])
# If the key doesn't exist in the dictionary, it will raise a KeyError. To avoid this, you can use the get() method:
# print(my_dict.get("city"))
# Adding or Updating Items:
# my_dict["job"] = "Teacher" # Adding a new key-value pair
# my_dict["age"] = 23 # Updating the value of an existing key
# print(my_dict)
# Removing Items:
# To remove a key-value pair from a dictionary, you can use the del statement or the pop() method:
# del my_dict["city"] # Removing a key-value pair
# my_dict.pop("age") # Removing a key-value pair using pop()
# print(my_dict)
# dict in loop
# for i in my_dict:
# print(i, my_dict[i])
# # Or using the items() method
# for key, value in my_dict.items():
# print(key, value)
# You can use the in keyword to check if a key exists in a dictionary:
if "name" in my_dict:
print("Name:", my_dict["name"])