Dictionaries in Python
Python Dictionaries
What are Dictionaries in Python?
Dictionaries in Python means a dictionary stores data in key-value pairs. It is useful for records, JSON-like data and fast lookups.
In real programs, this topic helps in storing student records. Learn the idea first, then type the program yourself and compare the output.
| Point | Details |
|---|---|
| Course Area | Core Python Basic programming concepts used to write Python programs. |
| Main Use | storing student records |
| Example File | dictionaries.py |
| Practice Focus | Run, change values, and explain the output line by line. |
Why should you learn this?
- It is useful for storing student records.
- It connects with working with JSON-like data.
- It improves your ability to read, write and debug Python programs.
Important Terms
These terms are used directly in this lesson. Understand them before memorising the code.
| Term | Meaning |
|---|---|
| key | Identifier used to access a value in a dictionary. |
| value | Data stored against a key in a dictionary. |
| dict | Dictionary data type used for key-value records. |
| get() | Dictionary method that safely returns a value or default. |
| items() | Dictionary method that returns key-value pairs. |
Syntax / Basic Pattern
The simple pattern is: prepare data, apply the concept, then show the result.
student = {
"name": "Anaya",
"class": "XI",
"marks": 92
}
print(student["name"])
print(student.get("section", "Not assigned"))
for key, value in student.items():Complete Example Program
student = {
"name": "Anaya",
"class": "XI",
"marks": 92
}
print(student["name"])
print(student.get("section", "Not assigned"))
for key, value in student.items():
print(key, "=>", value)Expected Output
Program Explanation
student = {stores a value in student."name": "Anaya",performs the next step of the program logic."class": "XI",performs the next step of the program logic."marks": 92performs the next step of the program logic.}performs the next step of the program logic.print(student["name"])displays information or calculated result on the screen.print(student.get("section", "Not assigned"))displays information or calculated result on the screen.
Where will you use it?
- Storing student records.
- Working with json-like data.
- Finding values by key quickly.
Common Mistakes
- Writing code with wrong indentation.
- Using input() value directly in calculations without converting it to int or float.
- Using unclear variable names that make the program difficult to understand.
Practice Tasks
- Type the program in
dictionaries.pyand run it. - Change input values or sample data and observe the new output.
- Create one example related to storing student records.
- Write 5 lines explaining the logic in your own words.
Summary
Dictionaries in Python is not a theory-only topic. You should be able to explain the meaning, write the example, run it successfully, and use it in a small practical program.
Python Dictionaries क्या है?
Python Dictionaries ka matlab hai: A dictionary stores data in key-value pairs. It is useful for records, JSON-like data and fast lookups. Simple words me, ye topic practical Python programs likhne me direct use hota hai.
Is topic ko sirf definition ke liye nahi, balki storing student records jaise real examples ke liye practice karein.
यह क्यों सीखना जरूरी है?
- Ye storing student records me kaam aata hai.
- Ye working with JSON-like data se bhi connected hai.
- Isse aap code ka output aur errors better samajh paate hain.
Important Terms
| Term | Meaning |
|---|---|
| key | Identifier used to access a value in a dictionary. |
| value | Data stored against a key in a dictionary. |
| dict | Dictionary data type used for key-value records. |
| get() | Dictionary method that safely returns a value or default. |
| items() | Dictionary method that returns key-value pairs. |
Syntax / Basic Pattern
Basic idea: pehle data तैयार करें, phir Python logic apply करें, aur finally result display करें.
student = {
"name": "Anaya",
"class": "XI",
"marks": 92
}
print(student["name"])
print(student.get("section", "Not assigned"))
for key, value in student.items():Complete Example Program
student = {
"name": "Anaya",
"class": "XI",
"marks": 92
}
print(student["name"])
print(student.get("section", "Not assigned"))
for key, value in student.items():
print(key, "=>", value)Expected Output
Program Explanation
student = {stores a value in student."name": "Anaya",performs the next step of the program logic."class": "XI",performs the next step of the program logic."marks": 92performs the next step of the program logic.}performs the next step of the program logic.print(student["name"])displays information or calculated result on the screen.print(student.get("section", "Not assigned"))displays information or calculated result on the screen.
Practical Uses
- Storing student records.
- Working with json-like data.
- Finding values by key quickly.
Common Mistakes
- Writing code with wrong indentation.
- Using input() value directly in calculations without converting it to int or float.
- Using unclear variable names that make the program difficult to understand.
Practice Tasks
- Program ko
dictionaries.pyfile me type karke run karein. - Values change karke output compare karein.
- storing student records par ek छोटा example banayen.
- Logic ko apne words me 5 lines me likhein.
सारांश
Dictionaries in Python ko tab complete maanenge jab aap iska meaning, example, output aur practical use clearly explain kar saken.