Function Arguments
Use the full potential of Python functions. Learn about different ways to declare, call, and interact with functions!
StartKey Concepts
Review core concepts you need to learn to master this subject
Default argument is fallback value
Mutable Default Arguments
Python Default Arguments
Python function default return value
Python variable None check
Python function arguments
Default argument is fallback value
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.
- 1Python’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…
- 2How 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…
- 3What 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…
- 4Function 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…
- 5Not 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…
- 6When 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…
- 7When 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…
- 8So 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…
- 9A 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 …
- 10We 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…
- 11Python 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…
- 12This 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…
- 13Not 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…
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory