Arguments
Usually arguments are identified by their position in the function call. As an example, random.ranint(1,5)
is different from random.ranint(5, 1)
. The function call random.ranint(1,5)
will return a random number between 1 to 5, while random.ranint(1,5)
will cause an error. This is because the first argument is used to represent the lower value of the range and the second argument is used for the higher value.
Now, we have an understanding of what an argument is. Let's move on to the next concept, keyword arguments. These arguments are identified by the keyword put before them in the function call.
Keyword arguments are often used for optional parameters. For example, the print()
function has the optional parameters end
and sep
to specify what should be printed at the end of its arguments and between its arguments (separating them), respectively.
Example:
print('Hello')
print('World')
This will output:
Hello
World
What if we want them to be on the same line?
print('Hello', end = ' ')
print('World')
This will result in:
Hello World
As we end the first print()
statement with a space, the next print()
statement executes on the same line. By default in Python, a print()
statement ends and the next statement starts from a new line.
TRY IT YOURSELF: What if you want to seperate the
Hello
andWorld
by a,
? What will you do?
You can add keyword arguments to the functions you write as well, but first you’ll have to learn about the list and dictionary data types in the next two chapters. For now, just know that some functions have optional keyword arguments that can be specified when the function is called.