How to calculate a Percentage in Python
Last updated: Apr 9, 2024 Reading time · 5 min
# Table of Contents
- Calculate percentage in Python
- Rounding to N digits after the decimal when calculating a percentage
- Getting the percentage increase/decrease between two numbers
- Calculate a Percentage from User Input in Python
- Calculate a Percentage Increase/Decrease from User Input in Python
- Formatting a Percentage Value
# Calculate percentage in Python
To calculate a percentage in Python:
- Use the division / operator to divide one number by another.
- Multiply the quotient by 100 to get the percentage.
- The result shows what percent the first number is of the second.
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 .
# Rounding to N digits after the decimal when calculating a percentage
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:
If ndigits is omitted, the function returns the nearest integer.
Note that if you try to divide by 0 , you'd get a ZeroDivisionError .
# Handling a potential ZeroDivisionError exception
If you need to handle this in any way, use a try/except block to handle the error.
If a ZeroDivisionException error is raised in the try block, the except block runs.
# Getting the percentage increase/decrease between two numbers
The following function shows how to get the percentage increase/decrease between two numbers.
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.
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.
If we get a ZeroDivisionError error, we return Infinity, however, you can handle the error in any other way that suits your use case.
# Using the modulo % operator to calculate percentage
The third function in the code sample uses the modulo % operator.
The modulo (%) operator returns the remainder from the division of the first value by the second.
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.
# Calculate a Percentage from User Input in Python
To calculate percentage from user input:
- Convert the input values to floats.
Make sure to use the float() class to convert the input strings to floating-point numbers.
Use the round() function if you need to round the result to N decimal places.
# Calculate a Percentage Increase/Decrease from User Input in Python
If you need to get the percentage increase from one number to another, use the following function.
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.
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.
# Formatting a Percentage value
You can use a formatted string literal if you need to format a percentage value.
The example uses a formatted string literal to format an input value to 1 or more decimal places.
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.
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.
# Additional Resources
You can learn more about the related topics by checking out the following tutorials:
- Round a Float to 1, 2 or 3 Decimal places in Python
- Round a number to the nearest 5, 10, 100, 1000 in Python
- Format a Number to a fixed Width in Python
- Format number with comma as thousands separator in Python
Borislav Hadzhiev
Web Developer
Copyright © 2024 Borislav Hadzhiev