Key Concepts

Review core concepts you need to learn to master this subject

Default argument is fallback value

def greet(name, msg="How do you do?"): print("Hello ", name + ', ' + msg) greet("Ankit") greet("Ankit", "How do you do?") """ this code will print the following for both the calls - `Hello Ankit, How do you do?` """

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.

Mutable Default Arguments

def greet(name, msg="How do you do?"): print("Hello ", name + ', ' + msg) greet("Ankit") greet("Ankit", "How do you do?") """ this code will print the following for both the calls - `Hello Ankit, How do you do?` """

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.

Python Default Arguments

def greet(name, msg="How do you do?"): print("Hello ", name + ', ' + msg) greet("Ankit") greet("Ankit", "How do you do?") """ this code will print the following for both the calls - `Hello Ankit, How do you do?` """

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 default return value

def greet(name, msg="How do you do?"): print("Hello ", name + ', ' + msg) greet("Ankit") greet("Ankit", "How do you do?") """ this code will print the following for both the calls - `Hello Ankit, How do you do?` """

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

Python variable None check

def greet(name, msg="How do you do?"): print("Hello ", name + ', ' + msg) greet("Ankit") greet("Ankit", "How do you do?") """ this code will print the following for both the calls - `Hello Ankit, How do you do?` """

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.

Python function arguments

def greet(name, msg="How do you do?"): print("Hello ", name + ', ' + msg) greet("Ankit") greet("Ankit", "How do you do?") """ this code will print the following for both the calls - `Hello Ankit, How do you do?` """

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.

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 None. None is a special value in Python. It is unique (there can’t be two different Nones) and immutable (you can’t upda…
  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 return statement will return…
  4. 4
    Function arguments are required in Python. So a standard function definition that defines two parameters requires two arguments passed into the function. # Function definition with two required pa…
  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 populate_list that has two r…
  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 None as a special value to indicate we did not receive anything. After we check…
  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