argument
In Python, an argument is a value or object that you pass into a function or method when you call it.
Arguments allow you to provide input data to the function so that it can perform its task. Functions can accept any number of arguments, which can be of any type, including custom objects.
Arguments can be classified into two main categories:
- Positional arguments must be passed in the same order as defined in the function’s signature.
- Keyword arguments are provided as
argument_name=value
, allowing you to specify which parameter they correspond to, regardless of their order.
Arguments vs Parameters
When defining a function, you specify parameters in its signature. They act as placeholders for the arguments. When you call the function, you provide the arguments that correspond to these parameters.
Example
Here’s an example of a function that takes two arguments, and it’s called with both positional and keyword arguments:
>>> def greet(name, greeting):
... print(f"{greeting}, {name}!")
...
>>> greet("Alice", "Hello") # Called with positional arguments
Hello, Alice!
>>> greet(name="Bob", greeting="Hi") # Called with keyword arguments
Hi, Bob!
In this example, you first call greet()
with positional arguments "Alice"
and "Hello"
. In the second call, you use keyword arguments name="Bob"
and greeting="Hi"
.
Related Resources
Tutorial
Defining Your Own Python Function
In this tutorial, you'll learn how to define and call your own Python function. You'll also learn about passing data to your function, and returning data from your function back to its calling environment.
For additional information on related topics, take a look at the following resources:
- Using Python Optional Arguments When Defining Functions (Tutorial)
- Python args and kwargs: Demystified (Tutorial)
- Defining and Calling Python Functions (Course)
- Defining Your Own Python Function (Quiz)
- Defining Python Functions With Optional Arguments (Course)
- Python args and kwargs: Demystified (Course)
- Python args and kwargs: Demystified (Quiz)