Functions and Classes
Functions
Section titled “Functions”def greet(name): return f"Hello, {name}"
def add(a, b): return a + bOptional default arguments:
def say(msg, prefix=">"): return f"{prefix} {msg}"Classes
Section titled “Classes”class Person: def __init__(self, name, age): self.name = name self.age = age
def greet(self): return f"Hi, I'm {self.name}"
p = Person("Alice", 30)p.greet() # "Hi, I'm Alice"__init__is the constructor;selfis the instance.- Methods take
selfas the first parameter. You can nest data (attributes) and behavior (methods) in a class.