table of contents
1. Data Types in Python
2. Numeric Types: Integer, Float, Complex
3. Sequence Types: String, List, Tuple
4. Set and Dictionary
5. Understanding Indexing
6. Comparing and Merging Sets
7. Dictionaries and Key-Value Pair
8. Modifying Key-Value Pairs in Dictionaries
9. Deleting Key-Value Pairs from Dictionaries
10. Managing Data Types in Python
,Data Types in Python - Dictionaries
Modifying Key-Value Pairs in Dictionaries
To modify a value in a dictionary, you can simply assign a new
value to the key:
my_dict = {'key': 'value'}
my_dict['key'] = 'new value'
To add a new key-value pair to a dictionary, you can simply assign
a value to a new key:
my_dict = {'key': 'value'}
my_dict['new_key'] = 'new value'
To update the value of a key-value pair in a dictionary using
another dictionary, you can use the update() method:
, dict1 = {'key1': 'value1'}
dict2 = {'key2': 'value2'}
dict1.update(dict2)
Deleting Key-Value Pairs from Dictionaries
To delete a key-value pair from a dictionary, you can use
the del keyword:
my_dict = {'key': 'value'}
del my_dict['key']
To delete all the key-value pairs from a dictionary, you can use
the clear() method:
my_dict = {'key': 'value'}
my_dict.clear()
Understanding Indexing
Indexing in dictionaries is different from indexing in lists or strings.
Dictionaries are unordered collections of key-value pairs, and
therefore do not have a defined order.
However, you can still access the value of a key-value pair by using
the key inside square brackets:
my_dict = {'key': 'value'}
print(my_dict['key'])