Python Conditional Statements (if, if-else, elif) – Complete Beginner's Guide 2026

 Learn Python if, if-else, elif Statements with Examples

Python if Statement

Introduction

Conditional statements are one of the most important concepts in Python programming. They allow a program to make decisions based on different conditions. Just like humans make decisions in daily life—for example, carrying an umbrella if it is raining—Python programs also use conditional statements to decide which block of code should execute.

Python provides three main conditional statements:

  1. if Statement
  2. if-else Statement
  3. if-elif-else Statement

These statements help developers create dynamic applications such as login systems, grading systems, calculators, banking applications, games, and data validation programs.

In this tutorial, you'll learn Python conditional statements from basic to advanced with syntax, flow explanation, multiple examples, interview questions, FAQs, and practical use cases.

First Python Program


Table of Contents

  1. What are Conditional Statements?
  2. Why Use Conditional Statements?
  3. Python if Statement
  4. Python if-else Statement
  5. Python if-elif-else Statement
  6. Nested if Statement
  7. Comparison Operators
  8. Logical Operators
  9. Real-Life Examples
  10. Common Mistakes
  11. Best Practices
  12. Advantages
  13. Interview Questions
  14. FAQs
  15. Conclusion

What are Conditional Statements? 

Conditional statements are decision-making statements in Python. They execute different blocks of code depending on whether a condition is True or False.

Example:

If your age is greater than or equal to 18, you can vote.

Similarly, Python checks conditions before executing code.


Why Use Conditional Statements? 

Conditional statements help to:

  • Make decisions
  • Validate user input
  • Create login systems
  • Build games
  • Calculate grades
  • Banking applications
  • AI decision making
  • Automation scripts

Python if Statement 

The if statement executes a block only when the condition is True.

Syntax 

if condition:

    statement


Example 1 

age = 20

 

if age >= 18:

    print("You are eligible to vote.")

Output

You are eligible to vote.

Explanation

Python checks whether age is greater than or equal to 18.

Condition:

20 >=18

Result:

True

So Python executes the print statement.


Example 2

number = 10

 

if number > 0:

    print("Positive Number")

Output

Positive Number


Python if-else Statement 

The if-else statement executes one block if the condition is True and another block if it is False.


Syntax 

if condition:

    statement

else:

    statement


Example

age = 15

 

if age >= 18:

    print("Eligible")

else:

    print("Not Eligible")

Output

Not Eligible


Explanation

Condition:

15 >=18

Result

False

Python skips the if block and executes the else block.


Python if-elif-else Statement 

When multiple conditions need to be checked, Python uses elif.


Syntax 

if condition:

    statement

 

elif condition:

    statement

 

else:

    statement


Example 

marks = 75

 if marks >= 90:

    print("Grade A")

 elif marks >= 75:

    print("Grade B")

 elif marks >= 50:

    print("Grade C")

 else:

    print("Fail")

Output

Grade B


Explanation

Python checks conditions from top to bottom.

  • 75 >=90 ❌
  • 75 >=75 ✅

Remaining conditions are ignored.


Nested if Statement 

A nested if means placing  if statement inside another.

Example 

age = 25

citizen = True

 if age >= 18:

    if citizen:

        print("Eligible to Vote")

Output

Eligible to Vote


Comparison Operators

Operator

Meaning

==

Equal

!=

Not Equal

> 

Greater Than

< 

Less Than

>=

Greater Than or Equal

<=

Less Than or Equal


Logical Operators 

Operator

  Meaning

and

   Both conditions True

or

    Any condition True

not

   Reverse condition


Example

age = 22

citizen = True 

if age >=18 and citizen:

    print("Eligible")

Output

Eligible


Real-Life Examples 

Student Grade System

marks = int(input("Enter Marks: "))

if marks >=90:

    print("Grade A") 

elif marks >=75:

    print("Grade B") 

elif marks >=50:

    print("Grade C") 

else:

    print("Fail")

output:

Enter Number: 16

Even


Even Odd Checker

number = int(input("Enter Number: ")) 

if number %2==0:

    print("Even")

else:

    print("Odd")

Output

Enter Number: 16

Even


Login Verification

password = "python123" 

user = input("Enter Password: ") 

if user == password:

    print("Login Successful")

else:

    print("Invalid Password")

Output:

Enter Password: python123

Login Successful


Common Mistakes 

  • Forgetting the colon (:)
  • Incorrect indentation
  • Using = instead of ==
  • Wrong condition order in elif
  • Missing else when needed

Best Practices

  • Keep conditions simple.
  • Use meaningful variable names.
  • Avoid deeply nested if statements.
  • Test all possible conditions.
  • Add comments for readability.

Advantages 

  • Makes programs intelligent.
  • Easy to understand.
  • Supports complex decision making.
  • Improves code readability.
  • Essential for real-world applications.
  • Used in automation, AI, and web development.
  • Reduces repetitive code.

Interview Questions 

  1. What is an if statement in Python?
  2. What is the difference between if and if-else?
  3. When should you use elif?
  4. What is a nested if statement?
  5. Explain logical operators with examples.
  6. What happens if multiple elif conditions are True?
  7. Difference between = and ==?
  8. Can if exist without else?
  9. What are comparison operators?
  10. What is indentation in Python?

FAQs 

1. What is an if statement?

It executes code only when a condition is True.

2. Can we have multiple elif statements?

Yes, Python supports multiple elif blocks.

3. Is else mandatory?

No. else is optional.

4. Can if be nested?

Yes.

5. Which operator checks equality?

==

6. Which operator means "not equal"?

!=

7. Can we combine conditions?

Yes, using and, or, and not.

8. Why is indentation important?

Indentation defines the code block in Python.

9. What is the difference between if and elif?

if starts the condition check, while elif checks additional conditions only if previous ones are False.

10. Where are conditional statements used?

They are used in login systems, form validation, games, calculators, banking applications, and many other real-world programs.


Conclusion

Python conditional statements (if, if-else, and if-elif-else) are fundamental building blocks of programming. They allow programs to make decisions, execute different actions based on conditions, and solve real-world problems efficiently. By understanding syntax, comparison operators, logical operators, and nested conditions, you can create interactive and intelligent Python applications. Practice these concepts with different examples to build a strong programming foundation.

Comments

Popular posts from this blog

HTML Tag

CSS Text Color Explained with Syntax and HTML Examples

HTML Input Type Submit Syntax and Example