Editor settings

General settings

Save settings

Learn to program with Python

Keyword arguments #

Another way to call a function in Python is by using keyword arguments. Keyword arguments are passed to a function by specifying the parameter name followed by the value, separated by an equal sign =. This allows the arguments to be passed in any order, regardless of the order they were defined in the function signature.

Here is an example of using keyword arguments:

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

greet("John", "Hello") # Output: Hello, John!
greet(message="Hello", name="John") # Output: Hello, John!

Calling greet with positional and keyword arguments

In this example, the greet() function takes two parameters, name and message. When calling the function with the last line of code, we pass the arguments in a different order than they were defined in the function signature, but this is okay because we use keyword arguments to specify which value corresponds to which parameter.

By using keyword arguments, we can make our code more readable and less error-prone, especially when working with functions that take a large number of arguments.


Exercise #

  • Call the function greet() with keyword arguments.
Output will be displayed here