Skip to main content

Decision Making in Python (if-else)

Normally, Python executes your code line-by-line from top to bottom. But real-world software needs to make decisions based on rules.

For example:

  • If a student gets 35 marks or more → They Pass.
  • Else (otherwise) → They Fail.

To achieve this, we use Conditional Statements.


1. What is a "Condition" vs a "Conditional Statement"?

Before writing code, let's clarify two core terms:

  • Condition: A logical test that evaluates to either True or False (e.g., marks >= 35).
  • Conditional Statement: The decision-making construct (if, if-else) that controls which block of code to run based on the condition.

2. Visual Structure: Anatomy of an if Statement

Understanding how an if statement is structured visually helps prevent syntax and indentation errors:

Syntax Structure of an if Statement

ifcondition:
INDENTATION(4 Spaces)
CONDITIONAL BLOCKBlock of code (Executes ONLY if condition is True)

3. The 3 Golden Rules of if Syntax

  1. The Condition: The logical test that produces True or False.
  2. The Colon (:): Placed at the end of the if line. It signals to Python: "Attention! A new block of code starts on the next line."
  3. Indentation (4 Spaces): Spacing in front of the conditional block. It is used to identify which lines belong inside the conditional block.

4. The Simple if Statement (One Choice)

The if statement checks a condition. If the condition is True, the conditional block runs. If it is False, Python completely skips it.

marks = 75

if marks >= 35:
print("Congratulations!")
print("You passed the exam.")

print("Program complete.")

Output:

Congratulations!
You passed the exam.
Program complete.

5. The if-else Statement (Two Choices)

What if you want one action when the condition is True, and a fallback action when it is False? We add an else block.

Syntax Structure of an if-else Statement

ifcondition:
INDENTATION(4 Spaces)
IF BLOCKExecutes ONLY if condition is True
else:
INDENTATION(4 Spaces)
ELSE BLOCKExecutes ONLY if condition is False
marks = 28

if marks >= 35:
print("Result: Pass")
else:
print("Result: Fail")

How it executes:

  • If marks >= 35 is True, only the if block runs.
  • If marks >= 35 is False, only the else block runs.
  • Notice that both if and else end with a colon (:), and their blocks are indented by 4 spaces.

6. What is Indentation & Possible Mistakes

In languages like C, Java, or JavaScript, curly braces { } are used to group code. Python does not use curly braces. Instead, Python uses Indentation (spaces) to define a code block.

marks = 80

if marks >= 35:
print("Line 1: Inside IF block") # Indented -> Inside IF
print("Line 2: Inside IF block") # Indented -> Inside IF

print("Line 3: Outside IF block") # NOT indented -> Always runs

Common Mistakes to Avoid:

❌ Mistake 1: Missing Colon (:)

if marks >= 35 # Missing colon at the end!
print("Pass")
# SyntaxError: expected ':'

❌ Mistake 2: Missing Indentation

if marks >= 35:
print("Pass") # Not indented!
# IndentationError: expected an indented block after 'if' statement

Quick Summary

  • Condition Check: Evaluates an expression to either True or False.
  • The Colon (:): Mandatory at the end of every if and else header line.
  • Indentation (4 spaces): Python uses indentation to group statements inside the conditional block.
  • The else Block: Executes as the default fallback when the if condition is False.

What's Next?

Now that you understand basic if-else logic, let's learn how to handle multiple choices using elif chains in the next lesson!