Let's talk about the two types of arguments you can use when calling a function: positional arguments and named arguments (a.k.a. keyword arguments).
When you call the built-in
>>> print(2, 1, 3, 4) 2 1 3 4
We call these positional arguments because their position matters. The order of these arguments is significant: the first argument is printed out first; the last one is printed out last.
sep argument (which defaults to a space character).
>>> print(2, 1, 3, 4, sep=' ') 2 1 3 4 >>> print(2, 1, 3, 4) 2 1 3 4 >>> print(2, 1, 3, 4, sep='-') 2-1-3-4 >>> print(2, 1, 3, 4, sep=', ') 2, 1, 3, 4
sep argument defines the separator that should be printed-out between each of the positional arguments given to
There's also an optional
end keyword argument.
end argument defaults to a newline character:
>>> print(2, 1, 3, 4, sep=', ', end='\n') 2, 1, 3, 4
But we can put some exclamation marks in the
end argument (before a newline) to print out exclamation marks at the end:
>>> print(2, 1, 3, 4, sep=', ', end='!!\n') 2, 1, 3, 4!!
The order of the
end arguments doesn't actually matter.
>>> print(2, 1, 3, 4, end='!!\n', sep=', ') 2, 1, 3, 4!!
The order doesn't matter with these because they're not positional arguments: they're named arguments.
Positional arguments have commas between their values.
>>> print(2, 1, 3, 4) 2 1 3 4
Keyword arguments (a.k.a. named arguments) have a name and an equals sign in addition to those values and commas.
>>> print(2, 1, 3, 4, sep=',', end='!\n') 2,1,3,4!
Keyword arguments must come after any positional arguments. Beyond that, the position of keyword arguments doesn't matter at all: it's the name that matters not the position.
Keyword arguments aren't just useful for functions that accept any number of positional arguments (like
For example, the built-in
sum function accepts a first argument:
>>> sum([2, 1, 3 ,4]) 10
But it also accepts a second argument, which defaults to zero:
>>> sum([2, 1, 3 ,4], 0) 10
If we change that second argument to
1, we'll see that this is the start value for the returned summation:
>>> sum([2, 1, 3 ,4], 1) 11
I would much rather see this function called like this:
>>> sum([2, 1, 3 ,4], start=1) 11
We're passing in one positional argument and one keyword argument.
start=1 works with
start is the name of that argument.
In the documentation for the
sum function it says the second argument that is called
>>> help(sum) Help on built-in function sum in module builtins: sum(iterable, /, start=0) Return the sum of a 'start' value (default: 0) plus an iterable of numbers When the iterable is empty, return the start value. This function is intended specifically for use with numeric values and may reject non-numeric types.
So when you're working with named arguments (a.k.a. keyword arguments) the name of the argument actually matters! Whereas when you're working with positional arguments, it's the position that's significant.
When we call a function in Python, we can pass in two different types of arguments:
Named arguments can sometimes make your code a bit more descriptive because you've given a name to an object whose use might not otherwise be clear simply by its position in a function call.
Hello friendly web visitor! 👋
This page is part of Python Morsels, an online Python skill-building service.
The best way to learn is by doing. In the case of Python that means writing Python code. If you'd like to improve your Python skills every week, try out Python Morsels by entering your email below to create an account.
Python Morsels topics pages are free and the first month's worth of exercises is free as well. You don't need to enter payment details to sign up.
You can find explanations of many other Python topics by signing up below.