There is no percentage operator in Python to calculate the percentage.
Calculating Percentage in Python
Method #1: Using Mathematical Formula (Static Input)
For Numbers:
Approach:
- Give the first roll number marks as static input and store it in a variable.
- Give the second roll number marks as static input and store it in another variable.
- Give the third roll number marks as static input and store it in another variable.
- Give the fourth roll number marks as static input and store it in another variable.
- Sum all the roll numbers marks and divide them by total marks, multiply the whole result with 100 to get the percentage, and store it in another variable.
- Print the percentage of all the roll numbers marks.
- The Exit of the Program.
Below is the implementation:
# Give the first roll number marks as static input and store it in a variable. rollno_1 = 90 # Give the second roll number marks as static input and store it in another variable. rollno_2 = 85 # Give the third roll number marks as static input and store it in another variable. rollno_3 = 94 # Give the fourth roll number marks as static input and store it in another variable. rollno_4 = 75 # Sum all the roll numbers marks and divide it by total marks, multiply the whole result # with 100 to get the percentage and store it in another variable. rslt_percent = ((rollno_3 + rollno_2 + rollno_3+ rollno_4)/400)*100 # Print the percentage of all the rollnumbers marks print("The percentage of all the rollnumbers marks = ", rslt_percent,"%")
Output:
The percentage of all the rollnumbers marks = 87.0 %
Method #2: Using Mathematical Formula (User Input)
Approach:
- Give the first roll number marks as user input using the int(input()) function and store it in a variable.
- Give the second roll number marks as user input using the int(input()) function and store it in another variable.
- Give the third roll number marks as user input using the int(input()) function and store it in another variable.
- Give the fourth roll number marks as user input using the int(input()) function and store it in another variable.
- Sum all the roll numbers marks and divide it by total marks, multiply the whole result with 100 to get the percentage and store it in another variable.
- Print the percentage of all the roll numbers marks
- The Exit of the Program.
Below is the implementation:
# Give the first rollnumber marks as user input using the int(input()) function # and store it in a variable. rollno_1 = int(input("Enter some random number = ")) # Give the second rollnumber marks as user input using the int(input()) function and # store it in another variable. rollno_2 = int(input("Enter some random number = ")) # Give the third rollnumber marks as user input using the int(input()) function and # store it in another variable. rollno_3 = int(input("Enter some random number = ")) # Give the fourth rollnumber marks as user input using the int(input()) function and # store it in another variable. rollno_4 = int(input("Enter some random number = ")) # Sum all the rollnumbers marks and divide it by total marks, multiply the whole result # with 100 to get the percentage and store it in another variable. rslt_percent = ((rollno_3 + rollno_2 + rollno_3+ rollno_4)/400)*100 # Print the percentage of all the rollnumbers marks print("The percentage of all the rollnumbers marks = ", rslt_percent,"%")
Output:
Enter some random number = 88 Enter some random number = 92 Enter some random number = 99 Enter some random number = 78 The percentage of all the rollnumbers marks = 92.0 %
For dictionary:
Approach:
- Take an empty dictionary and store it in a variable.
- Give the years list as static input and store it in another variable.
- Loop in the above list of years using the for loop and scan the marks and initialize it to the year key in the dictionary
- Get all the values of the student_marks() list and calculate the sum of marks of a student using the sum() function and store it in another variable.
- Calculate the percentage of all the 3 years marks of a student by dividing the sum of marks by total marks and multiplying the result with 100.
- Store it in another variable.
- Print the percentage of all the 3 years marks of a student.
- The Exit of the Program.
Below is the implementation:
# Take an empty dictionary and store it in a variable. student_marks = {} # Give the years list as static input and store it in another variable. years = ["firstyear","secondyear","thirdyear"] # Loop in the above list of years using the for loop for k in years: #Scan the marks and initialize it to the year key in dictionary student_marks[k] = int(input("Enter some random " + k + " marks number: ")) # Get all the values of the student_marks() list and calculate the sum of marks of # a student using the sum() function and store it in another variable. total_sum = sum(student_marks.values()) # Calculate the percentage of all the 3 years marks of a student by dividing the # sum of marks by total marks and multiplying the result with 100. # Store it in another variable. rslt_percent = (total_sum / 300) * 100 # Print the percentage of all the 3 years marks of a student. print("The percentage of all the 3 years marks of a student =", rslt_percent,"%")
Output:
Enter some random firstyear marks number: 75 Enter some random secondyear marks number: 80 Enter some random thirdyear marks number: 99 The percentage of all the 3 years marks of a student = 84.66666666666667 %