Last updated: Apr 9, 2024
Reading timeยท5 min
To calculate a percentage in Python:
/
operator to divide one number by another.100
to get the percentage.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
The 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
.
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:
Name | Description |
---|---|
number | the number to round to ndigits precision after the decimal |
ndigits | the number of digits after the decimal the number should have after the operation (optional) |
print(round((33 / 65) * 100, 2)) # ๐๏ธ 50.77
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.
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
If a ZeroDivisionException
error is raised in the try
block, the except
block runs.
The following function shows how to get the percentage increase/decrease between two numbers.
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.
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.
You might also need to handle the division by zero case.
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.
%
operator to calculate percentageThe third function in the code sample uses the modulo %
operator.
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.
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.
To calculate percentage from user input:
/
operator to divide one number by another.100
to get the percentage.def is_what_percent_of(num_a, num_b): return (num_a / num_b) * 100 num1 = float(input('Enter a number: ')) # ๐๏ธ 25 num2 = float(input('Enter another number: ')) # ๐๏ธ 75 result = is_what_percent_of(num1, num2) print(result) # ๐๏ธ 33.33333333333333 print(round(result, 2)) # ๐๏ธ 33.33
Make sure to use the float() class to convert the input strings to floating-point numbers.
input()
function is guaranteed to return a string even if the user enters a number.Use the round()
function if you need to round the result to N decimal places.
round
function returns the number rounded to ndigits
precision after the decimal point.Note that if you try to divide by 0
, you'd get a ZeroDivisionError
.
If you need to get the percentage increase from one number to another, use the following function.
def get_percentage_increase(num_a, num_b): return ((num_a - num_b) / num_b) * 100 num1 = float(input('Enter a number: ')) # ๐๏ธ 60 num2 = float(input('Enter another number: ')) # ๐๏ธ 30 result = get_percentage_increase(num1, num2) print(result) # ๐๏ธ 100.0 print(round(result, 2)) # ๐๏ธ 100.0
The example shows that the percentage increase from 60
to 30
is 100 %
.
If you calculate the percentage increase from 40
to 100
you'd get -60%
back.
If you always need to get a positive number, use the abs()
function.
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.
You can use a formatted string literal if you need to format a percentage value.
# ๐๏ธ if you need to format an input value to 1 or more decimal places user_input = input('Type a percentage, e.g. 10: ') result = f'{float(user_input) / 100:.1%}' print(result) # ๐๏ธ 10.0%
The example uses a formatted string literal to format an input value to 1 or more decimal places.
user_input = input('Type a percentage, e.g. 10: ') result = f'{float(user_input) / 100:.1%}' print(result) # ๐๏ธ 10.0% result = f'{float(user_input) / 100:.2%}' print(result) # ๐๏ธ 10.00%
f
.Make sure to wrap expressions in curly braces - {expression}
.
We are also able to use the format specification mini-language in expressions in f-strings.
my_float = 0.79 result_1 = f'{my_float:.1%}' print(result_1) # ๐๏ธ 79.0% result_2 = f'{my_float:.2%}' print(result_2) # ๐๏ธ 79.00%
The digit after the period determines how many decimal places the value should have.
The percent %
sign after the digit is used to format the value as a
percentage.
You can learn more about the related topics by checking out the following tutorials: