Understanding if, if-else, and if-elif-else Statements in Python

What are if, if-else, and if-elif-else ?

These are Python’s tools for decision-making. They let your program choose between actions based on conditions you set. Think of them as decision checkpoints:

  •  if :Executes a block of code only if a condition is true.
  • if – else: Chooses between two actions—one for true and another for false.
  • if – elif – else : Chooses between multiple actions by checking several conditions one by one.

Let’s learn one by one in detail with coding examples:

The ‘ if ‘ Statement

if statement evaluates a condition (a test, like comparing numbers). If the condition is true, the code inside the if block runs. Otherwise, it skips the block.

Example : Checking Age

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

Code Explanation: 

  • The condition age > = 18 checks if the value of age is 18 or more.
  • If it’s true, the message “You are eligible to vote!”   is printed.
  • If the age were less than 18, nothing would happen.

Imagine you’re checking if you can enter a movie theater:

  • If your age is 18 or older, you’re allowed in.
  • Otherwise, you’ll just stand outside—but no one tells you explicitly why.


The ‘if-else’ Statement

What if you want your program to respond when a condition is not true? This is where if – else comes in. it gives your program an alternate option if condition is not true.

Example : Checking Age

age = 16
if age >= 18:
    print("You are eligible to vote!")
else:
    print("Sorry, you're too young to vote.")        

Explanation

  • If the condition age > = 18 is true, the program prints the first message.
  • Otherwise, it runs the else block, showing the second message.

Let’s go back to the movie theater example:

  • If you’re 18 or older, you enter the theater.
  • If you’re younger, the staff tells you, “Sorry, come back when you’re older.”

The if – elif – else Statement

Sometimes, you have multiple conditions to check. For this, Python provides the if – elif – else statement. Think of it as a series of checkpoints.

Example: Exam Grades

score = 85
if score >= 90:
    print("You got Grade A")
elif score >= 80:
    print("You got Grade B")
elif score >= 70:
    print("You got Grade C")
else:
    print("You need to work harder.")        

Code Explanation

  • The program checks each condition in order.
  • If score>=90 is true, it prints “You got Grade A”   and skips the rest.
  • If not, it moves to the next condition score> = 80 and so on.
  • If none of the conditions are true, the else block runs.

Think of this as choosing transportation based on how far you need to travel:

  • If the distance is less than 1 km, you walk.
  • If it’s between 1 and 10 km, you bike.
  • If it’s more than 10 km, you drive.
  • If there’s no way to travel, you stay home.

Click here to read more

To view or add a comment, sign in

More articles by Lal Babu Ray

  • Why India must bet big on Deep Tech

    India’s startup ecosystem has proven its capability in scaling software and service solutions. Now, it’s time to build…

  • Resume Writing

    PAID hashtag#RESUME WRITINGCrafting a good resume is still a daunting task for many freshers and professionals. It's…

  • Joins in Pandas

    Joins in python pandas is one of the important concepts to learn for data handling and data analysis. Normally we do…

  • Understanding Hypothesis Test

    Hypothesis is one of the prominent steps in statistics. Hypothesis testing is part of inferential statistics where we…

  • 10 Startup Ideas In this AI race

    10 Startup Ideas Starting a business today is more accessible than ever, thanks to advancements in technology. With the…

  • Understanding Hypothesis Testing in Data Science and Machine Learning

    Hypothesis testing is a fundamental tool for validating insights in data science and machine learning. It helps us…

  • Understanding Hypothesis Testing in Machine Learning

    Hypothesis testing is a fundamental tool for validating insights in data science and machine learning. It helps us…

  • Top 5 AI Tools To Accelerate Job Hunts

    AI has transformed the job search process, making it more efficient and tailored to individual needs. From resume…

    2 Comments
  • 10 Startup Ideas using AI

    Technology continues to reshape entrepreneurship, offering endless opportunities for innovation. Whether launching an…

  • 10 Startup Ideas using AI and How to Build Them

    This age of technology is rapidly changing. Keeping upto the mark is not so easy.

Insights from the community

Others also viewed

Explore topics