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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Other Important math Functions: gcd()

For more information about concepts covered in this lesson, you can check out:

00:00 There are a lot of other functions in the math module. Let me just show you a few, and then in the next lesson, we’ll talk about some of the newer functions that appeared in Python 3.8.

00:12 The first function we’ll talk about is gcd(). This is short for the Greatest Common Divisor, and it takes a variable number of integers and it computes the greatest common divisor of those numbers. Now, usually, you want to compute the greatest common divisor of two integers, but gcd() actually takes either one integer or zero integers, In the case where zero integers are passed, then it returns 0, and if you just pass in one integer, it’s going to return that same integer.

00:44 Now, prior to Python version 3.9, gcd() only took in two arguments, whereas starting with version 3.9, it does take an arbitrary number of arguments.

00:55 So just be aware of that in case you’re using a version of Python prior to 3.9. Next, we’ve got fsum(). fsum() takes an iterable as the argument, and it’s very similar to the sum() function: It computes the sum of the numbers in the iterable, but we’ll see that it’s a little bit more accurate than the sum() function that’s built into Python. Then we’ve got sqrt(), which is short for square root. this computes the square root of a number, and it has to be a positive number or 0.

01:24 Otherwise, it’s going to return a ValueError because the functions in the math module only deal with real numbers and not complex numbers.

01:32 Then there are a couple of functions that convert from degrees to radians. The degrees() function converts to degrees from radians, and the radians() function converts to radians from degrees. In most computations with trigonometric functions, the inputs are interpreted as radians or are assumed to be radians, and so if you’re working with degrees, you’ll want to convert to radians.

01:56 And then we’ve got your favorite trig functions like cosine, sine, and tangent. These compute their values on the assumption that the input is in radians, and so, again, you may want to use the radians() function to do that if you’re working initially with degrees.

02:13 And then you’ve got the inverses of the sine, cosine, and tangent functions. These are acos()—that’s the arccosine of x, asin()that’s the inverse of sine, and then atan()that is the inverse or arctangent of x. Now, I’ll mention this right here that the acos() and asin(), they have domain limitations on the input.

02:35 These inputs can only be between -1 and 1, whereas for the atan() function, the input x can be any real number.

02:44 All right, let’s try a few of these.

02:47 Let’s start off with gcd(). The gcd() function will compute the greatest common divisor of an arbitrary number of integers. If you don’t pass any integers, you’ll get zero. If you pass in one integer,

03:05 then you just get the integers back. And if you pass in two, then this is what you may have learned about the gcd(), is that it finds the greatest common divisor of two integers. In this case, you get 64.

03:18 Now let’s add another one, say, 24.

03:23 Then you get 8. So this is kind of cool because if you have a list of integers and you want to find the greatest common divisor of all of the numbers in the list, then you can use the gcd() function.

03:36 And if you’re familiar with the unpacking operator (*) in Python, then you can use that instead of having to do, say, some sort of recursion or a reducing type of operation. Let me show you what I mean.

03:50 Here’s a list of integers, and if you wanted to find the greatest common divisor of these numbers, you could just put these in one by one… Oh, this is kind of slow! Hey, why don’t we use the unpacking operator?

04:04 So, if you’re not familiar with it, there will be a link underneath the video description there for you to read up. But basically, if you have an iterable and you want to use those iterable elements, the elements in the iterable, as arguments to a function that takes on an arbitrary number or however many number of elements in the iterable, then you can use this star or unpacking operator.

04:27 And so that’s what we get, 8! So, this would have been the same if you had written these out one by one as arguments to the gcd() function.

04:41 So, that’s kind of cool with the gcd(). Again, this is only starting with Python version 3.9.

04:48 Now let’s take a look at the fsum() function. Actually, why don’t you grab a drink, come back, and then in the next lesson, we’ll finish off with some of the other functions in the math module.

Become a Member to join the conversation.