Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e9f3c18

Browse files
author
Rai Muhammad Haider
committed
Dictionary
1 parent 757a4bd commit e9f3c18

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

‎Dictionary.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Creating a dictionary
2+
student = {
3+
'name': 'Ali Haider',
4+
'age': 21,
5+
'class': 'BSCS'
6+
}
7+
8+
# Accessing values using keys
9+
print(student)
10+
print(student['name']) # Access value directly by key
11+
print(student.get('name')) # Access value safely using get()
12+
13+
# Updating a value
14+
student['age'] = 22
15+
print(student)
16+
17+
# Looping through dictionary keys and values
18+
for key in student:
19+
print(key, student[key]) # Traditional loop through keys
20+
21+
print('Another method we have:')
22+
for key, value in student.items(): # Recommended way to get both key and value
23+
print(key, value)
24+
25+
# Checking for key existence
26+
if 'mango' in student:
27+
print('Yes, the key "mango" is available') # This will not print, 'mango' not in student
28+
29+
# Dictionary length
30+
print(len(student)) # Total key-value pairs
31+
32+
# Adding a new key-value pair
33+
student['address'] = 'Lahore, Pakistan'
34+
print(student)
35+
36+
# Removing key-value pairs
37+
student.pop('address') # Remove specific key
38+
print(student)
39+
40+
student.popitem() # Remove the most recently added key-value pair
41+
print(student)
42+
43+
del student['name'] # Delete a key-value pair using del
44+
print(student)
45+
46+
# Copying a dictionary
47+
college = student.copy() # Creates a new independent copy
48+
print(college)
49+
50+
# Modifying the copied dictionary
51+
college['location'] = 'Lahore'
52+
print(college) # New key added in copied dictionary
53+
print(student) # Original remains unchanged
54+
55+
# Nested dictionary example
56+
Pakistan = {
57+
'punjab': {
58+
'kasur': {
59+
'pattoki': 'Pattoki',
60+
'habib abad': 'Habib Abad',
61+
'jambar': 'Jamber'
62+
},
63+
'lahore': {
64+
'kanchi': 'Kanchi',
65+
'chungi': 'Chungi',
66+
}
67+
},
68+
'sindh': {
69+
'multan': 'Multan',
70+
'karachi': 'Karachi'
71+
}
72+
}
73+
74+
# Accessing nested dictionary values
75+
print(Pakistan['punjab']['lahore']['kanchi'])
76+
77+
# Dictionary comprehension
78+
square_numbers = {x: x**2 for x in range(10)}
79+
print(square_numbers)
80+
81+
# Clearing a dictionary
82+
square_numbers.clear()
83+
print(square_numbers)
84+
85+
# Creating a dictionary using fromkeys()
86+
default_value = 'taste is very good'
87+
dinner = ['tea', 'patato chips', 'burger']
88+
new_dict = dict.fromkeys(dinner, default_value)
89+
print(new_dict)
90+
91+
# Using setdefault() to add a new key with a default value
92+
default = new_dict.setdefault('age', '21') # Only adds if 'age' doesn't exist
93+
print(new_dict)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /