Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Dictionaries and lists are similar to other sequences list and tuples. Go through the concepts lists and refer to the book if you need more details, focus on OOP.

Reading - Dictionaries and Sets

(1-2h)

Reading PDF

Concepts:

  • 9.1 dictionary

    • 12
    • Is it in the dictionary?

      True
    • True
    • Adding

      {'İstanbul': 20, 'København': 12, 'Zürich': 20}
    • Deleting

      {'İstanbul': 20, 'København': 12}
    • Keys can be any immutable value, values can be anything

    • Iteration

      İstanbul
      København
      İstanbul 20
      København 12
      
    • clear, get, items, keys, pop, popitem, values

    • Example: deck of cards deck = {"Ace of Spades": 1, ...}

    • Example: name -> birthday

    • Merging

      {'Ata': 24, 'Dilek': 25, 'Su': 33}
    • Comprehension

      {'DILEK'}
  • 9.2 Sets

    • Create

      {'playing music', 'programming', 'tango'}
    • Add

      {'playing music', 'programming', 'tango'}
    • Updating

      {'playing music', 'programming', 'salsa', 'tango', 'watching series'}
    • Removing vs discarding

      ---------------------------------------------------------------------------
      KeyError                                  Traceback (most recent call last)
      Cell In[13], line 2
            1 activity_ideas.remove("salsa")
      ----> 2 activity_ideas.remove("salsa")
            3 activity_ideas
      
      KeyError: 'salsa'
      {'playing music', 'programming', 'tango'}
    • Iteration, in, not in same as list

    • Union

      {'playing music', 'programming', 'salsa', 'tango', 'watching series'}
    • Difference

      {'salsa', 'watching series'}
    • Intersection

      {'playing music'}
    • Symmetric difference ^

    • issubset, issuperset, <=, >=

    • set comprehensions similar to dict

  • 9.3 Serializing objects

    • pickle (and json) can save Python objects as files. This is also called serialization.

    • Example: take a snapshot of an object, exit the program, and continue the program beginning at the last snapshot.

    • Analogy to pickling 🏺🥒: We pickle food to conserve it for a longer time, then unpickle it before eating.

Reading - Classes and object-oriented programming

(2-3h)

Reading PDF

Concepts:

  • 9.1 object-oriented programming (OOP)

    • Advantage: reusability

    • Example: Alarm clock

      • private methods

      • public

    • private vs public methods

      • sound_alarm(): private because we don’t want that the user can make the alarm sound

      • set_alarm_time(): for the user

  • 9.2 Classes

    • class is a blueprint

    • many objects are created from a class; each is also called an instance

    • Definition

    • Instantiation

    • Usage

      Rex says Woof!
      Happy birthday, Fido! Now 6 years old.
      Rex
      
    • the book uses get_* methods. Do not do this. Use data directly, e.g., rex.name.[1]

    • This is advanced. var vs _var vs __var: For now, use just var

      0

      _var hides but still reachable:

      0

      __var really hides:

      ---------------------------------------------------------------------------
      AttributeError                            Traceback (most recent call last)
      Cell In[23], line 1
      ----> 1 c.__var
      
      AttributeError: 'C' object has no attribute '__var'
    • Storing classes in modules: similar to functions

    • Example BankAccount

    • __str__ for printing a class object

  • 10.3 Working with instances

    • Multiple instances

    • Accessor and mutator methods (a.k.a getter and setters): As I wrote before, avoid them.

    • class objects can be passed as function arguments like other objects

    • Pickling your objects: We will use databases to store process data, but pickling can be useful for storing GUI state.

  • 10.4 Techniques for designing classes

    • class diagram part of Unified Modeling Language.

    • Our Dog class example:

    • Finding the classes in a problem. (What are the nouns in the problem?)

    • Refining (some nouns have the same meaning, others we don’t need to be concerned about)

    • Identifying classes responsibilities

  • If a class has some instances of another class: Object composition#Aggregation

Footnotes
  1. getter and setter (or accessor and mutator) functions are recommended in OOP, but leads to too much code that does nothing. If needed later, they can be added as properties