How to calculate percentage in Python

In this tutorial, we are going to learn about finding the percentage in Python.

Example 1 :

A student got 91 marks in Math, 100 on the computer, 98 in Science, out of 100. Find the percentage of marks occupied by him.

x = 91 + 100 + 98

x = 289

x = (289/300)*100

x = 96.33%

Solving it using Python:

Math = 91
Science = 98
Computer = 100
z = ((Math + Science + Computer)/300)*100
print(z,"%")

Output :

96.33 %

Example 2:-

Create a dictionary for subjects, take value from the user and gives the percentage of marks occupied by him.

marks = {}
subjects = ["Maths","Science","Computer"]


for i in subjects:
   marks[i] = int(input("Input the " + i + " marks: "))


total = sum(marks.values())
percentage = (total / 300) * 100

print ("percentage = ",percentage,"%")

We created a dictionary “subject” and marks will be stored as its value. Taking input from the user and calculating the percentage of marks occupied by him.

Output:

Input the Maths marks: 91
Input the Science marks: 98
Input the Computer marks: 100
percentage =  96.33333333333334 %

In this article, we learned about how to find percentage in Python by various method like normal method and by using a dictionary. If you have any queries please comment below.

Also read: How to use numpy.percentile() in Python

Leave a Reply

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