Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Positional Arguments | Positional and Optional Arguments
Python Functions Tutorial

Positional Arguments

Swipe to show menu

In Python, positional arguments are function arguments passed to a function based on their position or order. When defining a function, you can specify the parameters it expects. When calling the function, you provide the corresponding arguments in the same order as the parameters.

def function_name(argument1, argument2):
    ...

In previous chapters, you used positional arguments by placing them in parentheses () and calling the function with arguments in the correct order.

Unpacking Dictionaries as Keyword Arguments

While you can pass arguments individually, Python also allows you to deliver a collection of arguments stored inside a dictionary. By prefixing the dictionary with double asterisks () when calling a function, you unpack its key-value pairs directly into the function as keyword arguments.

def function_name(argument1, argument2):
    ...
    
args = {
    'argument1': value1,
    'argument2': value2
}
function_name(**args)
Note
Note

The ** operator will be explained in a later chapter.

Python translates dictionary under the hood into standard keyword arguments. Because this technique uses keyword matching rather than position matching, the order of the keys in your dictionary does not matter. Python will successfully map the dictionary values to the correct function parameters as long as the dictionary keys exactly match the parameter names.

123456789
# Function with two positional arguments def greet(name, age): print(f'Hello, {name}! You are {age} years old.') # Calling the `greet()` function using dictionary greet(age=25, name='Alex') # Calling the `greet()` function using ordered values greet('Alex', 25)

This method of setting arguments is preferable because it enhances the readability and interpretability of the code.

question mark

Which statements are true about the function calls?

Select all correct answers

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Section 2. Chapter 1
some-alt