Arguments
Functions can receive information from the outside through arguments.
You write arguments inside the parentheses that follow the function name. There is no fixed limit on how many you can add; just put a comma between them.
The function in the next example has a single argument, fname. Each call hands it a first name, and the function uses that name to print the full name.
Example
One argument, three calls:
fname as a parameter and prints the name followed by "Refsnes".
Call it with "Emil", "Tobias", and "Linus".
Output: complete Python program
def my_function(fname):
print(fname + " Refsnes")
my_function("Emil")
my_function("Tobias")
my_function("Linus")
Parameters vs Arguments
People often use parameter and argument interchangeably, since both mean information passed into a function.
Seen from the function's side:
The parameter is the variable named between the parentheses of the function definition.
The argument is the real value that the function receives when someone calls it.
def my_function(name): # name is a parameter
print("Hello", name)
my_function("Emil") # "Emil" is an argument
Number of Arguments
Normally the number of arguments in a call has to match what the function expects.
A function that needs 2 arguments must get exactly 2 of them.
Give it more or fewer and Python reports an error.
Example
This function needs 2 arguments and receives 2:
fname and lname and prints them together.
Call it with "Emil" and "Refsnes".
Output: complete Python program
def my_function(fname, lname):
print(fname + " " + lname)
my_function("Emil", "Refsnes")
Example
This one needs 2 arguments but receives just 1:
fname and lname, then call it with only "Emil".
Output: complete Python program
def my_function(fname, lname):
print(fname + " " + lname)
my_function("Emil")
Default Parameter Values
A parameter can have a default value. The function falls back on that value whenever a call leaves out the argument:
Example
name = "friend" that prints a greeting.
Call it with "Emil", "Tobias", no argument, and "Linus".
Output: complete Python program
def my_function(name = "friend"):
print("Hello", name)
my_function("Emil")
my_function("Tobias")
my_function()
my_function("Linus")
Example
A country parameter with a default:
country = "Norway" that prints "I am from" followed by the country.
Call it with "Sweden", "India", no argument, and "Brazil".
Output: complete Python program
def my_function(country = "Norway"):
print("I am from", country)
my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
Keyword Arguments
Arguments can also be sent in the key = value form.
Because each value is labeled with its name, the order in which you write them no longer matters.
Python documentation often abbreviates Keyword Arguments to kwargs.
Example
animal and name parameters and prints information about a pet.
Call it using keyword arguments animal = "dog" and name = "Buddy".
Output: complete Python program
def my_function(animal, name):
print("I have a", animal)
print("My", animal + "'s name is", name)
my_function(animal = "dog", name = "Buddy")
Example
animal and name parameters.
Call it with keyword arguments in reversed order: name = "Buddy", animal = "dog".
Output: complete Python program
def my_function(animal, name):
print("I have a", animal)
print("My", animal + "'s name is", name)
my_function(name = "Buddy", animal = "dog")
Positional Arguments
Arguments passed without keywords are known as positional arguments.
Python matches them to parameters by position, so they have to be in the right order.
Swap them and you get a different outcome.
Example
animal and name parameters.
Call it with positional arguments "dog" and "Buddy".
Output: complete Python program
def my_function(animal, name):
print("I have a", animal)
print("My", animal + "'s name is", name)
my_function("dog", "Buddy")
Example
Changing the order gives a different result:
Output: complete Python program
def my_function(animal, name):
print("I have a", animal)
print("My", animal + "'s name is", name)
my_function("Buddy", "dog")
Mixing Positional and Keyword Arguments
A single function call can use both positional and keyword arguments.
The one rule is that the positional ones go first:
Example
animal, name, and age parameters.
Call it mixing positional and keyword arguments.
Output: complete Python program
def my_function(animal, name, age):
print("I have a", age, "year old", animal, "named", name)
my_function("dog", name = "Buddy", age = 5)
Passing Different Data Types
Arguments can be of any data type: a string, a number, a list, a dictionary and so on.
Inside the function, the value keeps the same type it had when you sent it:
Example
Passing a list to a function:
["apple", "banana", "cherry"] to it.
Output: complete Python program
def my_function(fruits):
for fruit in fruits:
print(fruit)
my_fruits = ["apple", "banana", "cherry"]
my_function(my_fruits)
Example
Passing a dictionary to a function:
{"name": "Emil", "age": 25} to it.
Output: complete Python program
def my_function(person):
print("Name:", person["name"])
print("Age:", person["age"])
my_person = {"name": "Emil", "age": 25}
my_function(my_person)
Return Values
The return statement lets a function give a value back:
Example
Output: complete Python program
def my_function(x, y):
return x + y
result = my_function(5, 3)
print(result)
Returning Different Data Types
A returned value can be of any data type, such as a list, a tuple or a dictionary.
Example
Returning a list:
Output: complete Python program
def my_function():
return ["apple", "banana", "cherry"]
fruits = my_function()
print(fruits[0])
print(fruits[1])
print(fruits[2])
Example
Returning a tuple:
Output: complete Python program
def my_function():
return (10, 20)
x, y = my_function()
print("x:", x)
print("y:", y)
Positional-Only Arguments
A function can insist on receiving positional arguments ONLY.
You do this by adding , / after the arguments, as the first example shows.
Leave out , / and callers may use keyword arguments, even for a function written with positional arguments in mind, as in the second example.
Once , / is there, passing a keyword argument causes an error, which the third example demonstrates.
Example
/.
Call it with a positional argument.
Output: complete Python program
def my_function(name, /):
print("Hello", name)
my_function("Emil")
Example
/, a function allows keyword arguments.
Define a function with name and call it with name = "Emil".
Output: complete Python program
def my_function(name):
print("Hello", name)
my_function(name = "Emil")
Example
name, / and call it with name = "Emil".
Output: complete Python program
def my_function(name, /):
print("Hello", name)
my_function(name = "Emil")
Keyword-Only Arguments
To accept keyword arguments only, put *, before the arguments, as in the first example.
Without *,, callers may pass positional arguments even though the function was written with keywords in mind. The second example does this.
With *, in place, a positional argument causes an error, as the third example shows.
Example
*,.
Call it with name = "Emil".
Output: complete Python program
def my_function(*, name):
print("Hello", name)
my_function(name = "Emil")
Example
*,, a function allows positional arguments.
Define a function with name and call it with "Emil".
Output: complete Python program
def my_function(name):
print("Hello", name)
my_function("Emil")
Example
*, name and call it with "Emil".
Output: complete Python program
def my_function(*, name):
print("Hello", name)
my_function("Emil")
Combining Positional-Only and Keyword-Only
A function may use both kinds together.
Everything before / must be positional, and everything after * must be passed by keyword:
Example
/ and * to enforce argument types and return the sum.
Output: complete Python program
def my_function(a, b, /, *, c, d):
return a + b + c + d
result = my_function(5, 10, c = 15, d = 20)
print(result)