Python Programming
Functions in Python
Functions in Python
Section titled “Functions in Python”Functions are reusable blocks of code that perform specific tasks.
Defining Functions
Section titled “Defining Functions”def greet(name): """Return a greeting message.""" return f"Hello, {name}!"
message = greet("Alice")print(message) # Hello, Alice!Parameters and Arguments
Section titled “Parameters and Arguments”# Default parametersdef greet(name, greeting="Hello"): return f"{greeting}, {name}!"
# Keyword argumentsgreet(name="Bob", greeting="Hi")
# *args for variable positional argumentsdef sum_all(*numbers): return sum(numbers)
# **kwargs for variable keyword argumentsdef print_info(**kwargs): for key, value in kwargs.items(): print(f"{key}: {value}")Lambda Functions
Section titled “Lambda Functions”square = lambda x: x ** 2print(square(5)) # 25
# Common use with map, filternumbers = [1, 2, 3, 4, 5]squared = list(map(lambda x: x**2, numbers))Next Steps
Section titled “Next Steps”Continue to Object-Oriented Programming →
Tutorials