What is an object in OOP?
an entity or thing in your program
how do you create objects in Python?
Using classes. Classes define a type (not default types like str, int). Process of creating an object from that class is called instantiation.
True or False - Objects of the same class are independent of each other.
True. The class just tells python how to create the objects.
Write a class named Dog that has no input parameters and all it does is pass.
class Dog:
passWhat are instance attributes?
Attributes that are independent to each object
What value do you pass in for ‘self’ when creating an object?
Nothing. Self is used to refer to the current object being created during instantiation.
What does self mean?
Take the value we are passing in and assign it to the attribute for the new instance.
What are class attributes?
Attributes that are the same for each instance of the class.
What is an instance method?
An instance method is a function that belongs to a class. Just like functions, these methods can accept parameters, and they can access the attributes of the object they belong to.
What is inheritance?
Inheritance is the OOP concept that allows one class to subclass another.
What is overwriting in Python when referring to class attributes?
Overwriting can allow for a class which inherits attributes from a parent, to overwrite one of the attributes.
Can you overwrite / add methods to children classes?
Yes.
What is the difference between dict['key'] and dict.get('key')?
dict[‘key’] raises a KeyError if the key doesn’t exist. dict.get(‘key’) returns None (or a default you specify) if the key doesn’t exist. Use .get() when the key might not be there.
When should you store objects in a dict instead of a list?
When you need to look up objects by a unique identifier. Dicts give O(1) constant-time lookup. Lists require scanning every item O(n). If you have patient_id and need fast retrieval: use a dict keyed by patient_id.
How do you store an object in a dict using one of its attributes as the key?
Access the attribute as the key: self.patients[patient.patient_id] = patient. Now you can retrieve it instantly with self.patients.get('P001').
How do you iterate over key-value pairs in a dict?
Use .items(): for key, value in my_dict.items():. Use .keys() for keys only. Use .values() for values only.
How do you get a value from a dict with a custom default if the key is missing?
Pass the default as the second argument to .get(): my_dict.get('missing_key', 'default_value'). Returns ‘default_value’ instead of None if the key isn’t found.
How do you check if a key exists in a dict?
Use the in operator: if 'patient_id' in my_dict:. Faster and more readable than calling .get() and checking for None.
How do you remove a key-value pair from a dict?
Use del my_dict['key'] — raises KeyError if missing. Or use my_dict.pop('key', None) — returns None instead of crashing if the key doesn’t exist.
What is a dict comprehension?
A concise way to build a dict: {patient.patient_id: patient for patient in patient_list}. Equivalent to a for loop that builds the dict but in one line.
What is the time complexity of dict lookup vs list lookup?
Dict lookup is O(1) — constant time regardless of size. List lookup (scanning for a value) is O(n) — grows with the size of the list. Dicts are backed by a hash table.
What is the difference between a class and an instance?
A class is a blueprint. An instance is an object created from that blueprint. class Patient: is the class. Patient('P001', 'Alice', 45) creates an instance.
What does \_\_init\_\_ do and when does it run?
It runs automatically when you create an instance. It sets up the instance’s initial data (attributes). Think of it as the setup step that runs every time you build a new object.
What is self in a Python class?
A reference to the specific instance the method is being called on. Always the first parameter in instance methods — Python passes it automatically. You never pass it yourself when calling the method.