Functions are blocks of reusable code in Python. They help in organizing code, avoiding repetition, and improving code readability. This page covers the types of functions based on arguments and return values, as well as the different types of arguments that functions can accept.
A function definition is the process of specifying the structure and behavior of a function. It includes the function's name, parameters, and the block of code that defines what the function does. The syntax is:
Example:
The f
in print(f"Hello, {name}!")
stands for "formatted string literals," also known as f-strings. F-strings are a way to embed expressions inside string literals using curly braces {}. They were introduced in Python 3.6 and provide a concise and convenient way to format strings.
These functions neither take any input arguments nor return any value.
These functions take input arguments but do not return any value.
These functions do not take any input arguments but return a value.
These functions take input arguments and also return a value.
Arguments passed to the function in the correct positional order.
Arguments passed to the function by explicitly stating the parameter names.
Arguments that assume a default value if a value is not provided during the function call.
Functions that accept a variable number of arguments. These can be of two types:
Allows a function to accept any number of positional arguments.
Allows a function to accept any number of keyword arguments.