|
| 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