Math Functions and Number Methods
00:00 In this lesson, you’ll learn about the most common functions that let you work with numbers in Python. You’ll also discover some useful behaviors exhibited by integers and floating-point numbers. Without further ado, let’s dive right into it.
Another interesting fact about the
round() function in Python is that you can optionally pass a second argument to it, specifying how many decimal digits you want to round to. For instance, if you have an approximation of pi, then you can round it to say, three decimal places.
Notice that you’re getting a floating-point number as a result this time. Because
5 was the last digit in this example, it means there’s a tie. Instead of rounding to the nearest even integer, however, Python will look at the last but one digit to decide about the rounding direction. In this case,
2 is the closest even number. However, this doesn’t always work right.
When you try rounding a number like
2.675 to two decimal places, you’d expect to get
2.68 as the result because
8 is the closest even number. But when you actually run this code, you’ll notice that Python rounds down instead of up.
The next function related to numbers in Python that you may want to use is called
abs(), which is short for the absolute value. It takes a number and returns its size without the sign. For example, the absolute value of
Either way, you’ll always get the positive value of a number. However, when you call the
abs() function on a complex number, then you’ll get the so-called magnitude of a complex number, which is calculated using the corresponding formula.
Finally, the third and last function that you’ll learn about in this lesson is the
pow() function. It provides a more explicit way of raising a number to the given power when compared to the
** (exponentiation) operator you learned about before.
pow() function takes two arguments. The first one is the base, or the number to be raised to a power, and the second argument is the exponent, or the power to which the number is to be raised. As with the
** operator, you can use negative or fractional exponents and bases.
04:00 You can write this more compactly, passing the third value as an optional argument. Before closing this lesson, it’s worthwhile to mention that numbers in Python expose a few useful methods, just like strings do, which you might remember from an earlier course.
04:35 To avoid this ambiguity, you can wrap your numeric literal in parentheses, but this method is only available for the floating-point data type. When you try to call it on an integer value, then you’ll get an error.
Remember that a numeric literal without a decimal point represents an integer. Alternatively, if you want to be more explicit, then you can assign your number to a variable and call the method on the variable rather than the literal. In this case,
3.14 is not an integer, but
Another useful method available on floats is
.as_integer_ratio(), which returns a pair of integers whose ratio is exactly equal to the original float. In this case, the numbers are rather big, but you can try something more like three-quarters to get a ratio comprised of smaller numbers.
Integers also have a couple of helpful methods. One of them can tell you how many bits you need to represent a number in the binary system. For example, a bit string consisting of binary digits
101010 represents the decimal number
42, so you need at least six bits to faithfully express the same number in the binary system.
To recap what you’ve just learned, Python comes with a few built-in math functions for working with numbers. It has the
round() function, which rounds a floating-point number to either the nearest whole integer or up to the given decimal places, which you can specify as the second argument of the function.
abs() function returns an absolute value of a number or the magnitude of a complex number. The
pow() function performs largely the same mathematical operation as the
** operator in Python, but it also provides a shorthand notation for calculating the modulo of the resulting power.
.as_integer_ratio(), on the other hand, returns a pair of integers whose ratio is equal to the floating-point number. Integers also have their methods. For example, they can tell you how many bits you need to represent them in the binary system or how many of those bits are lit up.
Become a Member to join the conversation.