Python def default values

Python Default Arguments: A Complete Guide (with Examples)

In almost every Python program, you are dealing with functions that take input, perform an action, and spit out an output. More often than not, it can be useful to include a default argument value in a function. If an input argument isn’t provided, the default value is used instead. And if the input value is provided, that’s going to be used in the function.

You can add default function parameters in Python by giving a default value for a parameter in the function definition using the assignment operator (=).

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

Now the default value for the name is “friend“.

If you call this function without a parameter:

You get the default greeting:

But if you call it with a name argument:

You get a personalized greeting using the name:

This is a complete guide to default values in Python.

You will learn what is a default argument and how to specify multiple default arguments in a function. Besides, you learn that you can use both regular arguments and default arguments in the same function.

Multiple Default Parameters in Python

Multiple default parameters in a function in Python

You can implement Python functions with multiple default parameters. This works similarly to how you can have functions with a number of regular parameters:

  • Specify the parameter name and assign it a default value.
  • Then add a comma and specify the next parameter.
def function(param1=value1, param2=value2, param3=value3, . ):

For instance, let’s create a function that asks for first name and last name. If these are not given, the default values are used instead:

def greet(firstname="Mr.", lastname="Who"): print("Hello,", firstname, lastname)

Here are some example calls for this function:

greet() greet("Jack") greet(lastname="Jones") greet("Matt", "Gibson")
Hello, Mr. Who Hello, Jack Who Hello, Mr. Jones Hello, Matt Gibson

Next, let’s take a look at how to add both default and regular parameters into a function call.

Mix Up Default Parameters and Regular Parameters

Mixing regular and default parameters in the same function call python

You can define a function with both default parameters and regular parameters in Python. When you call a function like this, the Python interpreter determines which variable is which based on whether it has the argument label or not.

def greet(greeting_word, firstname="Mr.", lastname="Who"): print(greeting_word, firstname, lastname) # Example calls: greet("Hi") greet("Hi", "Jack") greet("Hello", lastname="Jones") greet("Howdy", "Matt", "Gibson")
Hi Mr. Who Hi Jack Who Hello Mr. Jones Howdy Matt Gibson

Last but not least, let’s take a look at how default parameters are denoted in Python documentation.

Читайте также:  Header text html utf8

Default Parameters in Python Documentation

If you read the Python documentation, you sometimes see functions that have default parameters in the function syntax.

For example, let’s take a look at the enumerate function documentation:

In this function, there is start=0 in the arguments.

This means that the enumerate function has a default argument start that is 0 by default. In other words, you can call the enumerate() function by specifying:

  • iterable parameter only.
  • start parameter to be something else than 0.

Conclusion

Today you learned about Python function default parameters.

You can provide default values for function parameters in Python. This can simplify your function calls and code.

Here is an example of a function with a default parameter value:

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

When you call this function without a parameter, it defaults to saying “Hello, friend”. When you call it with a name argument, it greets the person with that name.

Thanks for reading. I hope you enjoy it. Happy coding!

Further Reading

Источник

Python Default Parameters

Summary: in this tutorial, you’ll learn about the Python default parameters to simplify function calls.

Introduction to Python default parameters

When you define a function, you can specify a default value for each parameter.

To specify default values for parameters, you use the following syntax:

def function_name(param1, param2=value2, param3=value3, . ):Code language: Python (python)

In this syntax, you specify default values ( value2 , value3 , …) for each parameter using the assignment operator ( =) .

When you call a function and pass an argument to the parameter that has a default value, the function will use that argument instead of the default value.

However, if you don’t pass the argument, the function will use the default value.

To use default parameters, you need to place parameters with the default values after other parameters. Otherwise, you’ll get a syntax error.

For example, you cannot do something like this:

def function_name(param1=value1, param2, param3):

This causes a syntax error.

Python default parameters example

The following example defines the greet() function that returns a greeting message:

def greet(name, message='Hi'): return f"  "Code language: Python (python)

The greet() function has two parameters: name and message . And the message parameter has a default value of ‘Hi’ .

The following calls the greet() function and passes the two arguments:

def greet(name, message='Hi'): return f"  " greeting = greet('John', 'Hello') print(greeting)Code language: Python (python)
Hello JohnCode language: Python (python)

Since we pass the second argument to the greet() function, the function uses the argument instead of the default value.

The following example calls the greet() function without passing the second argument:

def greet(name, message='Hi'): return f"  " greeting = greet('John') print(greeting)Code language: Python (python)
Hi JohnCode language: Python (python)

