Skip to main content

Command Palette

Search for a command to run...

Conditional Statements in Python

Published
3 min read
Conditional Statements in Python

🔹 Introduction

Conditional statements are very helpful in any programming language. They allow us to execute certain parts of code only when specific conditions are met.

Think of them as decision-making tools in programming:

  • If it rains, take an umbrella.

  • If it’s summer, wear light-colored clothes to stay cool.

In Python, the main conditional statements are:

  • if

  • if-else

  • if-elif-else

  • Nested if


🔹 Precautions

While writing conditional statements in Python, remember:

  • Indentation (spaces) is mandatory.

  • Don’t forget the colon : at the end of each condition.


🔹 Taking Input from User

In real-world programs, we cannot predict what value a user will enter. So, we take input using the input() function.

number = int(input("Enter a value: "))
print(number)

# Example Input: 20
# Output: 20

text = input("Enter some text: ")
print(text)

# Example Input: Hello
# Output: Hello

🔹 1. Simple if Statement

Used when we want to check a single condition.

Syntax:

if (condition):
    # statements to execute if condition is True

Example:

num1 = 10
if num1 > 0:
    print("Positive")
# Output: Positive

🔹 2. if-else Statement

Used when we want one block to run if the condition is True, and another if it’s False.

Syntax:

if (condition):
    # statements if condition is True
else:
    # statements if condition is False

Example:

age = int(input("Enter your age to check voting eligibility: "))

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

🔹 3. if-elif-else Statement

Used when we need to check multiple conditions.

Syntax:

if (condition1):
    # statements
elif (condition2):
    # statements
else:
    # statements if all conditions fail

Examples:

👉 Checking positive, negative, or zero:

num = int(input("Enter a value: "))

if num > 0:
    print("Positive")
elif num < 0:
    print("Negative")
else:
    print("Zero")

👉 Grading System:

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

if marks >= 85:
    print("A grade")
elif marks >= 70:
    print("B grade")
elif marks >= 50:
    print("C grade")
elif marks >= 35:
    print("D grade")
else:
    print("F grade")

🔹 4. Nested if Statement

Used when one condition depends on another condition.

Example:

num = int(input("Enter a value: "))

if num > 0:
    if num % 2 == 0:
        print("Positive and Even")
    else:
        print("Positive and Odd")
else:
    if num % 2 == 0:
        print("Negative and Even")
    else:
        print("Negative and Odd")

🔹 5. Ternary Operator (Short-Hand if)

A compact way of writing if-else in a single line.

Example:

num = 13
result = "Even" if num % 2 == 0 else "Odd"
print(result)
# Output: Odd

🔹 Mini Projects / Practice

  1. ATM Withdrawal Check

    • Input: account balance and withdrawal amount.

    • Output: whether withdrawal is possible.

  2. Login System

    • Input: username & password.

    • Output: successful login or invalid credentials.


Conclusion:
Conditional statements help us control the flow of execution in Python. They are the foundation for problem-solving and decision-making in programs.

Next up: Loops in Python (for, while) 🚀