Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

New math Functions in Python 3.8

You can copy-paste this variable assignment to follow along at this point in the lesson:

Python
x = 89837610936485038264990264309287262940457

00:00 In this lesson, we’ll take a look at the functions that were added to the math module in Python 3.8.

00:07 The math module saw some significant improvements in Python 3.8. Five functions were added and one was extended. Two counting functions were added, and these are comb() and perm(). comb(), short for combinations, computes binomial coefficients.

00:24 So when you call comb() with the integers n and k, it’s going to compute the number of ways to choose k items from a collection of n items, and your selections are done so that you have no repetition in your elements that you select, and the order doesn’t matter.

00:43 The number of ways to do that is n factorial divided by k factorial times (n - k) factorial. The other counting function that was added is perm().

00:55 When you call perm() with n and k, it computes the number of ways to choose k items from n items without repetition, but where the order does matter.

01:06 The number of ways to do that is n factorial divided by (n - k) factorial. An interesting function that was added to the math module is isqrt(), and this returns mathematically the floor of the square root of x when you’re calling it with the input variable x.

01:25 So, this will return the largest integer whose square is less than or equal to x.

01:32 Next up is prod(), and prod is short for product. This does what it sounds like it does. It computes the product of the elements of the iterable. So instead of sum, this would return the product of the elements. And then lastly, there’s dist().

01:49 dist() is short for distance, and dist() will compute the Euclidean distance between the points p and q. And so here, p and q, they’re going to be iterables.

02:00 They have to have the same number of elements so that the computation of the distance can be done properly because it’s going to be subtracting coordinate by coordinate, squaring the difference, and then taking the square root.

02:14 So there has to be the same number of elements in each of the iterables. The last function that I want to mention is hypot(). Now, this function wasn’t added in Python 3.8, but it was extended in 3.8.

02:27 What was done is that support for N-dimensional points was added. What this function does is it computes the Euclidean norm of a point specified by the coordinates that are passed in as the arguments.

02:42 All right, let’s take a look at some of these.

02:45 Let’s start off with the counting functions with comb(). So, let’s try 545, 23. This is computing the number of ways that you can select 23 elements from a collection of 454 elements in such a way that when you make a selection, those 23 items that you choose, you do so without repetition and the order in which you choose them doesn’t matter.

03:12 That’s a pretty big number. Let’s try even bigger. Say 1333, 432,

03:23 Yikes. Now, comb() and perm()—the input integers, they have to be positive. So if you pass in, say, a negative integer for either one of the arguments,

03:37 you’re going to get a ValueError. And so n in this case, which is the name given to the first argument, must be a non-negative integer.

03:44 If you pass a negative for the second argument, then here, k must be a non-negative integer.

03:53 Now let’s try out this integer square root function. I’m going to include in the video description this number, but you can try it out for yourself and just input a really large number just to see how the isqrt() function is a lot more accurate than a naive implementation. Let’s save in the variable y_isqrt, we want to compute the integer square root of this number x.

04:24 What this means is that if we square this y_isqrt and we compare it with x, it’s going to be less than or equal to this x.

04:36 Now let’s take a look at what the square is.

04:41 This number is less than or equal to x. Now let’s try this with a naive implementation of the integer square root operation. So if you remember, this is mathematically computing the floor of the square root of x, and let’s save this in y_sqrt.

05:04 And then let’s see if when we square this number, whether that’s less than or equal to x. We get True as well. Now, the integer square root that we computed with the built-in function in the math module should be the same as the naive implementation, but we get False, so let’s take a look at these.

05:31 If you visually compare these numbers, you see there’s actually a big difference. Let’s compute the difference.

05:38 It’s a difference of 16,114. Now, which one’s right? Let’s clear this up and print these out again. The value that we computed using the built-in isqrt() function, it’s bigger than the value obtained using the naive implementation, and so the one with isqrt() is definitely a lot closer to what the definition is supposed to be, which is the largest integer whose square is less than or equal to the input value x. Now, if we add 1 more to the one computed with isqrt() and we square this, is this less than or equal to x?

06:21 You get False. And so, yes, it means that the value computed with the isqrt() function is indeed the largest integer whose square is less than or equal to x.

06:37 So, this is a good example of why, if you want to do some of these computations in your application, you may want to first take a look at what are the functions in the math module that you may need or you may want to use instead of implementing your own versions.

06:53 In the next lesson, we’ll take a look at what you can do if you’re working on a problem that requires complex numbers.

Become a Member to join the conversation.