Master Object-Oriented Programming and advanced Python concepts through hands-on challenges.
Level up your Python skills with classes, comprehensions, and functional programming.
__init__ method:class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self.odometer = 0
• class keyword defines a new class (capitalize class names)__init__ is the constructor, called when creating new objectsself refers to the current instance (like "this" in other languages)self.attribute = valuedef get_description(self):
return f"{self.year} {self.make} {self.model}"
• Methods are functions defined inside a classselfdef drive(self, miles):
self.odometer += miles
print(f"Drove {miles} miles. Total: {self.odometer}")my_car = Car("Toyota", "Camry", 2023)
print(my_car.get_description())
my_car.drive(100)__str__ method for printable representation__init___privateclass ElectricCar(Car):super().__init__() to call parent constructorCar created: 2023 Toyota Camry
Description: 2023 Toyota Camry
Drove 100 miles. Total: 100
Drove 50 miles. Total: 150
Current odometer: 150
class Car: (capitalize name)def __init__(self, param1, param2):self.make = makedef method_name(self):my_car = Car("Toyota", "Camry", 2023)my_car.get_description()Review feedback below
# Traditional way:
squares = []
for x in range(10):
squares.append(x ** 2)
# List comprehension (one line!):
squares = [x ** 2 for x in range(10)]
# Result: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
• Format: [expression for item in iterable]# Only squares of even numbers:
even_squares = [x ** 2 for x in range(10) if x % 2 == 0]
# Result: [0, 4, 16, 36, 64]
• Format: [expression for item in iterable if condition]names = ["alice", "bob", "charlie"]
upper_names = [name.upper() for name in names]
# Result: ["ALICE", "BOB", "CHARLIE"]# Flatten a 2D list:
matrix = [[1, 2], [3, 4], [5, 6]]
flat = [num for row in matrix for num in row]
# Result: [1, 2, 3, 4, 5, 6]numbers = [1, 2, 3, 4, 5]
labels = ["even" if x % 2 == 0 else "odd" for x in numbers]
# Result: ["odd", "even", "odd", "even", "odd"]{k: v for k, v in items}{x for x in items}(x for x in items)Squares: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Even squares: [0, 4, 16, 36, 64]
Upper names: ['ALICE', 'BOB', 'CHARLIE']
Filtered: [6, 7, 8, 9, 10]
[expression for item in iterable][x for x in range(10) if x > 5][s.upper() for s in strings][x ** 2 for x in range(10)]["even" if x%2==0 else "odd" for x in nums]Review feedback below
# Regular function:
def square(x):
return x ** 2
# Lambda equivalent (anonymous function):
square = lambda x: x ** 2
print(square(5)) # Output: 25
• Syntax: lambda arguments: expressionlambda x, y: x + ynumbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x ** 2, numbers))
# Result: [1, 4, 9, 16, 25]
• map(function, iterable) applies function to each itemlist() to see resultsnumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
evens = list(filter(lambda x: x % 2 == 0, numbers))
# Result: [2, 4, 6, 8, 10]
• filter(function, iterable) keeps items where function returns Truestudents = [("Alice", 85), ("Bob", 92), ("Charlie", 78)]
# Sort by score (second element):
by_score = sorted(students, key=lambda x: x[1])
# Result: [("Charlie", 78), ("Alice", 85), ("Bob", 92)]
# Sort descending:
by_score_desc = sorted(students, key=lambda x: x[1], reverse=True)from functools import reduce
numbers = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x * y, numbers)
# Result: 120 (1*2*3*4*5)def functionsLambda square: 25
Mapped squares: [1, 4, 9, 16, 25]
Filtered evens: [2, 4, 6, 8, 10]
Sorted by score: [('Charlie', 78), ('Alice', 85), ('Bob', 92)]
lambda x: x ** 2lambda x, y: x + ylist(map(lambda x: x*2, nums))list(filter(lambda x: x>5, nums))sorted(items, key=lambda x: x[1])Review feedback below