In this case, the greet() function uses the default value of the message parameter.

Multiple default parameters

The following redefines the greet() function with the two parameters that have default values:

def greet(name='there', message='Hi'): return f"  "Code language: Python (python)

In this example, you can call the greet() function without passing any parameters:

def greet(name='there', message='Hi'): return f"  " greeting = greet() print(greeting)Code language: Python (python)
Hi thereCode language: Python (python)

Suppose that you want the greet() function to return a greeting like Hello there . You may come up with the following function call:

def greet(name='there', message='Hi'): return f"  " greeting = greet('Hello') print(greeting)Code language: Python (python)

Unfortuntely, it returns an unexpected value:

Hi HelloCode language: Python (python)

Because when you pass the ‘Hello’ argument, the greet() function treats it as the first argument, not the second one.

To resolve this, you need to call the greet() function using keyword arguments like this:

def greet(name='there', message='Hi'): return f"  " greeting = greet(message='Hello') print(greeting)Code language: Python (python)
Hello thereCode language: Python (python)

Summary

  • Use Python default parameters to simplify the function calls.
  • Place default parameters after the non-default parameters.

Источник

Default Arguments in Python Functions

Functions in Python are used to implement logic that you want to execute repeatedly at different places in your code. You can pass data to these functions via function arguments. In addition to passing arguments to functions via a function call, you can also set default argument values in Python functions. These default values are assigned to function arguments if you do not explicitly pass a parameter value to the given argument. Parameters are the values actually passed to function arguments.

In this article, you will see how to use default arguments in Python functions. But first, we will see how to define a function in Python and how to explicitly pass values to function arguments.

Function without Arguments

Let’s define a very simple Python function without any arguments:

def my_function(): print("This is a function without arguments") 

The above script defines a function, my_function , which doesn’t accept any arguments and simply prints a string.

The following script shows how you’d actually call the my_function() function:

In the output, you should see a simple statement printed to the screen by the my_function() function:

This is a function without arguments 

Function with Explicit Arguments

Let’s now define a simple Python function where we have to pass multiple values for the function arguments. If you do not specify values for all the function arguments, you will see an error.

Here is the function we’ll be using as an example:

def func_args(integer1, integer2): result = integer1 + integer2 return result 

In the code above we create a function, func_args() , with two arguments integer1 and integer2 . The function adds the values passed in the two arguments and returns the result to the function caller.

Let’s try calling the above function with two arguments:

result = func_args(10, 20) print(result) 

The above script calls the func_args() method with two parameter values, i.e. 10 and 20. In the output, you should see the sum of these two values, i.e. 30.

Let’s now try to call the func_args() method without passing values for the arguments:

result = func_args() print(result) 

In the output, you should see the following error:

--------------------------------------------------------------------------- TypeError Traceback (most recent call last) in ----> 1 result = func_args() 2 print(result) TypeError: func_args() missing 2 required positional arguments: 'integer1' and 'integer2' 

The error is quite clear, the function call to func_args() is missing the 2 required positional arguments, integer1 and integer2 . The error basically tells us that we need to pass values for the integer1 and integer2 arguments via the function call.

Let’s now pass a value for one of the arguments and see what happens:

result = func_args(10) print(result) 

Now in the output, you should again see the following error:

--------------------------------------------------------------------------- TypeError Traceback (most recent call last) in ----> 1 result = func_args(10) 2 print(result) TypeError: func_args() missing 1 required positional argument: 'integer2' 

The difference here is that the error now tells us that the value for one of the positional arguments, i.e. integer2 , is missing. This means that without any default argument values set, you have to pass values explicitly for all the function arguments, otherwise an error will be thrown.

What if you want your function to execute with or without the argument values in the function call? This is where default arguments in Python functions come in to play.

Function with Default Arguments

Default arguments in Python functions are those arguments that take default values if no explicit values are passed to these arguments from the function call. Let’s define a function with one default argument.

def find_square(integer1=2): result = integer1 * integer1 return result 

The above script defines a function find_square() with one default argument i.e. integer1 . The default value for the integer1 argument is set to 2. If you call the find_square() method with a value for the integer1 argument, the find_square() function will return the square of that value.

Otherwise, if you do not pass any value for the integer1 argument of the find_square() function, you will see that the default value, i.e. 2, will be assigned to integer1 , and the function will return the square of 2, i.e. 4.

Let’s first call the find_square() method with the argument value of 10:

result = find_square(10) print(result) 

Источник

Оцените статью