Skip to main content

Python Conditional Statements (if, elif, else) with Examples

 ✍️Introduction

In programming, we often need to make decisions.

For example:

• If marks ≥ 40 → Pass

• Else → Fail


Python uses conditional statements to make such decisions.


In this article, you will learn:

  • What conditional statements are
  • if, elif, else statements
  • Simple and clear examples

What are Conditional Statements in Python?

Conditional statements allow a program to execute different code blocks based on conditions.

Python checks the condition:

• If True → code runs

• If False → code is skipped

1️⃣ if Statement

The if statement executes code only if the condition is true.

Syntax:

if condition:
    statement

Example:

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

2️⃣ if–else Statement

The else block runs when the if condition is false.

Syntax:

if condition:
    statement
else:
    statement

Example:

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

3️⃣ if–elif–else Statement

Used when there are multiple conditions.

Syntax:

if condition1:
    statement
elif condition2:
    statement
else:
    statement

Example:

marks = 75

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

4️⃣ Nested if Statement

An if inside another if is called a nested if.

Example:

age = 20
citizen = True

if age >= 18:
    if citizen:
        print("Eligible to vote")

Comparison Operators Used in Conditions

      Operator                         Meaning

           ==                                   Equal to
           !=                                   Not equal
           >                                   Greater than
          <                                      Less than
         >=                            Greater than or equal
        <=                               Less than or equal


Logical Operators in Conditions

       Operator                     Meaning

         and                     Both conditions true
         or                      Any one condition true
        not                           Reverse result

Example:

age = 25
if age > 18 and age < 60:
    print("Working age group")

❌ Common Mistakes by Beginners

❌ Forgetting colon :

❌ Wrong indentation

❌ Using = instead of ==


Correct example:

if a == 10:
    print("Correct")

🎯 Why Conditional Statements are Important?

  • Used in decision making
  • Core concept of programming
  • Important for exams & interviews
  • Used in real-life applications

Real-Life Example

balance = 500

if balance >= 1000:
    print("Withdraw allowed")
else:
    print("Insufficient balance")


Conclusion

Python conditional statements help programs think and decide.
Understanding if, elif, and else is very important for writing logical programs.

Practice more examples to master this topic.

💬 Let’s Interact!

What will be the output of this code?

x = 10
if x > 5:
    print("Yes")
else:
    print("No")

Comment your answer 👇😊


📌 Related Articles:



Comments

popular

What Is Python Programming? Features, Uses and Career Scope.

  ✍️ Introduction Python is one of the most popular and beginner-friendly programming languages in the world today. It is widely used for web development, data science, artificial intelligence, automation, and software development. Because of its simple syntax and powerful features, Python is an excellent choice for students, beginners, and professionals. In this article, you will learn what Python programming is, its key features, real-life uses, and the career opportunities it offers. 🧠 What Is Python Programming? Python is a high-level, interpreted programming language created to make coding easy and readable. It allows developers to write programs using simple English-like statements, which makes it ideal for beginners. Python supports multiple programming styles such as procedural, object-oriented, and functional programming. ⭐ Features of Python Easy to learn and understand Simple and readable syntax Interpreted language Platform independent Large standard l...

what is Python Date and Time ? Complete Guide with Examples:

  ✍️ Introduction Python provides powerful tools to work with dates and times . The most commonly used module is datetime . The datetime module allows Python programs to create, manipulate, format, and perform calculations with dates and times easily. Date and time handling is used in: Logging systems Attendance & billing systems Data analysis Real-time applications How To Import Date and Time Module : In Python, the datetime module is used to work with dates , times , and date–time combinations . It is part of Python’s standard library, so no installation is required. You can import it in different ways depending on your need. import datetime Get Current Date and Time: This code obtains the current system date and time as a single datetime object using the datetime module. Example: import datetime now = datetime.datetime.now() print (now) Output: 2026-02-13 11:25:53.445827 "Code executed successfully"  Get Only Date: This code retrieves t...

Features of Python Programming Language Explained with Examples

✍️ Introduction Python is one of the most popular programming languages because of its powerful and easy-to-use features. It is designed to make programming simple, readable, and efficient. Python is widely used by beginners as well as professionals due to its flexibility and strong community support. In this article, we will discuss the main features of Python programming language in simple English with easy examples. 🧠 What Are Features of Python? Features of Python are the special characteristics that make it different from other programming languages. These features help programmers write clean, readable, and efficient code with less effort. ⭐ Key Features of Python Programming Language 1️⃣ Easy to Learn and Easy to Use Python has a simple syntax that is close to the English language. This makes it very easy for beginners to learn and understand programming concepts. Example: print("Hello, World!") 2️⃣ Interpreted Language Python is an interpreted language, which me...

Python Operators: Types with Examples

 ✍️Introduction In Python, operators are symbols used to perform operations on variables and values. Operators help us do calculations, comparisons, and logical decisions in programs. In this article, you will learn: • What Python operators are • Types of Python operators • Simple examples for each type