Python Basics

 Conditional Statements (if, elif, else)

Welcome back, Python learner!

In Post 3, we explored Operators and Expressions — how to perform calculations, compare values, and make logical decisions.

Now, it’s time to take your programs to the next level:
We’ll learn how to make Python think and decide using Conditional Statements.


 What Are Conditional Statements?

Conditional statements allow your program to make decisions based on certain conditions.

For example:

“If it’s raining, take an umbrella; otherwise, don’t.”

In Python, we use the if, elif, and else keywords to write such logic.


 Basic Structure of if Statement

if condition:

    # code block

If the condition is True, the indented code block runs.
If the condition is False, Python skips that block.

 Example:

age = 18

if age >= 18:

    print(“You are eligible to vote.”)

 Output:

You are eligible to vote.

If age was less than 18, nothing would print because the condition would be False.


The if-else Statement

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

That’s where else comes in!

if condition:

    # if True

else:

    # if False

 Example:

age = int(input(“Enter your age: “))

if age >= 18:

    print(“You can vote!”)

else:

    print(“Sorry, you are too young to vote.”)

 Output Example 1:

Enter your age: 20

You can vote!

 Output Example 2:

Enter your age: 15

Sorry, you are too young to vote.


 The if-elif-else Ladder

Sometimes, you have multiple conditions to check.
In that case, we use elif (short for else if).

if condition1:

    # code

elif condition2:

    # code

else:

    # code

 Example:

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

if marks >= 90:

    print(“Grade: A+”)

elif marks >= 80:

    print(“Grade: A”)

elif marks >= 70:

    print(“Grade: B”)

elif marks >= 60:

    print(“Grade: C”)

else:

    print(“Grade: F”)

 Output Example:

Enter your marks: 85

Grade: A


 Nested if Statements

You can place one if statement inside another — this is called a nested if.

 Example:

age = int(input(“Enter your age: “))

if age >= 18:

    print(“You are an adult.”)

    if age >= 60:

        print(“You are also a senior citizen.”)

    else:

        print(“You are not a senior citizen yet.”)

else:

    print(“You are a minor.”)

 Output Example:

Enter your age: 65

You are an adult.

You are also a senior citizen.


Combining Conditions with Logical Operators

You can also combine multiple conditions using and, or, and not (from the previous lesson).

 Example 1:

age = 25

has_id = True

if age >= 18 and has_id:

    print(“You can enter the club.”)

else:

    print(“Access denied.”)

 Example 2:

day = “Sunday”

if day == “Saturday” or day == “Sunday”:

    print(“It’s weekend!”)

else:

    print(“It’s a weekday.”)


 Using if with Strings and Lists

Python allows you to use conditions with strings, lists, and other data types too.

 Example 1:

name = input(“Enter your name: “)

if name == “Alice”:

    print(“Welcome, Alice!”)

else:

    print(“Hello, stranger!”)

 Example 2:

fruits = [“apple”, “banana”, “mango”]

if “apple” in fruits:

    print(“Yes, apple is available!”)

else:

    print(“No, apple is not in the list.”)


 Real-Life Example: Simple Calculator

Let’s combine everything we’ve learned so far — operators, input, and conditions.

 Code Example:

num1 = float(input(“Enter first number: “))

num2 = float(input(“Enter second number: “))

op = input(“Enter operator (+, -, *, /): “)

if op == “+”:

    print(“Result:”, num1 + num2)

elif op == “-“:

    print(“Result:”, num1 – num2)

elif op == “*”:

    print(“Result:”, num1 * num2)

elif op == “/”:

    if num2 != 0:

        print(“Result:”, num1 / num2)

    else:

        print(“Error: Division by zero!”)

else:

    print(“Invalid operator!”)

Sample Run:

Enter first number: 10

Enter second number: 5

Enter operator (+, -, *, /): *

Result: 50.0


Practice Tasks

Try solving these challenges

 Write a program to check whether a number is positive, negative, or zero.
 Take a user’s age and check:

If the user is a child (<13)

A teenager (13–19)

An adult (20–59)

Or a senior citizen (60+)
Ask the user for a year and determine if it’s a leap year or not.
Bonus: Create a login system that checks if username and password match preset values.


Summary

You learned:

How to use if, elif, and else to make decisions

How to nest conditions and use logical operators

How to write real-world programs that react to user input

Conditional statements are the foundation of logic in programming — every smart program you’ll ever build depends on them!

About the author

guestpostlinkingum@gmail.com

Leave a Comment