grade in exam using if else elif in Python

Mark of a student in Exam is taking as input. Based on this mark grade is decided by using if , elif and else.

More than or equal to 80 is A grade
More than or equal to 60 is B grade
More than or equal to 40 is C grade
Less than 40 mark is Fail
m=int(input("Enter your mark "))
if(m>=80):
    print("You got A grade")
elif(m>=60):
    print("You got B grade")
elif(m>=40):
    print("You got C grade")
else:
    print("Failed in this Exam ")
if elif else condition check
Python if else and elif to execute code blocks based on condition checking True or False

Example : Using Ternary Operator for Quick Grade Assignment

marks = 88
grade = 'A' if marks >= 90 else 'B' if marks >= 80 else 'C'
print(f"Marks: {marks}, Grade: {grade}")

Example 6: Calculating Average Mark and Grade for a Class

Here’s how to calculate the average marks for a class and assign a grade based on the average:

marks = [85, 78, 92, 68, 74, 88, 90]

# Calculate the total and average marks
total_marks = sum(marks)
average_marks = total_marks / len(marks)

# Assign grade based on the average
if average_marks >= 90:
    grade = 'A'
elif average_marks >= 80:
    grade = 'B'
elif average_marks >= 70:
    grade = 'C'
elif average_marks >= 60:
    grade = 'D'
else:
    grade = 'F'

print(f"Average Marks: {average_marks:.2f}, Grade: {grade}")
Output
Average Marks: 82.14, Grade: B

View and Download if_else_elif ipynb file ( .html format )
if else elif
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer