Python Docs

Python Function Arguments — Full Cheat Sheet

Function arguments are the values you pass when calling a function. You define parameters in the function definition and supply arguments when you call it.

Basic Arguments

A simple function with one parameter, called multiple times with different arguments:

def my_function(fname):
  print(fname + " Refsnes")

my_function("Emil")
my_function("Tobias")
my_function("Linus")

Parameters vs Arguments

  • Parameter: variable in the function definition.
  • Argument: actual value you pass during the call.
def my_function(name):  # name is a *parameter*
  print("Hello", name)

my_function("Emil")  # "Emil" is an *argument*

Number of Arguments

By default, the number of arguments must match the number of parameters.

def my_function(fname, lname):
  print(fname + " " + lname)

my_function("Emil", "Refsnes")

Error Example

def my_function(fname, lname):
  print(fname + " " + lname)

my_function("Emil")  # ERROR: missing argument

Default Parameter Values

You can give parameters default values. If the argument is omitted, the default is used.

def my_function(name="friend"):
  print("Hello", name)

my_function("Emil")
my_function("Tobias")
my_function()
my_function("Linus")

Another Example

def my_function(country="Norway"):
  print("I am from", country)

my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")

Keyword Arguments

With keyword arguments, you specify which parameter each value belongs to.

def my_function(animal, name):
  print("I have a", animal)
  print("My", animal + "'s name is", name)

my_function(animal="dog", name="Buddy")

Order Does Not Matter

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

In positional arguments, the order decides which value goes to which parameter.

def my_function(animal, name):
  print("I have a", animal)
  print("My", animal + "'s name is", name)

my_function("dog", "Buddy")

Wrong Order Example

def my_function(animal, name):
  print("I have a", animal)
  print("My", animal + "'s name is", name)

my_function("Buddy", "dog")  # wrong order

Mixing Positional and Keyword Arguments

Rule: positional arguments must always come before keyword arguments.

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

List as Argument

def my_function(fruits):
  for fruit in fruits:
    print(fruit)

my_fruits = ["apple", "banana", "cherry"]
my_function(my_fruits)

Dictionary as Argument

def my_function(person):
  print("Name:", person["name"])
  print("Age:", person["age"])

my_person = {"name": "Emil", "age": 25}
my_function(my_person)

Return Values

Use return to send data back from a function.

def my_function(x, y):
  return x + y

result = my_function(5, 3)
print(result)

Returning a List

def my_function():
  return ["apple", "banana", "cherry"]

fruits = my_function()
print(fruits[0])
print(fruits[1])
print(fruits[2])

Returning a Tuple

def my_function():
  return (10, 20)

x, y = my_function()
print("x:", x)
print("y:", y)

Positional-Only Arguments

Arguments before / are positional-only. They cannot be passed as keyword arguments.

def my_function(name, /):
  print("Hello", name)

my_function("Emil")

Error Example

def my_function(name, /):
  print("Hello", name)

my_function(name="Emil")  # ERROR

Keyword-Only Arguments

Arguments after * are keyword-only. You must call them using the parameter name.

def my_function(*, name):
  print("Hello", name)

my_function(name="Emil")

Error Example

def my_function(*, name):
  print("Hello", name)

my_function("Emil")  # ERROR

Combining Positional-Only and Keyword-Only

Here, a and b are positional-only, and c, d are keyword-only.

def my_function(a, b, /, *, c, d):
  return a + b + c + d

result = my_function(5, 10, c=15, d=20)
print(result)