0% found this document useful (0 votes)
3 views6 pages

Python Dictionary Notes

This document provides detailed notes on Python dictionaries, covering their definition, literals, methods for adding and removing elements, accessing and updating values, and traversing dictionaries. Key points include that dictionaries are mutable, unordered collections of unique keys and can store complex data types. The document also highlights advantages and disadvantages of using dictionaries, along with important functions and a conclusion on their utility in data storage.

Uploaded by

morsarika38
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)
3 views6 pages

Python Dictionary Notes

This document provides detailed notes on Python dictionaries, covering their definition, literals, methods for adding and removing elements, accessing and updating values, and traversing dictionaries. Key points include that dictionaries are mutable, unordered collections of unique keys and can store complex data types. The document also highlights advantages and disadvantages of using dictionaries, along with important functions and a conclusion on their utility in data storage.

Uploaded by

morsarika38
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

5/8/26, 4:55 PM Python Dictionary Notes

i want detailed notes on python ( dictionry,its


literals,adding removig key,accessing replacing value,
traversing dictionaries

Here are exam-ready detailed notes on Python Dictionaries (perfect for 5–10 mark
answers, with code examples 👇)

📘 1. Dictionary in Python (Definition)


A Dictionary in Python is a collection of key-value pairs.
Each key is unique
Values can be duplicate
Defined using { }

👉 Example:

student = {
"name": "Sarika",
"age": 20,
"course": "BCA"
}

P th R
📌 Key Points:
Keys → unique (no duplicates)
Values → can repeat
Mutable (can change data)
Unordered (no fixed index like list)

📘 2. Dictionary Literals
Dictionary literal means directly creating a dictionary using {}

👉 Syntax:
[Link] 1/6
5/8/26, 4:55 PM Python Dictionary Notes

dict_name = {key1: value1, key2: value2}


Python Run

👉 Example:

emp = {"id": 101, "name": "Ravi", "salary": 25000}


Python Run

👉 Different types:

# Empty dictionary
d = {}

# Mixed data types


d = {"name": "Aman", "age": 21, "marks": [80, 90, 85]}

# Nested dictionary
d = {
"student1": {"name": "A", "age": 20},
"student2": {"name": "B", "age": 21}
}

📘 3. Adding Elements in Dictionary


👉 Method 1: Using new key

d = {"name": "Sarika"}
d["age"] = 20 # adding new key-value
print(d)
Python Run

👉 Output:

{'name': 'Sarika', 'age': 20}

👉 Method 2: Using update()

[Link] 2/6
5/8/26, 4:55 PM Python Dictionary Notes

[Link]({"course": "BCA"})
Python Run

📘 4. Removing Elements from Dictionary


👉 1. pop() → removes specific key

d = {"name": "Sarika", "age": 20}


[Link]("age")
print(d)
Python Run

👉 2. popitem() → removes last inserted item

[Link]()
Python Run

👉 3. del keyword

del d["name"]
Python Run

👉 4. clear() → removes all elements

[Link]()
Python Run

📘 5. Accessing Dictionary Values


👉 1. Using key

d = {"name": "Sarika", "age": 20}


print(d["name"])
Python Run
[Link] 3/6
5/8/26, 4:55 PM Python Dictionary Notes

👉 2. Using get() (safe method)

print([Link]("age"))
Python Run

📌 Difference:
d["key"] → error if key not present

get() → returns None (no error)

📘 6. Replacing / Updating Values


👉 Simply assign new value to existing key:

d = {"name": "Sarika", "age": 20}


d["age"] = 21
print(d)
Python Run

👉 Using update():

[Link]({"age": 22})
Python Run

📘 7. Traversing (Looping) Dictionary


👉 1. Loop through keys

for key in d:
print(key)
Python Run

👉 2. Loop through values

[Link] 4/6
5/8/26, 4:55 PM Python Dictionary Notes

for Python
value in [Link](): Run
print(value)

👉 3. Loop through key-value pairs

for key, value in [Link]():


print(key, value)
Python Run

👉 Example:

d = {"name": "Sarika", "age": 20}

for k, v in [Link]():
print(k, ":", v)
Python Run
👉 Output:

name : Sarika
age : 20

📘 8. Important Dictionary Functions (Extra for exams ⭐)

len(d) # number of items


[Link]() # returns keys
[Link]() # returns values
[Link]()
Python # returns key-value pairs Run

📘 9. Advantages of Dictionary
✔ Fast access using keys
✔ Flexible data storage
✔ Can store complex data (nested structures)

[Link] 5/6
5/8/26, 4:55 PM Python Dictionary Notes

📘 10. Disadvantages
❌ No indexing
❌ Keys must be unique
❌ Slightly more memory usage

📌 Final Short Conclusion (Write in exam)


A dictionary in Python is a mutable data structure that stores data in key-value pairs. It
allows efficient data retrieval, updating, and deletion using keys, making it useful for
structured and dynamic data storage.

If you want, I can also give:


✅ Important exam questions from dictionary
✅ Practice programs (very important for passing)
✅ Hinglish revision notes (super short for last day)

ChatGPT Get Plus

[Link] 6/6

You might also like