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

Python Program Dictionary

The document provides a series of Python programs demonstrating basic dictionary operations, including creating a dictionary, accessing values, adding and changing key-value pairs, deleting elements, checking for key existence, counting items, displaying all keys and values, and creating a dictionary using user input. Each program is accompanied by its output for clarity. These examples serve as a practical guide for beginners learning to manipulate dictionaries in Python.

Uploaded by

goralsokhiya
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)
8 views4 pages

Python Program Dictionary

The document provides a series of Python programs demonstrating basic dictionary operations, including creating a dictionary, accessing values, adding and changing key-value pairs, deleting elements, checking for key existence, counting items, displaying all keys and values, and creating a dictionary using user input. Each program is accompanied by its output for clarity. These examples serve as a practical guide for beginners learning to manipulate dictionaries in Python.

Uploaded by

goralsokhiya
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

1.

Create a Dictionary and Display It


Program:
student = {
"Name": "Rahul",
"Class": 9,
"Section": "A"
}
print(student)
Output:
{'Name': 'Rahul', 'Class': 9, 'Section': 'A'}

2. Access Values Using Keys


Program:
student = {
"Name": "Rahul",
"Roll No": 15
}
print(student["Name"])
print(student["Roll No"])
Output:
Rahul
15

3. Add a New Key-Value Pair


Program:
student = {
"Name": "Anita",
"Class": 9
}
student["Section"] = "B"
print(student)
Output:
{'Name': 'Anita', 'Class': 9, 'Section': 'B'}

4. Change the Value of an Existing Key


Program:
student = {
"Name": "Aman",
"Marks": 75
}
student["Marks"] = 85
print(student)
Output:
{'Name': 'Aman', 'Marks': 85}

5. Delete an Element from a Dictionary


Program:
student = {
"Name": "Riya",
"Age": 14
}
del student["Age"]
print(student)
Output:
{'Name': 'Riya'}
6. Check if a Key Exists in a Dictionary
Program:
student = {
"Name": "Karan",
"Class": 9
}
if "Class" in student:
print("Key exists")
Output:
Key exists

7. Find the Number of Items in a Dictionary


Program:
student = {
"Name": "Neha",
"Roll No": 10,
"Section": "A"
}
print(len(student))
Output:
3

8. Display All Keys and Values


Program:
student = {
"Name": "Rohit",
"Marks": 88
}
for key, value in [Link]():
print(key, ":", value)
Output:
Name : Rohit
Marks : 88

9. Create a Dictionary Using User Input


Program:
student = {}
student["Name"] = input("Enter name: ")
student["Class"] = int(input("Enter class: "))
print(student)
Sample Output:
Enter name: Simran
Enter class: 9
{'Name': 'Simran', 'Class': 9}

You might also like