Python Foundations - Assessments

19 / 54

Parameters and Arguments

Some of the built-in functions we have seen require arguments. For example,

  • When you call math.cos you pass a number as an argument.
  • math.pow takes two arguments, the base and the exponent to calculate the exponential of some number.

Inside the function, the arguments are assigned to variables called parameters. Here is an example of a user-defined function that takes an argument:

def print_name(value):
    x = value
    print(value)

This function assigns the argument to a parameter named value. When the function is called, it assigns the value to x and then prints the value of the parameter (whatever it is).

The same rules of composition that apply to built-in functions also apply to user-defined functions, so we can use any kind of expression as an argument for print_name:

print_name(str(math.cos(0)))

It prints 1.0

The argument is evaluated before the function is called, so in the above example, str(math.cos(0)) is evaluated only once.

Passing arguments as args and kwargs

We can pass any number of arguments to a function using special symbols. There are two ways to do this:

Non-keyworded Arguments -> Represented as *args

Keyworded Arguments -> Represented as **kwargs

These ways are used when we don't know how many arguments we might need to pass to the function. These types of arguments can appear before or after the fixed or known arguments in the function definition. If known arguments are appearing after the args, then while calling the function, you will need to pass the fixed parameters by keyword.

Passing as *args

It allows passing zero or more arguments which can be iterated over inside the function. The syntax is to define the args with a single star. Example: Below function multiplies all numbers passed as arguments and returns the output suffixed with a pre-defined message passed to the function.

def multiply(message, *args):
    output = 1
    for arg in args:
        output *= arg
    return message + " " + str(output)

multiply_value = multiply("My Sum:", 1, 2, 4, 6)
print(multiply_value)

Another variation:

def multiply(*args, message):
    output = 1
    for arg in args:
        output *= arg
    return message + " " + str(output)

multiply_value = multiply(1, 2, 4, 6, message="My Sum:")
print(multiply_value)

Another variation:

def multiply(premessage, *args, message):
    output = 1
    for arg in args:
        output *= arg
    return premessage + " " + str(output) + " " + message

multiply_value = multiply("Starting calculation:", 2, 4, 6, message="My Sum:")
print(multiply_value)

Passing as **kwargs

It allows passing zero or more keyworded arguments which can be iterated over inside the function. The syntax is to define the args with two stars. Example: Below function sums up the count of each type of fruit from the passed arguments. Each argument is key-value pair of fruit type and count.

def groupdata(message, **kwargs):
    grouped_data = {}
    for key, value in kwargs.items():
        grouped_data[key.split("_")[0]] = grouped_data.get(key.split("_")[0], 0) + value
    return message + " " + str(grouped_data)

summary = groupdata(
    "My fruits:",
    apple_indian = 3,
    orange_indian = 5,
    apple_mexican = 6,
    orange_mexica = 7,
    banana=11
)
print(summary)
INSTRUCTIONS
  • Write a function with name my_function which takes one argument with name number. Then, inside the function, write these statements,

    1 - Store the number in a variable xyz
    2 - Convert it into str and store it in xyz_str
    3 - Concatenate cloudxlab to xyz_str
    4 - print xyz_str

  • Test your function by passing an argument 10. It should print 10cloudxlab.



Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...