Dictionaries and Sets Review

📑Dictionary

Dictionaries are built-in data structures that store collections of key-value pairs.

dictionary = {
    key1: value1,
    key2: value2
}
  • To access the value of a key-value pair: dictionary[key]
  • The get() method retrieves the value associated with a key: dictionary.get(key, default)
  • The keys() and values() methods return a view object with all the keys and values in the dictionary, respectively.
    pizza = {
      'name': 'Margherita Pizza',
      'price': 8.9,
      'calories_per_slice': 250
      }
    
     pizza.keys()
     # dict_keys(['name', 'price', 'calories_per_slice'])
    
     pizza.values()
     # dict_values(['Margherita Pizza', 8.9, 250])
    
  • The items() method returns a view object with all the key-value pairs in the dictionary, including both the keys and the values.
    pizza.items()
    # dict_items([('name', 'Margherita Pizza'), ('price', 8.9), ('calories_per_slice', 250)])
    
  • If you need to iterate over the values in a dictionary, you can write a for loop with values() to get all the values of a dictionary.
    products = {
        'Laptop': 990,
        'Smartphone': 600,
        'Tablet': 250,
        'Headphones': 70,
    }
    
    for price in products.values():
        print(price)
    # 990
      600
      250
      70
    
  • Iterating Over Keys: If you need to iterate over the keys in the products dictionary above, you can write products.keys() or products directly.
  • Iterating Over Key-Value Pairs: If you need to iterate over the keys and their corresponding values simultaneously, you can iterate over products.items().
  • enumerate() Function: If you need to iterate over a dictionary while keeping track of a counter, you can call the enumerate() function.

🧦Set

Sets are built-in data structures in Python that do not allow duplicate values.

  • Defining a Set: my_set = {1, 2, 3, 4, 5}
  • If you need to define an empty set, you must use the set() function.
  • The issubset() and the issuperset() methods check if a set is a subset or superset of another set, respectively.
    my_set = {1, 2, 3, 4, 5}
    your_set = {2, 3, 4, 5}
    
    print(your_set.issubset(my_set)) # True
    print(my_set.issuperset(your_set)) # True
    
  • The isdisjoint() method checks if two sets are disjoint, if they don’t have elements in common.
  • The union operator | returns a new set with all the elements from both sets.
    my_set = {1, 2, 3}
    your_set = {4, 5, 6}
    
    my_set | your_set # {1, 2, 3, 4, 5, 6}
    
  • The intersection operator & returns a new set with only the elements that the sets have in common.
  • The difference operator - returns a new set with the elements of the first set that are not in the other sets.
  • The symmetric difference operator ^ returns a new set with the elements that are either in the first or the second set, but not both.
  • check if an element is in a set or not with the in operator: print(5 in my_set) # True

📚Python Standard Library

A library gives you pre-written and reusable code, like functions, classes, and data structures, that you can reuse in your projects.

  • use the import keyword followed by the name of the module: import module_name
  • if you need to call a method from that module, you would use dot notation, with the name of the module followed by the name of the method. module_name.method_name()
  • you can use as followed by the alias at the end of the import statement. import module_name as module_alias

Test 18/20


<
Previous Post
0302 This isn’t where I should stay
>
Next Post
0303 Actually feeling better