Arguments par défaut et par mot-clé

Python intermédiaire pour les développeurs

Jasmin Ludolf

Senior Data Science Content Developer

Moyenne

# 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
Python intermédiaire pour les développeurs

Arguments

  • Valeurs fournies à une fonction ou à une méthode
    • Position
    • Mot-clé
Python intermédiaire pour les développeurs

Arguments de position

  • Fournir les arguments dans l'ordre, séparés par des virgules
# Round pi to 2 digits
print(round(3.1415926535, 2))
3.14
Python intermédiaire pour les développeurs

Arguments par mot-clé

  • Fournir des arguments en attribuant des valeurs à keywords

  • Utile pour l'interprétation et le suivi des arguments

# Round pi to 2 digits 
print(round(number=3.1415926535
Python intermédiaire pour les développeurs

Arguments par mot-clé

  • Fournir des arguments en attribuant des valeurs à keywords

  • Utile pour l'interprétation et le suivi des arguments

# Round pi to 2 digits 
print(round(number=3.1415926535, ndigits=2))
3.14
Python intermédiaire pour les développeurs

Identification des arguments par mot-clé

# Get more information about the help function
print(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.
Python intermédiaire pour les développeurs

Arguments par mot-clé

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.

$$

  • Premier argument : number
  • Deuxième argument : ndigits
Python intermédiaire pour les développeurs

Arguments par défaut

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 = aucune valeur / vide

  • Argument par défaut : définir une valeur default pour un argument

  • Remplaçons None par 2

  • Valeur couramment utilisée : définissez-la à l'aide d'un argument par défaut

Python intermédiaire pour les développeurs

Ajout d’un argument

# Create a custom function
def average(values):
    average_value = sum(values) / len(values)
    rounded_average = round(average_value, 2)
    return rounded_average
Python intermédiaire pour les développeurs

Ajout d’un argument

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


Python intermédiaire pour les développeurs

Ajout d’un 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
Python intermédiaire pour les développeurs

Ajout d’un 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
Python intermédiaire pour les développeurs

Utilisation de la fonction average() modifiée

# List of preparation times (minutes)
preparation_times = [19.23, 15.67, 48.57, 23.45, 12.06, 34.56, 45.67]
Python intermédiaire pour les développeurs

Utilisation de la fonction average() modifiée

# Get the average without rounding
print(average(preparation_times, False))
28.4585714
# Get the average without rounding
print(average(preparation_times))
28.4585714
Python intermédiaire pour les développeurs

Utilisation de la fonction average() modifiée

# Get the rounded average
print(average(values=preparation_times, rounded=True))
28.46
Python intermédiaire pour les développeurs

Passons à la pratique !

Python intermédiaire pour les développeurs

Preparing Video For Download...