Free Python course. Sign Up for tracking progress →

Python: Optional function parameters

In programming, many functions and methods have parameters that are rarely changed. In such cases, these parameters are given default values that can be changed as needed. This reduces the amount of identical code. Let's see how it looks like in practice.

Let's look at an example:

# The function of degree increase
# The second parameter has a default value of two
def pow(x, base=2):
    return x ** base

# Three to the second power (two is the default setting)
pow(3)  # 3 * 3 = 9
# Three to the Third Degree
pow(3, 3)  # 3 * 3 * 3 = 27

The default value looks like a normal assignment in the definition. It only works if the parameter is not passed.

Imagine that you didn't bring the parts for your car with you to the car service. Then the car mechanic will offer you to put the ones he has - the default ones.

The default value can be even when there is only one parameter:

def my_print(text='nothing'):
  print(text)

my_print()  # => "nothing"
my_print("Hexlet")  # => "Hexlet"

There can be any number of parameters with default values:

def f(a=5, b=10, c=100):

The default values have one limitation. They must go at the very end of the parameter list. From the syntax point of view, it is impossible to create a function with an optional parameter followed by a mandatory one:

# Такой код завершится с ошибкой
def f(a=5, b=10, c=100, x):
# And such a
def f(a=5, b=10, x, c=100):

# This code will work
def f(x, a=5, b=10, c=100):

# This one will work, too.
def f(x, y, a=5, b=10, c=100):

Now you know how to work with the default values of the parameters. You can have them for several parameters, or for one parameter. And remember that the default values should be at the very end of the parameter list. This knowledge will help to reduce the amount of identical code.

Instructions

Implement a function get_hidden_card() that takes a credit card number (consisting of 16 digits) as a string and returns its hidden version, which can be used on the site for display. If the original card had the number 2034399002125581, then the hidden version looks like ****5581. In other words, the function replaces the first 12 characters with asterisks. The number of asterisks is controlled by a second, optional parameter. The default value is 4.

# The credit card is passed inside as a string
# The second parameter is not passed, so there will be 4 stars
get_hidden_card('1234567812345678') # ****5678

get_hidden_card('1234567812345678', 2) # **5678
get_hidden_card('1234567812345678', 3) # ***5678

# Or using variables

card_number = '2034399002121100'
get_hidden_card(card_number) # ****1100
get_hidden_card(card_number, 1) # *1100

To perform the task, you will need a string repetition mechanism that repeats a string a specified number of times. To do this, just multiply the string by the number of repetitions:

'+' * 5 # +++++
'o' * 3 # ooo
The exercise doesn't pass checking. What to do? 😶

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
In my environment the code works, but not here 🤨

Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.

My code is different from the teacher's one 🤔

It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.

In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.

I've read the lessons but nothing is clear 🙄

It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.

By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.


If you got stuck and don't know what to do, you can ask a question in our community