CBSERanker

Loading

Class Notes: Python Dictionary

Class Notes: Python Dictionary

1. Introduction to Dictionary

  • A dictionary is a collection of key-value pairs in Python.
  • Each key is unique, and it is used to access its corresponding value.
  • Example: student = {"name": "John", "age": 15, "class": 10}

2. Accessing Items in a Dictionary

  • Use the key to get the value from a dictionary.
  • Example: print(student["name"]) # Output: John print(student["age"]) # Output: 15

3. Mutability of a Dictionary

  • Mutability means you can change a dictionary by adding, modifying, or deleting items.
  • Adding a New Item: student["marks"] = 90 print(student) # Output: {'name': 'John', 'age': 15, 'class': 10, 'marks': 90}
  • Modifying an Existing Item: student["age"] = 16 print(student) # Output: {'name': 'John', 'age': 16, 'class': 10, 'marks': 90}

4. Traversing a Dictionary

  • Use a loop to go through all key-value pairs in the dictionary.
  • Example: for key, value in student.items(): print(key, ":", value)

5. Built-in Functions/Methods in Dictionaries

  • len(): Returns the number of key-value pairs. print(len(student)) # Output: 4
  • dict(): Creates a new dictionary. new_dict = dict(name="Alice", age=14) print(new_dict) # Output: {'name': 'Alice', 'age': 14}
  • keys(): Returns all keys in the dictionary. print(student.keys()) # Output: dict_keys(['name', 'age', 'class', 'marks'])
  • values(): Returns all values in the dictionary. print(student.values()) # Output: dict_values(['John', 16, 10, 90])
  • items(): Returns all key-value pairs as tuples. print(student.items()) # Output: dict_items([('name', 'John'), ('age', 16), ('class', 10), ('marks', 90)])
  • get(): Returns the value for a key, without raising an error if the key doesn’t exist. print(student.get("name")) # Output: John print(student.get("grade", "Not Found")) # Output: Not Found
  • update(): Adds key-value pairs to the dictionary. student.update({"grade": "A", "section": "B"}) print(student) # Output: {'name': 'John', 'age': 16, 'class': 10, 'marks': 90, 'grade': 'A', 'section': 'B'}
  • del: Deletes a key-value pair. del student["marks"] print(student) # Output: {'name': 'John', 'age': 16, 'class': 10, 'grade': 'A', 'section': 'B'}
  • clear(): Removes all items from the dictionary. student.clear() print(student) # Output: {}
  • fromkeys(): Creates a dictionary from a list of keys with the same value. keys = ["a", "b", "c"] default_dict = dict.fromkeys(keys, 0) print(default_dict) # Output: {'a': 0, 'b': 0, 'c': 0}
  • copy(): Creates a copy of the dictionary. copy_dict = student.copy() print(copy_dict)
  • pop(): Removes an item by key and returns its value. marks = student.pop("marks", "Not Found") print(marks) # Output: 90
  • popitem(): Removes and returns the last key-value pair. last_item = student.popitem() print(last_item)
  • setdefault(): Adds a key-value pair if the key is not in the dictionary. student.setdefault("section", "A") print(student) # Output: {'name': 'John', 'age': 16, 'class': 10, 'section': 'A'}
  • max() and min(): Finds the maximum and minimum keys in the dictionary. num_dict = {"a": 10, "b": 5, "c": 20} print(max(num_dict)) # Output: 'c' print(min(num_dict)) # Output: 'a'
  • sorted(): Returns a sorted list of keys. print(sorted(num_dict)) # Output: ['a', 'b', 'c']

6. Suggested Programs

  1. Count the number of times a character appears in a string: string = "banana" char_count = {} for char in string: char_count[char] = char_count.get(char, 0) + 1 print(char_count) # Output: {'b': 1, 'a': 3, 'n': 2}
  2. Create a dictionary with names of employees and their salaries: employees = { "Alice": 50000, "Bob": 60000, "Charlie": 55000 } print(employees["Alice"]) # Output: 50000

Leave a Reply

Your email address will not be published. Required fields are marked *