ITDarasgah Certified Python Developer
📘 Lecture 8: Dictionaries and Sets
📌 لیکچر کے مقاصد
اس لیکچر کے اختتام پر آپ:
Dictionaries اور Sets کا مقصد اور استعمال سمجھیں گے
Key–Value data کے ساتھ کام کرنا سیکھیں گے
Dictionaries میں add, update, delete کر سکیں گے
Sets کے ذریعے unique data handle کریں گے
🔹 Dictionary کیا ہوتی ہے؟
Dictionary ایک data structure ہے جو key : value کی صورت میں data محفوظ کرتی ہے۔
student = {
"name": "Ali",
"age": 20,
"city": "Lahore"
}
print(student)
📌 Dictionary mutable ہوتی ہے۔
🔹 Dictionary سے Data Access کرنا
print(student["name"])
print(student.get("age"))
📌 get() استعمال کرنے سے error کا خطرہ کم ہوتا ہے۔
🔹 Dictionary میں Data Add / Update کرنا
student["grade"] = "A"
student["age"] = 21
🔹 Dictionary سے Data Delete کرنا
del student["city"]
student.pop("age")
🔹 Dictionary Looping
for key in student:
print(key, student[key])
for key, value in student.items():
print(key, value)
🔹 Nested Dictionary (Basic)
students = {
"Ali": {"age": 20, "marks": 85},
"Sara": {"age": 19, "marks": 90}
}
print(students["Ali"]["marks"])
🔹 Set کیا ہوتا ہے؟
Set ایک unordered collection ہوتی ہے جس میں duplicate values نہیں ہوتیں۔
numbers = {1, 2, 3, 3, 4}
print(numbers)
🔹 Set Operations
numbers.add(5)
numbers.remove(2)
a = {1, 2, 3}
b = {3, 4, 5}
print(a | b) # Union
print(a & b) # Intersection
print(a - b) # Difference
🔹 Dictionary vs Set (فرق)
| خصوصیت | Dictionary | Set |
|---|---|---|
| Structure | Key : Value | Values Only |
| Duplicate | Keys نہیں | Values نہیں |
| Use Case | Records / Profiles | Unique lists |
📝 Assignment – Lecture 8
🔸 Mini Project: Contact Book Program
Program کی خصوصیات:
contact name کو key بنائیں
phone number کو value
add, update, delete contact
تمام contacts display کریں
📌 Bonus:
duplicate numbers روکنے کے لیے set استعمال کریں
📌 Assignment فورم پر code block میں پوسٹ کریں۔
💬 Discussion Prompt
Dictionary list سے زیادہ powerful کیوں ہے؟
Set کہاں زیادہ مؤثر ثابت ہوتا ہے؟
📌 اگلا لیکچر: File Handling

کوئی تبصرے نہیں:
ایک تبصرہ شائع کریں