Calculate Percentage between two numbers in Python

avatar

Borislav Hadzhiev

Last updated: Jul 9, 2022

banner

Photo from Unsplash

Calculate Percentage between two numbers in Python #

To calculate the percentage between two numbers, divide one number by the other and multiply the result by 100, e.g. (30 / 75) * 100. This shows what percent the first number is of the second. In the example, 30 is 40% of 75.

main.py
def is_what_percent_of(num_a, num_b): return (num_a / num_b) * 100 print(is_what_percent_of(30, 75)) # 👉️ 40.0 print(is_what_percent_of(20, 75)) # 👉️ 26.6666... print(round(is_what_percent_of(20, 75), 2)) # 👉️ 26.67 # -------------------------------------------------- def get_percentage_increase(num_a, num_b): return ((num_a - num_b) / num_b) * 100 print(get_percentage_increase(50, 30)) # 👉️ 66.6666... print(get_percentage_increase(50, 100)) # 👉️ -50.0 # -------------------------------------------------- def get_percentage_difference(num_a, num_b): # 👇️ use abs() function to always get positive number return (abs(num_a - num_b) / num_b) * 100 print(get_percentage_difference(50, 30)) # 👉️ 66.6666... print(get_percentage_difference(50, 100)) # 👉️ 50.0

The first function takes 2 numbers and returns what percent the first number is of the second.

For example, 25 / 50 * 100 shows that 25 is 50% of 50.

main.py
print((25 / 50) * 100) # 👉️ 50.0

When calculating percentages between two numbers, you might need to round to a specific number of digits after the decimal.

The round function takes the following 2 parameters:

NameDescription
numberthe number to round to ndigits precision after the decimal
ndigitsthe number of digits after the decimal the number should have after the operation (optional)
main.py
print(round((23 / 43) * 100, 2)) # 👉️ 53.49
The round function returns the number rounded to ndigits precision after the decimal point.

If ndigits is omitted, the function returns the nearest integer.

The second function shows how to get the percentage increase / decrease between two numbers.

main.py
def get_percentage_increase(num_a, num_b): return ((num_a - num_b) / num_b) * 100 print(get_percentage_increase(50, 30)) # 👉️ 66.6666... print(get_percentage_increase(50, 100)) # 👉️ -50.0

The first example shows that the percentage increase from 30 to 50 is 66.6666...%.

And the second shows that the percentage increase from 100 to 50 is -50%.

If you always need to get a positive number, use the abs() function.

main.py
def get_percentage_increase(num_a, num_b): return (abs(num_a - num_b) / num_b) * 100 print(get_percentage_increase(50, 30)) # 👉️ 66.6666... print(get_percentage_increase(50, 100)) # 👉️ 50.0

The abs function returns the absolute value of a number. In other words, if the number is positive, the number is returned, and if the number is negative, the negation of the number is returned.

This way we are always guaranteed to get a positive number calculating the difference in percentage between two numbers.

Something you might want to handle is division by 0. Division by 0 raises a ZeroDivisionError in Python.

We can handle the error in a try/except block.

main.py
def get_percentage_increase(num_a, num_b): try: return (abs(num_a - num_b) / num_b) * 100 except ZeroDivisionError: return float('inf') print(get_percentage_increase(50, 0)) # 👉️ inf print(get_percentage_increase(50, 50)) # 👉️ 0.0 print(get_percentage_increase(50, 100)) # 👉️ 50.0

If we get a ZeroDivisionError error, we return Infinity, however you can handle the error in any other way that suits your use case.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.