Python Notes
Dictionary:
# 1. Create an empty dictionary
my_dict = {}
print("1. Empty dictionary:", my_dict)
# 2. Check if a key exists in a dictionary
key_exists = 'key' in my_dict
print("2. Does 'key' exist in dictionary?", key_exists)
# 3. Update the value of an existing key in a dictionary
my_dict['key'] = 'new_value'
print("3. Dictionary after adding/updating 'key':", my_dict)
# 4. Delete a key-value pair from a dictionary
del my_dict['key']
print("4. Dictionary after deleting 'key':", my_dict)
# Adding some key-value pairs for further operations
my_dict['a'] = 1
my_dict['b'] = 2
my_dict['c'] = 3
# 5. Iterate over all key-value pairs in a dictionary
print("5. Iterating over dictionary:")
for key, value in my_dict.items():
print(f' Key: {key}, Value: {value}')
# 6. Get a list of all the keys in a dictionary
keys = list(my_dict.keys())
print("6. List of keys:", keys)
# 7. Get a list of all the values in a dictionary
values = list(my_dict.values())
print("7. List of values:", values)
# 8. Merge two dictionaries
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}
merged_dict = {**dict1, **dict2}
print("8. Merged dictionary:", merged_dict)
# 9. Get a value from a dictionary without causing a KeyError if the key does not exist
value = my_dict.get('nonexistent_key', 'default_value')
print("9. Value of 'nonexistent_key':", value)
# 10. Create a dictionary from two lists: one of keys and one of values
keys = ['d', 'e', 'f']
values = [4, 5, 6]
new_dict = dict(zip(keys, values))
print("10. Dictionary created from two lists:", new_dict)
Lists:
# 1. Initialize a 3x3 2D list (matrix)
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print("1. Initial matrix:", matrix)
# 2. Access the element at the second row and third column
element = matrix[1][2]
print("2. Element at second row, third column:", element)
# 3. Modify the element at the first row, second column
matrix[0][1] = 20
print("3. Matrix after modifying element at first row, second column:", matrix)
# 4. Add a new row to the 2D list
new_row = [10, 11, 12]
matrix.append(new_row)
print("4. Matrix after adding a new row:", matrix)
# 5. Add a new column to the 2D list
for row in matrix:
row.append(0)
print("5. Matrix after adding a new column:", matrix)
# 6. Remove the last row from the 2D list
matrix.pop()
print("6. Matrix after removing the last row:", matrix)
# 7. Remove the last column from the 2D list
for row in matrix:
row.pop()
print("7. Matrix after removing the last column:", matrix)
# 8. Transpose the 2D list
transposed = [[matrix[j][i] for j in range(len(matrix))] for i in range(len(matrix[0]))]
print("8. Transposed matrix:", transposed)
# 9. Flatten the 2D list into a 1D list
flattened = [element for row in matrix for element in row]
print("9. Flattened list:", flattened)
# 10. Find the sum of all elements in the 2D list
total_sum = sum(sum(row) for row in matrix)
print("10. Sum of all elements:", total_sum)
# 11. Find the maximum element in the 2D list
max_element = max(max(row) for row in matrix)
print("11. Maximum element in the matrix:", max_element)
# 12. Find the minimum element in the 2D list
min_element = min(min(row) for row in matrix)
print("12. Minimum element in the matrix:", min_element)
# 13. Sort each row of the 2D list
for row in matrix:
row.sort()
print("13. Matrix after sorting each row:", matrix)
# 14. Reverse each row of the 2D list
for row in matrix:
row.reverse()
print("14. Matrix after reversing each row:", matrix)
# 15. Check if a specific element exists in the 2D list
element_exists = any(5 in row for row in matrix)
print("15. Does the element '5' exist in the matrix?", element_exists)
# 1. Initialize a 3x3 2D list (matrix)
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print("1. Initial matrix:", matrix)
# 2. Access the element at the second row and third column
element = matrix[1][2]
print("2. Element at second row, third column:", element)
# 3. Modify the element at the first row, second column
matrix[0][1] = 20
print("3. Matrix after modifying element at first row, second column:", matrix)
# 4. Add a new row to the 2D list
new_row = [10, 11, 12]
matrix.append(new_row)
print("4. Matrix after adding a new row:", matrix)
# 5. Add a new column to the 2D list
for row in matrix:
row.append(0)
print("5. Matrix after adding a new column:", matrix)
# 6. Remove the last row from the 2D list
matrix.pop()
print("6. Matrix after removing the last row:", matrix)
# 7. Remove the last column from the 2D list
for row in matrix:
row.pop()
print("7. Matrix after removing the last column:", matrix)
# 8. Transpose the 2D list
transposed = []
for i in range(len(matrix[0])):
new_row = []
for j in range(len(matrix)):
new_row.append(matrix[j][i])
transposed.append(new_row)
print("8. Transposed matrix:", transposed)
# 9. Flatten the 2D list into a 1D list
flattened = []
for row in matrix:
for element in row:
flattened.append(element)
print("9. Flattened list:", flattened)
# 10. Find the sum of all elements in the 2D list
total_sum = 0
for row in matrix:
for element in row:
total_sum += element
print("10. Sum of all elements:", total_sum)
# 11. Find the maximum element in the 2D list
max_element = matrix[0][0]
for row in matrix:
for element in row:
if element > max_element:
max_element = element
print("11. Maximum element in the matrix:", max_element)
# 12. Find the minimum element in the 2D list
min_element = matrix[0][0]
for row in matrix:
for element in row:
if element < min_element:
min_element = element
print("12. Minimum element in the matrix:", min_element)
# 13. Sort each row of the 2D list
for row in matrix:
row.sort()
print("13. Matrix after sorting each row:", matrix)
# 14. Reverse each row of the 2D list
for row in matrix:
row.reverse()
print("14. Matrix after reversing each row:", matrix)
# 15. Check if a specific element exists in the 2D list
element_exists = False
for row in matrix:
if 5 in row:
element_exists = True
break
print("15. Does the element '5' exist in the matrix?", element_exists)
Sets:
# Initialize two sets
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
print("1. Initial sets:")
print(" Set 1:", set1)
print(" Set 2:", set2)
# 1. Add an element to a set
set1.add(6)
print("2. Set 1 after adding 6:", set1)
# 2. Remove an element from a set
set1.remove(6)
print("3. Set 1 after removing 6:", set1)
# 3. Discard an element from a set (no error if element not found)
set1.discard(10)
print("4. Set 1 after discarding 10 (element not present):", set1)
# 4. Pop an element from a set
popped_element = set1.pop()
print("5. Set 1 after popping an element:", set1)
print(" Popped element:", popped_element)
# 5. Clear all elements from a set
set3 = {9, 10, 11}
set3.clear()
print("6. Set 3 after clearing all elements:", set3)
# 6. Get the length of a set
set_length = len(set1)
print("7. Length of Set 1:", set_length)
# 7. Check if an element exists in a set
element_exists = 3 in set1
print("8. Does 3 exist in Set 1?", element_exists)
# 8. Union of two sets
union_set = set1.union(set2)
print("9. Union of Set 1 and Set 2:", union_set)
# 9. Intersection of two sets
intersection_set = set1.intersection(set2)
print("10. Intersection of Set 1 and Set 2:", intersection_set)
# 10. Difference of two sets
difference_set = set1.difference(set2)
print("11. Difference of Set 1 and Set 2 (Set 1 - Set 2):", difference_set)
# 11. Symmetric difference of two sets
symmetric_difference_set = set1.symmetric_difference(set2)
print("12. Symmetric difference of Set 1 and Set 2:", symmetric_difference_set)
# 12. Check if one set is a subset of another
is_subset = set1.issubset(set2)
print("13. Is Set 1 a subset of Set 2?", is_subset)
# 13. Check if one set is a superset of another
is_superset = set1.issuperset(set2)
print("14. Is Set 1 a superset of Set 2?", is_superset)
# 14. Update a set with the union of itself and another
set1.update(set2)
print("15. Set 1 after updating with union of Set 1 and Set 2:", set1)
# 15. Create a set from a list
list_to_set = set([1, 2, 3, 3, 4, 4, 5])
print("16. Set created from list [1, 2, 3, 3, 4, 4, 5]:", list_to_set)