How to Calculate the Percentage in Python?

Python provides several modules and functions that are used to perform mathematical computation in a program. The modules like “math”, “numpy”, and “pandas”, etc., have a large collection of functions that helps Python users to perform any mathematical operations.

This Python guide will cover numerous examples to calculate the percentage in a python program. This article will explain the following topics:

Method 1: Using Division and Multiplication Operator

The division operator “\” and multiplication operator “*” is used to calculate the percentage in Python. The example code to calculate the percentage is shown below:


quotient_value = 25 / 75

percentage_value = quotient_value * 100


In the above code, the division operator “/” calculates the quotient value of the given numbers. The quotient is then multiplied by “100” to obtain the percentage of a number.


The above output shows the calculated percentage of the input number.

Method 2: Using User-Defined Function

In this method, the user-defined function for percentage is defined at the program’s start. Within the function, the multiplication and division operators are used to calculate the percentage.


def percentage(a, b):
    return (a / b) * 100

print(percentage(927, 1050))
print(percentage(853, 1100))

In the above code, the user-defined function named “percentage” is defined in the program. The “percentage()” function accepts two values, “a” and “b”, as a numerator and denominator. If the “percentage()” function is invoked in the python program, it will calculate and retrieve the percentage of the given numbers.


The above output successfully shows the calculated percentage of the input numbers.

Bonus Tip: Calculate Percentage by Taking Input From User

Using the user-defined function, we can also calculate the percentage of numbers taken from the user. The example code is given below:


def percentage(a, b):
    return (a / b) * 100

a = int(input('Enter First Number: '))
b = int(input('Enter Second Number: '))

print('Percentage of Number is: ', percentage(a, b), '%')

In the above code:

  • A user-defined function named “percentage” is created using the def keyword.
  • The percentage function will accept two values as arguments.
  • The input() function takes the input from the user.
  • The “int()” function is used along with the “input()” function to convert the string numeric number into an integer.

Note: The “input()” function returns the value in strings, which can not be used for mathematical calculation. So we must convert it into an integer before executing any mathematical operation. Therefore the int() function is used in the above coding example.


The above output successfully calculates the percentage of user-input numeric numbers.


To calculate the percentage, the “simple division and multiplication operator” or the “user-defined function” is used in Python. The user-defined function has an expression inside the function that returns the percentage of the number when the function is accessed in the program. This article has presented a detailed guide on calculating the percentage in Python using appropriate examples.