Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
In Python, functions are first-class objects, which means that they can be assigned to variables, passed as parameters to other functions, and returned from other functions. This allows for advanced usage of functions and can make your code more modular and reusable.
Here are some examples of advanced usage of functions in Python:
Assign function to variables:
def add(a, b): return a + b my_function = add print(my_function(2, 3)) # Output: 5
In this example, we define a function add
that takes two parameters and returns their sum. We then assign the add
function to a variable called my_function
, and use it to add two numbers.
Pass function as parameters to other functions:
def add(a, b): return a + b def subtract(a, b): return a - b def calculate(func, a, b): return func(a, b) print(calculate(add, 2, 3)) # Output: 5 print(calculate(subtract, 5, 2)) # Output: 3
In this example, we define two functions add
and subtract
that take two parameters and return their sum or difference. We then define a function calculate
that takes a function and two parameters as inputs, and calls the function with the two parameters. We use the calculate
function to add or subtract two numbers.
Return a function from other function:
def create_adder(x): def adder(y): return x + y return adder add_5 = create_adder(5) print(add_5(3)) # Output: 8
In this example, we define a function create_adder
that takes a parameter x
and returns another function adder
. The adder
function takes a parameter y
and returns the sum of x
and y
. We use the create_adder
function to create a new function add_5
that adds 5
to any number.
These examples demonstrate how functions can be used in advanced ways in Python. By assigning functions to variables, passing functions as parameters to other functions, and returning functions from other functions, you can create more modular and reusable code.
How to define a function in Python:
def
keyword, specifying the function name, parameters, and a block of code.def greet(name): print(f"Hello, {name}!") greet("Alice")
Function parameters and arguments in Python:
def add_numbers(a, b): return a + b result = add_numbers(3, 5)
Return statement in Python functions:
return
statement to send a value back from a function to the calling code.def square(x): return x ** 2 result = square(4)
Default arguments and keyword arguments in Python functions:
def greet_person(name, greeting="Hello"): print(f"{greeting}, {name}!") greet_person("Bob") greet_person("Alice", greeting="Hi")
Anonymous functions and lambda expressions in Python:
lambda
expressions for short-term use.add = lambda x, y: x + y result = add(3, 5)
Scope and lifetime of variables in Python functions:
def example_function(): local_variable = 42 print(local_variable) example_function()
Recursive functions in Python:
def factorial(n): if n == 0 or n == 1: return 1 else: return n * factorial(n - 1)
Built-in functions in Python:
numbers = [3, 1, 4, 1, 5, 9, 2] sorted_numbers = sorted(numbers)
Function annotations in Python:
def add(x: int, y: int) -> int: return x + y
Decorators and function wrapping in Python:
def decorator(func): def wrapper(*args, **kwargs): print("Before function execution") result = func(*args, **kwargs) print("After function execution") return result return wrapper @decorator def my_function(): print("Inside the function")
Function overloading in Python:
def greet(name, greeting="Hello"): print(f"{greeting}, {name}!") greet("Alice") greet("Bob", greeting="Hi")
Higher-order functions in Python:
def apply_operation(func, x, y): return func(x, y) result = apply_operation(lambda a, b: a * b, 3, 4)
Generator functions and yield in Python:
yield
to generate a sequence of values lazily.def fibonacci_generator(): a, b = 0, 1 while True: yield a a, b = b, a + b