How to calculate percentage in Python

avatar

Borislav Hadzhiev

Last updated: Jul 9, 2022

banner

Check out my new book

Calculate percentage in Python #

To calculate percentage in Python:

  1. Use the division / operator to divide one number by another.
  2. Multiply the quotient by 100 to get the percentage.
  3. The result shows what percent the first number is of the second.
main.py
def is_what_percent_of(num_a, num_b): return (num_a / num_b) * 100 print(is_what_percent_of(25, 75)) # 👉️ 33.33 print(is_what_percent_of(15, 93)) # 👉️ 16.12903.. print(round(is_what_percent_of(15, 93), 2)) # 👉️ 16.13 # -------------------------------------------------- def get_percentage_increase(num_a, num_b): return ((num_a - num_b) / num_b) * 100 print(get_percentage_increase(60, 30)) # 👉️ 100.0 print(get_percentage_increase(40, 100)) # 👉️ -60.0 # -------------------------------------------------- def get_remainder(num_a, num_b): return num_a % num_b print(get_remainder(50, 15)) # 👉️ 5 print(get_remainder(50, 20)) # 👉️ 10

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, 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((33 / 65) * 100, 2)) # 👉️ 50.77
The round function returns the number rounded to ndigits precision after the decimal point.

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

Note that if you try to divide by 0, you'd get a ZeroDivisionError.

If you need to handle this in any way, use a try/except block to handle the error.

main.py
def is_what_percent_of(num_a, num_b): try: return (num_a / num_b) * 100 except ZeroDivisionError: return 0 print(is_what_percent_of(25, 0)) # 👉️ 0

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(60, 30)) # 👉️ 100.0 print(get_percentage_increase(40, 100)) # 👉️ -60.0

The first example shows that the percentage increase from 60 to 30 is 100 %.

And the second example, shows that the percentage increase from 40 to 100 is -60%.

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(60, 30)) # 👉️ 100.0 print(get_percentage_increase(40, 100)) # 👉️ 60.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.

You might also need to handle the division by zero case.

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(60, 0)) # 👉️ inf print(get_percentage_increase(60, 60)) # 👉️ 0.0 print(get_percentage_increase(60, 120)) # 👉️ 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.

The third function in the code sample uses the modulo % operator.

main.py
def get_remainder(num_a, num_b): return num_a % num_b print(get_remainder(50, 15)) # 👉️ 5 print(get_remainder(50, 20)) # 👉️ 10

The modulo (%) operator returns the remainder from the division of the first value by the second.

main.py
print(10 % 2) # 👉️ 0 print(10 % 4) # 👉️ 2

If the value on the right-hand side is zero, the operator raises a ZeroDivisionError exception.

The left and right-hand side values may also be floating point numbers.

Use the search field on my Home Page to filter through my more than 3,000 articles.