-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.py
38 lines (26 loc) · 1.28 KB
/
object.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
# In Python, dictionaries are similar to objects in JavaScript. Here are some of the most commonly used methods for manipulating dictionaries in Python:
# The get() method returns the value of a specific key in a dictionary.
my_dict = {"name": "John", "age": 30}
value = my_dict.get("name")
print(value) # Output: John
# The keys() method returns a list of all keys in a dictionary.
my_dict = {"name": "John", "age": 30}
keys = my_dict.keys()
print(keys) # Output: dict_keys(['name', 'age'])
# The values() method returns a list of all values in a dictionary.
my_dict = {"name": "John", "age": 30}
values = my_dict.values()
print(values) # Output: dict_values(['John', 30])
# The items() method returns a list of all key-value pairs in a dictionary.
my_dict = {"name": "John", "age": 30}
items = my_dict.items()
print(items) # Output: dict_items([('name', 'John'), ('age', 30)])
# The update() method updates the value of a specific key in a dictionary.
my_dict = {"name": "John", "age": 30}
my_dict.update({"age": 40})
print(my_dict) # Output: {'name': 'John', 'age': 40}
# The pop() method removes and returns the value of a specific key in a dictionary.
my_dict = {"name": "John", "age": 30}
value = my_dict.pop("age")
print(value) # Output: 30
print(my_dict) # Output: {'name': 'John'}