0% found this document useful (0 votes)
11 views4 pages

Class 11 Topic - Python Dictionary

The document provides a comprehensive overview of Python dictionaries, detailing various built-in methods such as len(), dict(), keys(), values(), and others. Each method is illustrated with example code and expected output, demonstrating how to manipulate and interact with dictionary data structures. Key functionalities include retrieving keys/values, updating entries, and performing operations like pop and clear.

Uploaded by

drishaanpaliwal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Class 11 Topic - Python Dictionary

The document provides a comprehensive overview of Python dictionaries, detailing various built-in methods such as len(), dict(), keys(), values(), and others. Each method is illustrated with example code and expected output, demonstrating how to manipulate and interact with dictionary data structures. Key functionalities include retrieving keys/values, updating entries, and performing operations like pop and clear.

Uploaded by

drishaanpaliwal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Class 11 Topic : Python Dictionary

[Link]()
my_dict = {'a': 1, 'b': 2, 'c': 3}
print(len(my_dict))
# Output: 3 (number of key-value pairs)

2. dict()
my_dict = dict(a=1, b=2, c=3)
print(my_dict)
# Output: {'a': 1, 'b': 2, 'c': 3}

3. keys()
my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict.keys())
# Output: dict_keys(['a', 'b', 'c'])

4. values()
my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict.values())
# Output: dict_values([1, 2, 3])

5. items()
my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict.items())
# Output: dict_items([('a', 1), ('b', 2), ('c', 3)])

6. get()
my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict.get('b'))
# Output: 2
print(my_dict.get('z'))
# Output: None (key doesn't exist)

7. update()
my_dict = {'a': 1, 'b': 2}
my_dict.update({'b': 3, 'c': 4})
print(my_dict)
# Output: {'a': 1, 'b': 3, 'c': 4}

8. del
my_dict = {'a': 1, 'b': 2, 'c': 3}
del my_dict['b']
print(my_dict)
# Output: {'a': 1, 'c': 3}

9. clear()
my_dict = {'a': 1, 'b': 2, 'c': 3}
my_dict.clear()
print(my_dict)
# Output: {}

10. fromkeys()
keys = ['a', 'b', 'c']
value = 0
my_dict = [Link](keys, value)
print(my_dict)
# Output: {'a': 0, 'b': 0, 'c': 0}

11. copy()
my_dict = {'a': 1, 'b': 2, 'c': 3}
new_dict = my_dict.copy()
print(new_dict)
# Output: {'a': 1, 'b': 2, 'c': 3}

12. pop()
my_dict = {'a': 1, 'b': 2, 'c': 3}
value = my_dict.pop('b')
print(value)
# Output: 2
print(my_dict)
# Output: {'a': 1, 'c': 3}

13. popitem()
my_dict = {'a': 1, 'b': 2, 'c': 3}
key, value = my_dict.popitem()
print(key, value)
# Output: (key, value) such as ('c', 3)
print(my_dict)
# Output: {'a': 1, 'b': 2}

14. setdefault()
my_dict = {'a': 1, 'b': 2}
value = my_dict.setdefault('c', 3)
print(value)
# Output: 3 (sets 'c' to 3)
print(my_dict)
# Output: {'a': 1, 'b': 2, 'c': 3}

15. max()
my_dict = {'a': 1, 'b': 2, 'c': 3}
print(max(my_dict, key=my_dict.get))
# Output: 'c' (key with the maximum value)

16. min()
my_dict = {'a': 1, 'b': 2, 'c': 3}
print(min(my_dict, key=my_dict.get))
# Output: 'a' (key with the minimum value)

17. sorted()
my_dict = {'a': 3, 'b': 1, 'c': 2}
sorted_keys = sorted(my_dict)
print(sorted_keys)
# Output: ['a', 'b', 'c'] (sorted by keys)

You might also like