Pythone if, elif, else Statements


What is an if Statement in Python?

In Python, the if statement is a conditional statement used for decision-making. It evaluates a given expression or condition, and if that condition is True, the code block under the if is executed. If it's False, the block is skipped.


Syntax of Python if Statement:

if condition:
    # Code to execute if condition is True
  • condition: A Boolean expression that returns True or False.
  • Indentation is mandatory in Python to define the code block inside if.


How if Statement Works in Python (Control Flow)

The if statement is part of the control flow in Python. Python reads code from top to bottom and takes decisions based on if, elif, and else conditions.



Example 1: Simple if Statement in Python

age = 18

if age >= 18:
    print("You are eligible to vote.")

Output:

You are eligible to vote.

Explanation:

  • The condition age >= 18 is evaluated.
  • Since 18 >= 18 is True, the message is printed.


Example 2: if Statement with Boolean Expression

is_logged_in = True

if is_logged_in:
    print("Welcome back, user!")

Output:

Welcome back, user!

Explanation:

  • is_logged_in is a Boolean variable already set to True.
  • Since the condition evaluates to True, the message is printed.


Real-World Use Case: Python if Statement in Login System

username = "admin"
password = "1234"

if username == "admin" and password == "1234":
    print("Login successful!")

Output:

Login successful!

Explanation:

  • This demonstrates how logical operators (and, or) can be used inside if conditions.
  • Both conditions must be True for the message to be printed.


What is elif in Python?



What is elif in Python?

The elif keyword stands for "else if" and is used in Python conditional statements to check multiple conditions sequentially. It allows you to test multiple expressions without writing multiple if statements.


Syntax of if elif else in Python:

if condition1:
    # Executes if condition1 is True
elif condition2:
    # Executes if condition2 is True
elif condition3:
    # Executes if condition3 is True
else:
    # Executes if none of the above conditions are True
  • Python evaluates conditions from top to bottom.
  • The first condition that is True will execute, and the rest will be skipped.


Example 1: Basic elif Usage in Python

marks = 75

if marks >= 90:
    print("Grade: A+")
elif marks >= 75:
    print("Grade: A")
elif marks >= 60:
    print("Grade: B")
else:
    print("Grade: C")

Output:

Grade: A

Explanation:

  • marks is 75.
  • First condition marks >= 90 is False.
  • Second condition marks >= 75 is True, so "Grade: A" is printed.
  • Remaining conditions are skipped.


Example 2: Real-World Use Case - Weather Condition

temperature = 32

if temperature > 35:
    print("It's very hot!")
elif temperature > 25:
    print("It's warm.")
elif temperature > 15:
    print("It's cool.")
else:
    print("It's cold.")

Output:

It's warm.

How Python Evaluates Multiple Conditions Using elif

Python's elif provides an efficient and readable way to handle multiple conditions without writing nested if statements.



Common Mistakes to Avoid

  • Use elif instead of multiple if blocks to avoid checking unnecessary conditions.
  • Don't forget the colon : after elif condition.
  • Use else at the end for a default case, though it's optional.


Nested Example with Logical Operators

score = 85
attendance = 90

if score >= 90 and attendance >= 90:
    print("Excellent!")
elif score >= 80 and attendance >= 85:
    print("Very Good!")
elif score >= 70:
    print("Good")
else:
    print("Needs Improvement")

Output:

Very Good!


Python else Statement:




What is else in Python?

In Python, the else statement is used as a fallback option when all previous if and elif conditions are False. It helps in handling the default case during decision-making in a program.


Syntax of Python if elif else Statement:

if condition1:
    # Executes if condition1 is True
elif condition2:
    # Executes if condition2 is True
else:
    # Executes if all above conditions are False
  • else does not take any condition.
  • It must be the last block in an if-elif-else structure.
  • It is part of control flow statements in Python.


Example 1: Basic Usage of else

age = 15

if age >= 18:
    print("You can vote.")
else:
    print("You are not eligible to vote.")

Output:

You are not eligible to vote.

Explanation:

  • The condition age >= 18 is False.
  • Hence, the code inside else is executed.


Example 2: else with if and elif

score = 40

if score >= 90:
    print("Excellent")
elif score >= 60:
    print("Good")
else:
    print("Needs Improvement")

Output:

Needs Improvement

Explanation:

  • Both if and elif conditions are False, so else runs.


Real-World Use Case: Login System

username = "admin"
entered_username = "user"

if entered_username == "admin":
    print("Welcome, Admin!")
else:
    print("Access Denied.")

Output:

Access Denied.


Common Mistakes to Avoid with else

  • Do not write a condition with else (e.g., else condition: is invalid).
  • Use else: with no condition, only a colon and an indented code block.
  • else is optional but recommended to handle unexpected inputs.


Summary of Python Conditional Statements

Statement Use Case
if Checks the first condition
elif Checks other conditions if if is False
else Executes when none of the above are True