How To Calculate Percentage In Python

How to calculate percentage in Python

To calculate percentage, there are some solutions we have effectively tested. Let’s follow this article, it will help you learn about how to calculate percentage in Python.

How to calculate percentage in Python

In Python, there is no percent operator to calculate percentages, but alternative ways exist. To calculate percentage, we have the following ways:

Use the division operator to combine the multiplication operator

You can use the division operator to get the quotient and multiply that quotient by 100 using the multiplication operator to get the result as a percentage.

Example:

  • Initialize two variables containing integers.
  • Use the division operator to get the quotient.
  • Use the multiplication operator to multiply the quotient by 100, which returns a percentage.
myNumber1 = 1
myNumber2 = 2

# Use operator (/) to get quotient
quotient = myNumber1 / myNumber2 

# Use operator (*) to multiply the quotient by 100
percent = quotient * 100

print('The percentage is:', percent)

Output:

The percentage is: 50.0

Use f-string

New built-in string formatting method from Python 3.6 in the following syntax:

f'a{value:pattern}b

Parameters:

  • f character: use the string f to format the string.
  • a,b: characters to format.
  • {value:pattern}: string elements need to be formatted.
  • pattern: string format.

Example:

  • Initialize two variables containing integers.
  • Use the division operator to get the quotient.
  • Use f-string to format the quotient as a percentage.
myNumber1 = 1.0
myNumber2 = 2.0
quotient = myNumber1 / myNumber2 

# Use f-string to format the quotient as a percentage
print(f'The percentage is: { quotient:.0%}')

Output:

The percentage is: 50%

Use the format() method

You can use the format() method. The format() method returns the formatted result of a given value specified by the specified formatting.

Syntax:

format(value[, format_spec])

Parameters:

  • value: value to be formatted.
  • format_spec: format you want to use for ‘value’.

Example:

  • Initialize two variables containing integers.
  • Use the division operator to get the quotient.
  • Use the format() function to format the quotient as a percentage.
myNumber1 = 1.0
myNumber2 = 2.0
quotient = myNumber1 / myNumber2

# Use the format() function to format the quotient as a percentage
percent = "{:.1%}".format(quotient)

print('The percentage is:', percent)     

Output:

The percentage is: 50.0%

Create a custom function to calculate percentage

You can create a custom function to calculate the percentage if you want.

Example:

def percentage(a, b):
    try:
        # Multiply the quotient by 100 to get the percentage result
        percentage = 100 * float(a) / float(b)
        return str(percentage) + "%"
    except ZeroDivisionError:
        return 0

print("The percentage is: ", percentage(1, 2))

Output:

The percentage is: 50.0%

The fractional function will output a string value.

Summary

Here are the solutions that can help you calculate percentage in Python. If you have any questions about this article, leave a comment below. I will answer your questions. Thank you for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *