Skip to content

Python function arguments

  • by

In Python, function arguments refer to the values or variables that are passed to a function when it is called. They provide a way to pass data into a function so that it can perform certain operations or calculations using that data.

Python function arguments can be classified into three main types: positional arguments, keyword arguments, and default arguments.

Positional arguments: These are arguments that are passed to a function in the order they are defined in the function signature. The position of the argument determines which parameter it corresponds to in the function.

def add_numbers(a, b):
    return a + b

result = add_numbers(2, 3)
print(result)  # Output: 5

Keyword arguments: These arguments are passed to a function using their corresponding parameter names. It allows you to specify arguments in any order, as long as you explicitly mention the parameter names.

def greet(name, message):
    print(f"Hello, {name}! {message}")

greet(message="Welcome to the chat", name="John")

Default arguments: These are arguments that have a predefined value set in the function signature. If no value is explicitly provided for a default argument when calling the function, the default value is used. Default arguments are defined using the assignment operator (=) in the function signature.

def greet(name, message="Welcome to the chat"):
    print(f"Hello, {name}! {message}")

greet("John")

These are the basic types of function arguments in Python. Functions can also have variable-length arguments using *args or **kwargs, where *args allows passing a variable number of positional arguments, and **kwargs allows passing a variable number of keyword arguments.

Python function arguments example

Here’s an example that demonstrates the different types of function arguments in Python:

# Positional arguments example
def add_numbers(a, b):
    return a + b

result = add_numbers(2, 3)
print(result)  # Output: 5


# Keyword arguments example
def greet(name, message):
    print(f"Hello, {name}! {message}")

greet(message="Welcome to the chat", name="John")


# Default arguments example
def greet(name, message="Welcome to the chat"):
    print(f"Hello, {name}! {message}")

greet("John") 


# Variable-length arguments example
def print_values(*args):
    for value in args:
        print(value)

print_values(1, 2, 3, 4)  


# Variable-length keyword arguments example
def print_info(**kwargs):
    for key, value in kwargs.items():
        print(f"{key}: {value}")

print_info(name="John", age=30, city="New York")

Output:

Python function arguments

In the above example, we have the following scenarios:

  • add_numbers() function takes two positional arguments and returns their sum.
  • greet() function takes two keyword arguments and prints a greeting message.
  • greet() function with default arguments where the second argument has a default value.
  • print_values() function accepts a variable number of positional arguments using *args and prints each value.
  • print_info() function accepts a variable number of keyword arguments using **kwargs and prints each key-value pair.

You can run this code to see the output generated by each function call.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *