Explore Functions
Explore Functions
def greet(name):
print(power(3)) # Output: 9
def add(*args):
return sum(args)
def print_details(**kwargs):
print(f"{key}: {value}")
print_details(name="Alice", age=30)
# Output:
# name: Alice
# age: 30
# Example: Lambda function
square = lambda x: x ** 2
print(square(4)) # Output: 16
nums = [1, 2, 3, 4]
# Example: Scope
def outer():
x = 10 # Enclosing scope
def inner():
nonlocal x
x += 5
return x
return inner()
print(outer()) # Output: 15
def decorator(func):
def wrapper():
func()
return wrapper
@decorator
def say_hello():
print("Hello!")
# Example: Higher-order function
return func(value)
return a * b
def generate_numbers(n):
for i in range(n):
yield i
print(num)
# Output:
#0
#1
#2