Using (or “calling”) a function - Interactive#

You’ve also already used at least one useful function, the print function. (Technically range is something different, but we’ll explore that later on).

Another example is abs, which gives the absolute value of a number, or the magnitude of a complex number. Give that a try (as this is another interactive notebook, open this in Colab, just like we did last week!)

Exercise#

What does abs(3) give? Try using abs on a negative integer, or on a complex number (look back to last week for definition of complex numbers). What does it give?

Exercise#

Assign the value of abs(-4) to a variable named abs_of_minus_4. Then compute the square root of abs_of_minus_4 and assign this to a new, suitably named variable.

Exercise#

Another two functions built-in to python are min and max. These take a number of input argument and return the minimum, or maximum value from among the inputs, respectively. To demonstrate this:

print("Minimum value out of 4, 1 and 2 is", min(4,1,2))
print("Minimum value out of -4, 10 and 12 is", min(-4,10,12))
print("Minimum value out of 3.4, 2.1 and 11.9 is", min(3.4,2.1,11.9))

print("Maximum value out of 4, 1 and 2 is", max(4,1,2))
print("Maximum value out of -4, 10 and 12 is", max(-4,10,12))
print("Maximum value out of 3.4, 2.1 and 11.9 is", max(3.4,2.1,11.9))
Minimum value out of 4, 1 and 2 is 1
Minimum value out of -4, 10 and 12 is -4
Minimum value out of 3.4, 2.1 and 11.9 is 2.1
Maximum value out of 4, 1 and 2 is 4
Maximum value out of -4, 10 and 12 is 12
Maximum value out of 3.4, 2.1 and 11.9 is 11.9

Find the minimum values in the following sets of numbers:

  • 4, 10 and 100

  • 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10

  • -10, -100 and -1000

Find the maximum values from the following sets of numbers:

  • 1, 1.1 and 1.2

  • 9, 10, 11, 12 and 100

  • e, \(\pi\) and 0

Exercise#

You’ve also used print already. You can use this to print many values. For example try doing print(1,2,3). Or to make this fancy. print(f"My 3 values are {1}, {2} and {3}".