Default and keyword arguments

Intermediate Python for Developers

George Boorman

Curriculum Manager, DataCamp

Average

# Create a custom function
def average(values):
    # Calculate the average
    average_value = sum(values) / len(values)

    # Round the results
    rounded_average = round(average_value, 2)

    # Return rounded_average as an output
    return rounded_average
  • values = Argument
Intermediate Python for Developers

Arguments

A couple arguing

  • Arguments are values provided to a function or method

  • Functions and methods have two types of arguments:

    • Positional
    • Keyword
Intermediate Python for Developers

Positional arguments

  • Provide arguments in order, separated by commas
# Round pi to 2 digits
round(3.1415926535, 2)
3.14
Intermediate Python for Developers

Keyword arguments

  • Provide arguments by assigning values to keywords

  • Useful for interpretation and tracking arguments

  • keyword = value

# Round pi to 2 digits 
round(number=3.1415926535
Intermediate Python for Developers

Keyword arguments

  • Provide arguments by assigning values to keywords

  • Useful for interpretation and tracking arguments

  • keyword = value

# Round pi to 2 digits 
round(number=3.1415926535, ndigits=2)
3.14
Intermediate Python for Developers

Identifying keyword arguments

# Get more information about the help function
help(round)
Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.

    The return value is an integer if ndigits is omitted or None.  Otherwise,
    the return value has the same type as the number.  ndigits may be negative.
Intermediate Python for Developers

Keyword arguments

Output of calling help on the round function, annotated to show number is the first argument and ndigits is the second argument

Intermediate Python for Developers

Default arguments

Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.

    The return value is an integer if ndigits is omitted or None.  Otherwise,
    the return value has the same type as the number.  ndigits may be negative.
  • None = no value / empty

  • Default argument: way of setting a default value for an argument

  • We overwrite None to 2

    • Otherwise, the result is an int
Intermediate Python for Developers

Why have default arguments?

  • Helps us think about likely uses for our function
    • Commonly used value - set it using a default argument

 

  • Potentially reduces code for users (if they stick with default values)

 

  • Maintains flexibility
Intermediate Python for Developers

Adding an argument

# Create a custom function
def average(values):
    average_value = sum(values) / len(values)
    rounded_average = round(average_value, 2)
    return rounded_average
Intermediate Python for Developers

Adding an argument

# Create a custom function
def average(values, rounded=False):


Intermediate Python for Developers

Adding an argument

# Create a custom function
def average(values, rounded=False):

# Round average to two decimal places if rounded is True if rounded == True:
average_value = sum(values) / len(values) rounded_average = round(average_value, 2) return rounded_average
Intermediate Python for Developers

Adding an argument

# Create a custom function
def average(values, rounded=False):

# Round average to two decimal places if rounded is True if rounded == True: average_value = sum(values) / len(values) rounded_average = round(average_value, 2) return rounded_average
# Otherwise, don't round else: average_value = sum(values) / len(values) return average_value
Intermediate Python for Developers

Using the modified average() function

sales = [125.97, 84.32, 99.78, 154.21, 78.50, 83.67, 111.13]
Intermediate Python for Developers

Using the modified average() function

# Get the average without rounding
average(sales, False)
105.36857142857141
# Get the average without rounding
average(sales)
105.36857142857141
Intermediate Python for Developers

Using the modified average() function

# Get the rounded average
average(values=sales, rounded=True)
105.37
Intermediate Python for Developers

Let's practice!

Intermediate Python for Developers

Preparing Video For Download...