Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

How to use keyword arguments for parameter positioning in Python

Onyejiaku Theophilus Chidalu

Overview

After creating a function with multiple parameters in Python, it is essential to know how to order the arguments for the parameters of the function whenever the function is called.

What is a parameter in Python?

A parameter in Python is the variable listed inside the parentheses in the function definition. For example, greet_customer(name) is a function that has (name) as its parameter.

What is an argument in Python?

An argument in Python is the value that is supplied to the parameter whenever the function is called.

Types of arguments in Python

The various types of argument in Python are:

  • Positional arguments
  • Keyword arguments
  • Required arguments
  • Optional arguments

Positional arguments

Positional arguments are arguments that can be called by their position in the function definition.

Code

Let’s define a function with two parameters. The arguments of the two parameters will be positioned accordingly.

# defining a function with two parameters
def greet_user(first_name, last_name):
  print(f' Hi {first_name} {last_name} welcome on board! ')

# using the corresponding positional arguments 
greet_user('Theophilus', 'Onyejiaku')

In the example above, the positional arguments are 'Theophilus' and 'Onyejiaku', and they are both positioned respectively for the first_name and last_name parameters.

Keyword arguments

Keyword arguments are arguments that are preceded by an identifier (e.g. first_name=) when calling a function.

Code

Let’s define a function with two parameters. We will call the function with keyword arguments.

# defining a function with two parameters
def greet_user(first_name, last_name):
  print(f' Hi {first_name} {last_name} welcome on board! ')

# using keyword arguments 
greet_user(first_name= 'Theophilus', last_name= 'Onyejiaku' )

In the example above, the keyword arguments are first_name='Theophilus' and last_name= 'Onyejiaku'.

Required arguments

Required arguments are arguments that must be passed to the function.

In the previous example, we define a function, greet_user(first_name, last_name). Since we never gave the parameters any value when defining the function, we must pass the required argument when calling the function. Hence, the required arguments used in the previous example are Theophilus and Onyejiaku.

You can also choose which parameters need to be required.

Code

# defining a function with only one required argument for the parameters
# Note that a required argument must come before the default argument
def greet_user(last_name, first_name = 'Theophilus'):
  print(f' Hi {first_name} {last_name} welcome on board! ')


# using the required arguments only
greet_user(last_name = 'Onyejiaku' )

We can see that the requirement argument from the program above is last_name.

A required argument must come before the default argument for the program to be successful.

Optional arguments

Optional arguments are arguments that cannot be passed to the function. In Python, optional arguments are arguments that have a default value.

Positional arguments vs. keyword arguments

In order of positioning, the positional argument must come first, i.e., before the keyword argument.

Code

Let’s write a code to see which comes first between a positional argument and a keyword argument when calling both in a program.

# defining a function with two parameters
def greet_user(first_name, last_name):
  print(f' Hi {first_name} {last_name} welcome on board! ')

# let the keyword argument come first 
greet_user(first_name='Theophilus', 'Onyejiaku')

In the program above, you can see the error message that reads:

SyntaxError: positional argument follows keyword argument

This happens because, when passing the arguments, our keyword argument comes before the positional argument.

Now, let’s write a new code, but this time, let the positional argument come before the keyword argument.

# defining a function with two parameters
def greet_user(first_name, last_name):
  print(f' Hi {first_name} {last_name} welcome on board! ')

#Let the positional argument come first 
greet_user('Theophilus', last_name = 'Onyejiaku', )

Since the program above runs successfully, we can conclude that when using positional and keyword arguments in Python, the positional argument must come before the keyword argument.

RELATED TAGS

python
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring