Math functions¶
We use functions in programming all the time. For example, print(), input(), int(), float() and str() are functions of the Python language that we have used so far. There are many other functions in Python, and many of them are used in mathematics. We’ll see some of the simpler math functions below.
Functions abs(), min() and max()¶
The abs(), min() and max() functions are often used in computational tasks. You probably already used them somewhere, so let’s just explain them briefly:
the abs() function returns the absolute value of a numeric expression, which is passed to it as an argument (the absolute value of a number is obtained by ignoring the sign of the number, see example below);
the min() function can have two or more numeric arguments, and returns the value of the smallest of them;
the max() function can have two or more numeric arguments, and returns the value of the greatest of them;
Here’s how to use these functions in a program:
print("abs(3) =", abs(3))
print("abs(-7) =", abs(-7))
print("abs(-5 - -2) =", abs(-5 - -2))
print("min(5, 2, 7, 3) =", min(5, 2, 7, 3))
print("max(5, 2, 7, 3) =", max(5, 2, 7, 3))
(console__numberfunc_absminmax_example)
Functions abs(), min() and max() - questions¶
Check your understanding of the above functions:
Q-17: What is the value of the expression min(10, 20, 30)
?
Q-18: What is the value of the expression max(10, 20, 30)
?
Value Rounding Functions¶
Rounding up real value to an integer is an operation we also often need. We have already seen that by converting a real number to a whole number, we round it towards zero. There are a few more functions that we can use in Python to round up a real number in different ways:
function round() returns the integer closest to the argument (type of the result is int);
function floor() returns the nearest integer less than or equal to the argument value (type of the result is float);
function ceil() returns the closest integer greater than or equal to the argument value (type of the result is float);
Run the following program to see how these functions work and to compare them.
import math
print("round(56.234) =", round(56.234))
print("round(56.789) =", round(56.789))
print("math.floor(56.234) =", math.floor(56.234))
print("math.floor(56.789) =", math.floor(56.789))
print("math.ceil(56.234) =", math.ceil(56.234))
print("math.ceil(56.789) =", math.ceil(56.789))
(console__numberfunc_rounding_example)
Note that the floor and ceil functions are somewhat different from the round function and all previous functions - it says math. in front of their name in the program. This is because these functions are defined in a module called math. Modules are programmatic entities that contain various functions, constants, and other pieces of code that we can use in our programs. The math module contains many other functions in addition to the floor and ceil functions. For example, the known constant pi can be used as math.pi, and the function square root as math.sqrt (we will not use them here).
In order to use the functions of the math module, we need to attach this module to our program. We did this by writing import math
at the beginning of the program. This, of course, enables us to use all other mathematical functions and everything else defined in this module as well.
No special module is required for the round function and all the previous functions, since these functions are built into Python itself, so they are always directly available to us.
Value Rounding Functions - Questions¶
Check your understanding of the functions explained in this lesson:
Q-21: What is the value of the expression abs(round(-2.7))
?
Q-22: One cashier rounds the bill to the nearest integer only if the amount is increased by rounding, otherwise it reports the amount as it is. What formula does this cashier apply (x is the starting value of the bill)?
The task for the curious - function round
The round function can also be called with two arguments (we won’t use it that way), where the second argument is usually a small integer. Check for example the values of round(123.23456,2), round(123.23456,3) and round(123.23456,−1). You can use the space below for trying things out quickly.
Try explaining what the second argument of round is for when a function is called with two arguments.
# insert your code here
(console__givenfunc_round)