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)
Functions in Python are blocks of reusable code that perform a specific task. They help you modularize your code, making it more organized and easier to maintain. Here's a tutorial on functions in Python:
To define a function, use the def
keyword, followed by the function name, a pair of parentheses containing any function parameters, and a colon. The function body should be indented:
def function_name(parameters): # function body
To call a function, use its name followed by a pair of parentheses containing any necessary arguments:
function_name(arguments)
Here's an example of a simple function that takes two numbers as arguments and prints their sum:
def add_numbers(a, b): result = a + b print(result) add_numbers(3, 4) # Output: 7
To return a value from a function, use the return
keyword followed by the value or expression you want to return:
def function_name(parameters): # function body return value
Here's an example of a function that returns the sum of two numbers:
def add_numbers(a, b): result = a + b return result sum_result = add_numbers(3, 4) print(sum_result) # Output: 7
You can set default values for function parameters. If a value for a specific parameter is not provided by the caller, the default value will be used:
def greet(name, greeting="Hello"): print(f"{greeting}, {name}!") greet("Alice") # Output: Hello, Alice! greet("Bob", "Hi") # Output: Hi, Bob!
If you want to allow a variable number of arguments in your function, use the *args
and **kwargs
syntax:
*args
: Allows you to pass a variable number of non-keyword (positional) arguments.**kwargs
: Allows you to pass a variable number of keyword arguments.Here's an example using both *args
and **kwargs
:
def example_function(*args, **kwargs): print(f"Positional arguments: {args}") print(f"Keyword arguments: {kwargs}") example_function(1, 2, 3, a=4, b=5, c=6)
This will output:
Positional arguments: (1, 2, 3) Keyword arguments: {'a': 4, 'b': 5, 'c': 6}
In summary, functions in Python are blocks of reusable code that help you modularize and organize your code. By understanding how to define, call, and return values from functions, as well as how to use default and variable-length arguments, you can write more efficient and clean code in Python.
How to define a function in Python:
def
keyword, specifying its name, parameters, and 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, 4)
Return statement in Python functions:
return
statement to specify the value that the function should produce.def square(x): return x ** 2 result = square(5)
Anonymous functions and lambda expressions in Python:
lambda
keyword for concise expressions.square = lambda x: x ** 2 result = square(5)
Scope and lifetime of variables in Python functions:
def example_function(): x = 10 # Local variable print(x) example_function()
Recursive functions in Python:
def factorial(n): if n == 0: return 1 else: return n * factorial(n - 1) result = factorial(5)
Built-in functions in Python:
numbers = [1, 2, 3, 4, 5] total = sum(numbers)
Function annotations in Python:
def add(a: int, b: int) -> int: return a + b
Decorators and function wrapping in Python:
def uppercase_decorator(func): def wrapper(name): result = func(name) return result.upper() return wrapper @uppercase_decorator def greet(name): return f"Hello, {name}!" result = greet("Alice")
Function overloading in Python:
def add(a, b=0, c=0): return a + b + c
Higher-order functions in Python:
def apply_operation(func, x, y): return func(x, y) def add(a, b): return a + b result = apply_operation(add, 3, 4)
Generator functions and yield in Python:
yield
keyword to produce a sequence of values lazily.def square_generator(n): for i in range(n): yield i ** 2 squares = square_generator(5)