def — Easy Examples
Defines a new function or method
Define a function
Creating and calling a simple function.
python
def greet(name): return f"Hello, {name}!" message = greet("Python") print(message) def add(a, b): return a + b print(add(3, 4))
Expected Output
Hello, Python! 7
def creates a named function. The return statement sends a value back to the caller.
Functions with default parameters
Using default values for optional arguments.
python
def power(base, exponent=2): return base ** exponent print(power(5)) print(power(5, 3)) print(power(2, 10))
Expected Output
25 125 1024
Default parameters let callers omit arguments. Parameters with defaults must come after those without.
Want to try these examples interactively?
Open Easy Playground