The Python GCD function is one of the Python Math function which is used to return the greatest common divisor of two given arguments. In this article we will show you, How to use GCD() function in Python Programming language with example.

## Syntax of a Python GCD Function

The basic syntax of the GCD Function in Python Programming Language is as shown below:

1 |
Math.gcd(x, y); |

**X:**Please specify the X value here.**Y:**Please specify the Y value here. GCD() Function will use these X and Y arguments to find the greatest common divisor.

**NOTE: **

- If both the
*X and Y*arguments are Zero, GCD() function will return the output as.**Zero** - If either the
*X*or Y argument is Zero, GCD() function will return the Non-zero value as the greatest common divisor - If either the
*X*or Y argument is a Decimal, GCD() function will return.**TypeError** - If the
*X value*or Y*value*argument is not a number, GCD() function will return.**TypeError**

### Python GCD Function Example

The Python GCD function is used to return the greatest common divisor of two given arguments. In this example, We are going to find the same for different data types and display the output

**PYTHON CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# Python GCD Function import math Tup = (10, 20, 12, -40 , 50) # Tuple Declaration Lis = [-98, 32, -39, -42 , 15] # List Declaration print('Calculating GCD of Positive Number = %d' %math.gcd(2, 3)) print('Calculating GCD of Negative Number = %d' %math.gcd(-2, 3)) print('Calculating GCD of Zero = %d' %math.gcd(0, 0)) print('Calculating GCD of Zero & Non-Zero element = %d' %math.gcd(2, 0)) print('Calculating GCD of Non-Zero & Zero element = %d' %math.gcd(0, 4)) print('Calculating GCD of of Tuple Item = %d' %math.gcd(Tup[2], 4)) print('Calculating GCD of of Tuple Item = %d' %math.gcd(Tup[2], -6)) print('Calculating GCD of List Item = %d' %math.gcd(Lis[4], 5)) print('Calculating GCD of List Item = %d' %math.gcd(Lis[4], -45)) print('Calculating GCD of Multiple Number = %d' %math.gcd(10 + 20 - 12, 40)) print('Calculating GCD of String Value = ', math.gcd('2', 3)) |

**OUTPUT**

**ANALYSIS**

- At the beginning, We declared Python List and Tuple with some random values.
- Within the first two statements, We passed both the Positive integer and negative integer as the GCD() Function arguments. From the above screenshot you can observe that, GCD() Function is returning output.
- Within the next three statements, We passed both the Zero as first argument and second argument. As we said before, GCD() Function is returning the non zero argument as function output.
- Next four statements, We used the Python Tuple and List items as first arguments and Positive and negative integer values as second argument for GCD() Function. If you observe the above screenshot, GCD() function is working perfectly on them.
- Next statement, We assigned multiple values as the first arguments and the GCD() Function worked without any issue.
- Next, We tried GCD() Function on String value. As we said before, this will return
as output**TypeError** - Within the IDE, We passed the Decimal values as the GCD() Function arguments. As we said before, GCD() Function is returning the
*TypeError: ‘float’ object cannot be interpreted as an integer*

Thank You for Visiting Our Blog

## Share your Feedback, or Code!!