🟢 Beginner  ·  Lesson 08

if, elif and else Statements

if, elif और else Statements

What are if, elif and else Statements?

if, elif and else Statements means conditional statements help a program make decisions based on conditions.

In real programs, this topic helps in grade calculation. Learn the idea first, then type the program yourself and compare the output.

💡 At a Glance
PointDetails
Course AreaCore Python
Basic programming concepts used to write Python programs.
Main Usegrade calculation
Example Fileif-else.py
Practice FocusRun, change values, and explain the output line by line.

Why should you learn this?

  • It is useful for grade calculation.
  • It connects with pass/fail logic.
  • It improves your ability to read, write and debug Python programs.

Important Terms

These terms are used directly in this lesson. Understand them before memorising the code.

TermMeaning
conditionA true/false expression used for decision making.
ifStarts a decision block that runs only when the condition is true.
elifChecks another condition if the previous if/elif was false.
elseRuns when none of the previous conditions are true.
decision makingChoosing different actions based on different conditions.

Syntax / Basic Pattern

The simple pattern is: prepare data, apply the concept, then show the result.

Basic Pattern
marks = 72
if marks >= 90:
    grade = "A+"
elif marks >= 75:
    grade = "A"
elif marks >= 60:
    grade = "B"
else:

Complete Example Program

Python – if-else.py
marks = 72

if marks >= 90:
    grade = "A+"
elif marks >= 75:
    grade = "A"
elif marks >= 60:
    grade = "B"
else:
    grade = "Needs improvement"

print("Grade:", grade)

Expected Output

Grade: B

Program Explanation

  • marks = 72 stores a value in marks.
  • if marks >= 90: checks a condition and runs the indented block when it is true.
  • grade = "A+" stores a value in grade.
  • elif marks >= 75: checks a condition and runs the indented block when it is true.
  • grade = "A" stores a value in grade.
  • elif marks >= 60: checks a condition and runs the indented block when it is true.
  • grade = "B" stores a value in grade.

Where will you use it?

  • Grade calculation.
  • Pass/fail logic.
  • Menu-based applications.

Common Mistakes

  • Writing code with wrong indentation.
  • Using input() value directly in calculations without converting it to int or float.
  • Using unclear variable names that make the program difficult to understand.

Practice Tasks

  1. Type the program in if-else.py and run it.
  2. Change input values or sample data and observe the new output.
  3. Create one example related to grade calculation.
  4. Write 5 lines explaining the logic in your own words.

Summary

if, elif and else Statements is not a theory-only topic. You should be able to explain the meaning, write the example, run it successfully, and use it in a small practical program.

if, elif और else क्या है?

if, elif और else ka matlab hai: Conditional statements help a program make decisions based on conditions. Simple words me, ye topic practical Python programs likhne me direct use hota hai.

Is topic ko sirf definition ke liye nahi, balki grade calculation jaise real examples ke liye practice karein.

यह क्यों सीखना जरूरी है?

  • Ye grade calculation me kaam aata hai.
  • Ye pass/fail logic se bhi connected hai.
  • Isse aap code ka output aur errors better samajh paate hain.

Important Terms

TermMeaning
conditionA true/false expression used for decision making.
ifStarts a decision block that runs only when the condition is true.
elifChecks another condition if the previous if/elif was false.
elseRuns when none of the previous conditions are true.
decision makingChoosing different actions based on different conditions.

Syntax / Basic Pattern

Basic idea: pehle data तैयार करें, phir Python logic apply करें, aur finally result display करें.

Basic Pattern
marks = 72
if marks >= 90:
    grade = "A+"
elif marks >= 75:
    grade = "A"
elif marks >= 60:
    grade = "B"
else:

Complete Example Program

Python – if-else.py
marks = 72

if marks >= 90:
    grade = "A+"
elif marks >= 75:
    grade = "A"
elif marks >= 60:
    grade = "B"
else:
    grade = "Needs improvement"

print("Grade:", grade)

Expected Output

Grade: B

Program Explanation

  • marks = 72 stores a value in marks.
  • if marks >= 90: checks a condition and runs the indented block when it is true.
  • grade = "A+" stores a value in grade.
  • elif marks >= 75: checks a condition and runs the indented block when it is true.
  • grade = "A" stores a value in grade.
  • elif marks >= 60: checks a condition and runs the indented block when it is true.
  • grade = "B" stores a value in grade.

Practical Uses

  • Grade calculation.
  • Pass/fail logic.
  • Menu-based applications.

Common Mistakes

  • Writing code with wrong indentation.
  • Using input() value directly in calculations without converting it to int or float.
  • Using unclear variable names that make the program difficult to understand.

Practice Tasks

  1. Program ko if-else.py file me type karke run karein.
  2. Values change karke output compare karein.
  3. grade calculation par ek छोटा example banayen.
  4. Logic ko apne words me 5 lines me likhein.

सारांश

if, elif and else Statements ko tab complete maanenge jab aap iska meaning, example, output aur practical use clearly explain kar saken.

← Back to Python Tutorial