A summary of the class-based assignments and advanced concepts in this deck.
Create a class called Book. The __init__ method should take title, author, and pages as arguments and store them as attributes.
Then, instantiate two Book objects with the following details:
Finally, print the title of the first book and the author of the second book.
Write your code below and click Run:
Loading Python…Modify the Book class from Assignment 1.
Add a method called get_summary() that returns a string in the following format:
"Title by Author, X pages"
Create a Book object for "The Hobbit" and call the get_summary() method on it. Print the returned string.
Write your code below and click Run:
Loading Python…Model a simple light switch by creating a class called LightSwitch.
__init__, create an attribute is_on and set its default to False.turn_on() that sets is_on to True and prints a confirmation.turn_off() that sets is_on to False and prints a confirmation.get_status() that prints the current state ("ON" or "OFF").Instantiate a LightSwitch object and test all its methods.
Write your code below and click Run:
Loading Python…Model a product for an e-commerce store by creating a class called Product.
__init__ should accept name, price, and quantity_in_stock.calculate_total_value() that returns the price * quantity.sell(amount) that reduces the quantity. Ensure you can't sell more than you have in stock.Create a "Laptop" product (price $1200, quantity 10) and test your methods.
Write your code below and click Run:
Loading Python…Create a class Circle that is initialized with a radius.
__init__ method should store the radius.calculate_area() that returns the area (π * r²).calculate_circumference() that returns the circumference (2 * π * r).You can use 3.14159 for π. Create a Circle with a radius of 5 and print its area and circumference.
Write your code below and click Run:
Loading Python…Add a new method to the Student class from our lecture.
Student class that has name, age, and grade.set_grade(self, new_grade) to update a student's grade.Create a student, update their grade with a valid value, then try to update it with an invalid value to test your logic.
Write your code below and click Run:
Loading Python…Model the relationship between a pet and its owner using two classes.
Pet Class:
__init__ takes name and species.get_info() returns a string like "Fido is a Dog".Owner Class:
__init__ takes owner_name and creates an empty list called pets.add_pet(pet_object) adds a Pet to the list.show_pets() prints info for all pets.Write your code below and click Run:
Loading Python…Imagine you are designing a simple role-playing game. Describe how you would create a Player class. This is a design question; no code is required.
Player object need? (List at least four).Player object be able to perform? (List at least three).Player class demonstrate the concept of Encapsulation?Write your code below and click Run:
Loading Python…How do we prevent users from setting invalid values, like a negative resistance?
Python doesn't have true "private" variables, but uses naming conventions:
class PowerSupply:
def __init__(self, voltage, current_limit):
self._max_voltage = 24.0 # Protected
self.__internal_temp = 25.0 # "Private"
self.set_voltage(voltage) # Use a method for validation
self.current_limit = current_limit
def set_voltage(self, new_voltage):
if 0 <= new_voltage <= self._max_voltage:
self.voltage = new_voltage
print(f"Voltage set to {self.voltage}V")
else:
print(f"Error: Voltage must be 0-{self._max_voltage}V")
psu = PowerSupply(5.0, 1.0)
psu.set_voltage(30.0) # Error: Voltage must be 0-24.0V
# You can still do this, but the underscore warns you not to.
# psu._max_voltage = 50.0
# This will cause an AttributeError due to name mangling.
# print(psu.__internal_temp)
A property is a checkpoint for an attribute. Your code still uses normal dot notation, while the class controls what happens behind the scenes.
r1.resistance@property getter.
r1.resistance = 2200@resistance.setter.
Key idea: Store the value internally as _resistance, so the setter can validate every public assignment.
class Resistor:
def __init__(self, resistance):
self.resistance = resistance # Uses setter
@property
def resistance(self):
return self._resistance
@resistance.setter
def resistance(self, value):
if value < 0:
raise ValueError("Resistance cannot be negative")
self._resistance = value
r1 = Resistor(1000)
print(r1.resistance) # Getter → 1000
r1.resistance = 2200 # Setter validates
print(r1.resistance) # Getter → 2200
# r1.resistance = -500 # Raises ValueError
Attributes can belong to an individual object (instance) or to the entire class.
class Capacitor:
# Class attribute: shared by all Capacitor objects
DIELECTRIC_MATERIAL = "Ceramic"
def __init__(self, capacitance_farads):
# Instance attribute: unique to each capacitor
self.capacitance = capacitance_farads
print(Capacitor.DIELECTRIC_MATERIAL)
c1 = Capacitor(1e-6) # 1 microfarad
c2 = Capacitor(10e-6) # 10 microfarads
# Both instances share the same class attribute
print(f"C1 Material: {c1.DIELECTRIC_MATERIAL}") # Ceramic
print(f"C2 Material: {c2.DIELECTRIC_MATERIAL}") # Ceramic
# But they have unique instance attributes
print(f"C1 Capacitance: {c1.capacitance}F") # 1e-06F
print(f"C2 Capacitance: {c2.capacitance}F") # 1e-05F
A static method doesn't receive the instance (`self`) or the class (`cls`) as an argument. It's essentially a regular function that is namespaced within the class.
Use them for utility functions that are logically related to the class but don't need to access any class or instance data.
class CircuitUtils:
@staticmethod
def parallel_resistance(resistors):
"""Calculate total resistance for resistors in parallel."""
# Note: no 'self' or 'cls' needed
if not resistors: return 0
inverse_sum = sum(1 / r for r in resistors)
return 1 / inverse_sum
@staticmethod
def ohms_law_voltage(i, r):
"""Calculate voltage using Ohm's Law (V=IR)."""
return i * r
# Call the static method directly on the class
r_parallel = [1000, 2000, 1000]
total_r = CircuitUtils.parallel_resistance(r_parallel)
print(f"Total Parallel Resistance: {total_r:.2f} Ω") # 400.00 Ω
voltage = CircuitUtils.ohms_law_voltage(i=0.05, r=100) # 5A * 100Ω
print(f"Voltage: {voltage}V") # 5.0V
Special methods, or "dunder" (double underscore) methods, let your objects integrate with Python's built-in behavior.
class Resistor:
def __init__(self, resistance_ohms):
self.resistance = resistance_ohms
def __str__(self):
# User-friendly output
if self.resistance >= 1e6:
return f"{self.resistance/1e6:.1f} MΩ Resistor"
if self.resistance >= 1e3:
return f"{self.resistance/1e3:.1f} kΩ Resistor"
return f"{self.resistance} Ω Resistor"
r_kilo = Resistor(2200)
r_mega = Resistor(4700000)
print(r_kilo) # Calls __str__: 2.2 kΩ Resistor
print(str(r_mega)) # Calls __str__: 4.7 MΩ Resistor
You can define how standard operators like `+`, `-`, `*` work on your objects by implementing their corresponding dunder methods.
This can make your code incredibly intuitive. For example, what should `resistor1 + resistor2` mean? It could represent connecting them in series!
class Resistor:
def __init__(self, resistance):
self.resistance = resistance
def __add__(self, other):
"""Overloads the + operator for series connection."""
if not isinstance(other, Resistor):
return NotImplemented
# Resistors in series: R_total = R1 + R2
new_resistance = self.resistance + other.resistance
return Resistor(new_resistance)
r1 = Resistor(1000)
r2 = Resistor(2200)
# This now calls the __add__ method we defined!
series_r = r1 + r2
print(f"R1: {r1}") # Resistor(1000)
print(f"R2: {r2}") # Resistor(2200)
print(f"R1 and R2 in series: {series_r}") # Resistor(3200)
You can make your objects behave like lists or dictionaries.
Let's model a digital signal as a collection of samples.
class DigitalSignal:
def __init__(self, samples):
# samples is a list of 0s and 1s
self._samples = list(samples)
def __len__(self):
"""Return the number of samples in the signal."""
return len(self._samples)
def __getitem__(self, index):
"""Allow accessing samples by index."""
return self._samples[index]
# A simple clock signal
clk_signal = DigitalSignal([0, 1, 0, 1, 0, 1, 0, 1])
print(f"Signal has {len(clk_signal)} samples.") # Calls __len__
print(f"Sample at index 3 is: {clk_signal[3]}") # Calls __getitem__
# We can even iterate over it because it has __getitem__!
for sample in clk_signal:
print(sample, end=' ')
A workshop sensor must reject impossible readings before they reach the control system.
-40°C to 125°C are accepted.self._temperature from the getter.ValueError when the value is outside the sensor range.72 to 150. Does the safety check work?✓ Target output: Motor sensor: 72°C — HOT
Complete the circuit code, then run it:
Loading Python…Make Python’s + operator behave like connecting two resistors end-to-end.
__add__ so it returns a new Resistor whose resistance is the sum of both components.__str__ to show a readable Ω value.470 Ω resistor to the chain.✓ Target output: Series network: 3200 Ω
Wire the missing methods, then run:
Loading Python…An oscilloscope captured a stream of zeros and ones. Turn it into a Python object that behaves like a sequence.
0 → 1 transition.len(signal) should report the sample count.signal[index] should return one sample.rising_edges() should count rising transitions.✓ Target output: 8 samples, sample[3] = 0, 3 rising edges
Decode the capture, then run:
Loading Python…