0% found this document useful (0 votes)
22 views2 pages

Python Dictionary Notes BCA

Uploaded by

saumyakasera22
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)
22 views2 pages

Python Dictionary Notes BCA

Uploaded by

saumyakasera22
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
You are on page 1/ 2

Python Dictionary - BCA 1st Semester Notes

Definition: A Dictionary in Python is a collection of key–value pairs. Each key is unique and is used
to access its corresponding value. It is unordered, mutable, indexed and written in curly braces {}.

Syntax & Example:


student = {
"name": "Rahul",
"roll_no": 101,
"course": "BCA",
"marks": 85
}

print(student)
print(student["name"])
print(student.get("marks"))
Output:
{'name': 'Rahul', 'roll_no': 101, 'course': 'BCA', 'marks': 85}
Rahul
85

Types of Dictionary:
1. Empty Dictionary → d = {}
2. Dictionary with Integer Keys → {1: "Apple", 2: "Banana"}
3. Dictionary with Mixed Keys → {"id": 101, "name": "Shreya", 10: "Ten"}
4. Nested Dictionary → {"name": "Aman", "marks": {"Math":90,"CS":95}}

Dictionary Methods:
Method Description Example Output
keys() Returns all keys student.keys() dict_keys([...])
values() Returns all values student.values() dict_values([...])
items() Returns key-value pairs student.items() dict_items([...])
get(key) Returns value for a key student.get('course') BCA
update() Updates dictionary student.update({'marks':90}) marks=90
pop(key) Removes item by key student.pop('course') Removes 'course':'BCA'
clear() Removes all items student.clear() {}
len() Returns total items len(student) 4

Important Examples:
1. Add new key → student["age"] = 20
2. Loop through dictionary → for k,v in student.items(): print(k,":",v)
3. Check if key exists → if "marks" in student: print("Marks found")

Previous Year Questions (PYQ) & Practice:


1. What is a dictionary in Python? Write syntax with example.
2. Difference between List and Dictionary.
3. Program to create dictionary of 5 students (roll_no: name).
4. Program to count frequency of each character in a string.
5. Create nested dictionary of students and print marks.
6. Store item names & prices in dictionary, update one & delete another.
7. Find maximum and minimum values in dictionary.
8. Merge two dictionaries.

Example Exam Question:


Q: Write a Python program to count the frequency of words in a sentence using dictionary.

sentence = "python is easy and python is powerful"


words = sentence.split()
freq = {}

for word in words:


freq[word] = freq.get(word, 0) + 1

print(freq)

Output:
{'python': 2, 'is': 2, 'easy': 1, 'and': 1, 'powerful': 1}

You might also like