Python function arguments
Last Updated :
26 Dec, 2024
In Python, function arguments are the inputs we provide to a function when we call it. Using arguments makes our functions flexible and reusable, allowing them to handle different inputs without altering the code itself. Python offers several ways to use arguments, each designed for specific scenarios. Let's break it down clearly and simply.
Python
def greet(name):
print(f"Hello, {name}!")
#GeeksforGeeks is the argument
greet("GeeksforGeeks")
OutputHello, GeeksforGeeks!
Explanation:
- In this example, the function
greet
takes an argument name
and uses it to print a personalized greeting. We can replace "GeeksforGeeks"
with any name, and the function will adapt.
Types of Arguments
1. Positional Arguments
Positional Arguments are the most common type of arguments. The values we pass are assigned to the function parameters in the same order they’re defined.
Python
def describe_pet(name, species):
print(f"{name} is a {species}")
# "Buddy" → name, "dog" → species
describe_pet("Buddy", "dog")
- Key Point: The order matters. If we swap the arguments, the meaning changes.
2. Keyword Arguments
With keyword arguments, we specify which value goes to which parameter, making our code more readable and flexible. This way, we don’t have to worry about the order of the arguments.
Python
def introduce(name, age, city):
# Introduces a person with their
#name, age, and city
print(f"My name is {name}, I am {age} years old, and I live in {city}.")
introduce(name="Alice", age=25, city="New York")
OutputMy name is Alice, I am 25 years old, and I live in New York.
- Key Point: The order doesn’t matter when using keyword arguments.
3. Default Arguments
Default arguments are handy when we want to provide a default value for a parameter, in case the caller doesn’t specify one.
Python
def greet(name="there"):
# Greets the user with a default value of "there"
#if no name is provided
print(f"Hello, {name}!")
greet()
greet("GeeksforGeeks")
OutputHello, there!
Hello, GeeksforGeeks!
- Key Point: weour can skip default arguments when calling the function, but we can still override them if needed.
Variable-Length Arguments
Sometimes, we don’t know how many arguments a function might need to handle. Python provides tools for this, namely *args and **kwargs:
Use *args
to accept any number of positional arguments. These are treated as a tuple inside the function.
Python
def sum_numbers(*args):
# Prints the arguments and returns their sum
print(f"{args}")
return sum(args)
print(sum_numbers(1, 2, 3, 4))
Use **kwargs
to accept any number of keyword arguments. These are treated as a dictionary inside the function.
Python
def print_details(**kwargs):
# Iterates over keyword arguments and prints each key-value pair
for key, value in kwargs.items():
print(f"{key}: {value}")
print_details(name="Harshit", age=25, city="New Delhi")
Outputname: Harshit
age: 25
city: New Delhi
Similar Reads
Default arguments in Python Python allows function arguments to have default values. If the function is called without the argument, the argument gets its default value.Default Arguments: Python has a different way of representing syntax and default values for function arguments. Default values indicate that the function argum
7 min read
Tuple as function arguments in Python Tuples have many applications in all the domains of Python programming. They are immutable and hence are important containers to ensure read-only access, or keeping elements persistent for more time. Usually, they can be used to pass to functions and can have different kinds of behavior. Different c
2 min read
Python Function Parameters and Arguments Parameters are variables defined in a function declaration. This act as placeholders for the values (arguments) that will be passed to the function. Arguments are the actual values that you pass to the function when you call it. These values replace the parameters defined in the function. Although t
3 min read
Passing function as an argument in Python In Python, functions are first-class objects meaning they can be assigned to variables, passed as arguments and returned from other functions. This enables higher-order functions, decorators and lambda expressions. By passing a function as an argument, we can modify a functionâs behavior dynamically
5 min read
Passing function as an argument in Python In Python, functions are first-class objects meaning they can be assigned to variables, passed as arguments and returned from other functions. This enables higher-order functions, decorators and lambda expressions. By passing a function as an argument, we can modify a functionâs behavior dynamically
5 min read
Function Annotations in Python Basic Terminology PEP: PEP stands for Python Enhancement Proposal. It is a design document that describes new features for Python or its processes or environment. It also provides information to the python community. PEP is a primary mechanism for proposing major new features, for example - Python W
7 min read