![]() |
VOOZH | about |
In Python, functions can accept values in different ways when we call them. When calling a function, the way you pass values decides how they will be received by the function. The two most common ways are:
Both methods are useful, but they behave differently.
Keyword arguments mean you pass values by parameter names while calling the function.
Example: The following example shows how keyword arguments work and why order doesn’t matter.
Hi, I am Prince My age is 20 Hi, I am Prince My age is 20
Explanation:
Positional arguments mean values are passed in the same order as parameters are defined in the function.
Example 1: This example show how positional arguments work and what happens if you mix up the order.
15
Explanation: Arguments are passed by their position.
Example 2: This example shows how changing the order of positional arguments can completely change the result of a mathematical operation.
Correct order: 10 Swapped order: -10
Explanation:
Note: Use positional arguments only when you are sure about the correct order of parameters. Otherwise, prefer keyword arguments to avoid mistakes.
Here’s a simple comparison to summarize everything:
| Keyword Arguments | Positional Arguments |
|---|---|
| Parameter names are used to pass values. | Values are passed strictly in the defined order. |
| Order of values does not matter. | Order of values must be correct. |
| More readable and less error-prone. | Easier for short functions with few arguments. |
| Example: func(a=10, b=20) | Example: func(10, 20) |