In Python, there are several types of arguments that can be used when defining a function. These arguments determine how values are passed to the function and how they are treated within the function. The following are the commonly used types of arguments in Python:
Positional Arguments: These are the arguments that are passed to a function in the order defined by the function’s parameter list. The values are assigned to the parameters based on their position.
def function_name(arg1, arg2, ...):
# Function body
Keyword Arguments: These arguments are passed to a function using the syntax parameter_name=value
. The order of the arguments doesn’t matter as long as you specify the parameter names.
def function_name(parameter1=value1, parameter2=value2, ...):
# Function body
Default Arguments: Default arguments are parameters that have a predefined value in the function definition. If no value is passed for such an argument, the default value is used.
def function_name(arg1, arg2=default_value):
# Function body
*args: It is used to pass a variable number of non-keyworded arguments to a function. The *args
parameter allows the function to accept any number of positional arguments, which are then accessible as a tuple within the function.
**kwargs: It is used to pass a variable number of keyword arguments to a function. The **kwargs
parameter allows the function to accept any number of keyword arguments, which are then accessible as a dictionary within the function.
Here’s the information presented in a tabular format:
Argument Type | Syntax | Description |
---|---|---|
Positional | def function(arg1, arg2): | Arguments are passed to the function based on their position in the parameter list. |
Keyword | def function(arg1=value1, arg2=value2): | Arguments are passed using parameter names and corresponding values. Order doesn’t matter. |
Default | def function(arg1, arg2=default_value): | Arguments have predefined default values. If no value is provided, the default value is used. |
Variable-Length | def function(*args): | Accepts a variable number of non-keyworded arguments. Arguments are accessible as a tuple within the function. |
Keyworded Variable | def function(**kwargs): | Accepts a variable number of keyword arguments. Arguments are accessible as a dictionary within the function. |
By using these argument types and their respective syntax, you can define functions that can handle different types of inputs and provide flexibility in how arguments are passed.
Types of Arguments in Python Examples
Here are examples demonstrating the different types of arguments in Python:
1. Positional Arguments:
def greet(name, age):
print("Hello", name, "you are", age, "years old")
greet("Alice", 25)
2. Keyword Arguments:
def greet(name, age):
print("Hello", name, "you are", age, "years old")
greet(age=25, name="Alice")
3. Default Arguments:
def greet(name, age=30):
print("Hello", name, "you are", age, "years old")
greet("Alice") # Output: Hello Alice you are 30 years old
greet("Bob", 35) # Output: Hello Bob you are 35 years old
4. Variable-Length Arguments:
*args
(non-keyworded variable-length arguments):
def add(*args):
result = 0
for num in args:
result += num
return result
print(add(1, 2, 3)) # Output: 6
print(add(1, 2, 3, 4, 5)) # Output: 15
**kwargs
(keyworded variable-length arguments):
def print_info(**kwargs):
for key, value in kwargs.items():
print(key, ":", value)
print_info(name="Alice", age=25, city="New York")
Suppose we have a function called calculate_total_cost
that calculates the total cost of an item, taking into account the base price, any discounts, and additional charges. We’ll use the different types of arguments to provide flexibility in how the values are passed to the function.
def calculate_total_cost(base_price, discount=0, *extra_charges, **additional_info):
total_cost = base_price - discount
for charge in extra_charges:
total_cost += charge
print("Item Cost:", total_cost)
print("Additional Info:", additional_info)
# Example 1: Using positional and default arguments
calculate_total_cost(100, 10)
# Example 2: Using keyword arguments
calculate_total_cost(base_price=200, discount=20)
# Example 3: Using variable-length arguments and keyword arguments
calculate_total_cost(150, 5, 2, 3, gift_wrapping=True, delivery="Express")
Output:
- Example 1 demonstrates the use of positional arguments, where
base_price
is assigned the value 100 anddiscount
takes the default value of 0. - Example 2 showcases keyword arguments, where we explicitly specify the values of
base_price
anddiscount
. - Example 3 showcases the use of variable-length arguments (
*extra_charges
) to accept any number of additional charges. It also utilizes keyword arguments (**additional_info
) to pass extra information like gift wrapping and delivery preferences.
Do comment if you have any doubts or suggestions on this Python function topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.