Key Concepts

Review core concepts you need to learn to master this subject

Python function default return value

# Function returning None def my_function(): pass print(my_function()) #Output None

If we do not not specify a return value for a Python function, it returns None. This is the default behaviour.

None variable

# Function returning None def my_function(): pass print(my_function()) #Output None

In Python, a variable is None if variable is None evaluates to True.

The identity operator is will check whether the variable is identical to None. The expression will return True only if the two variables point to the same object, which is the singleton object in Python of class NoneType.

Python variable None check

# Function returning None def my_function(): pass print(my_function()) #Output None

To check if a Python variable is None we can make use of the statement variable is None.

If the above statement evaluates to True, the variable value is None.

Default argument is fallback value

# Function returning None def my_function(): pass print(my_function()) #Output None

In Python, a default parameter is defined with a fallback value as a default argument. Such parameters are optional during a function call. If no argument is provided, the default value is used, and if an argument is provided, it will overwrite the default value.

Python Default Arguments

# Function returning None def my_function(): pass print(my_function()) #Output None

A Python function cannot define a default argument in its signature before any required parameters that do not have a default argument. Default arguments are ones set using the form parameter=value. If no input value is provided for such arguments, it will take on the default value.

Python function arguments

# Function returning None def my_function(): pass print(my_function()) #Output None

A function can be called using the argument name as a keyword instead of relying on its positional value. Functions define the argument names in its composition then those names can be used when calling the function.

Mutable Default Arguments

# Function returning None def my_function(): pass print(my_function()) #Output None

Python’s default arguments are evaluated only once when the function is defined, not each time the function is called. This means that if a mutable default argument is used and is mutated, it is mutated for all future calls to the function as well. This leads to buggy behaviour as the programmer expects the default value of that argument in each function call.

Learn Python: Function Arguments
Lesson 1 of 1
  1. 1

    Python’s functions offer us a very expressive syntax. We’re going to look into some of the finer details of how functions in Python work and some techniques we can use to be more intuitive while w…

  2. 2

    How do you define a variable that you can’t assign a value to yet? You use […] . […] is a special value in Python. It is unique (there can’t be two different […] s) and immutable (you c…

  3. 3

    What does a function return when it doesn’t return anything? This sounds like a riddle, but there is a correct answer. A Python function that does not have any explicit […] statement will retur…

  4. 4

    Function arguments are required in Python. So a standard function definition that defines two parameters requires two arguments passed into the function. […] Above we defined our function, […

  5. 5

    Not all of your arguments need to have default values. But Python will only accept functions defined with their parameters in a specific order. The required parameters need to occur before any para…

  6. 6

    When we call a function in Python, we need to list the arguments to that function to match the order of the parameters in the function definition. We don’t necessarily need to do this if we pass ke…

  7. 7

    When writing a function with default arguments, it can be tempting to include an empty list as a default argument to that function. Let’s say you have a function called […] that has two require…

  8. 8

    So if we can’t use a list as a default argument for a function, what can we use? If we want an empty list, we can use […] as a special value to indicate we did not receive anything. After we ch…

  9. 9

    A Python function can return multiple things. This is especially useful in cases where bundling data into a different structure (a dictionary or a list, for instance) would be excessive. In Python …

  10. 10

    We don’t always know how many arguments a function is going to receive, and sometimes we want to handle any possibility that comes at us. Python gives us two methods of unpacking arguments provided…

  11. 11

    Python doesn’t stop at allowing us to accept unlimited positional parameters, it gives us the power to define functions with unlimited keyword parameters too. The syntax is very similar, but uses t…

  12. 12

    This keyword argument unpacking syntax can be used even if the function takes other parameters. However, the parameters must be listed in this order: All named positional parameters An unpacke…

  13. 13

    Not only can we accept arbitrarily many parameters to a function in our definition, but Python also supports a syntax that makes deconstructing any data that you have on hand to feed into a functi…

  14. 14

    We covered a lot of ground in this lesson! We learned all about how functions can accept different arguments and different styles by which we can pass those arguments in. We covered: * The default …

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo