if else elif in Python

The code to be executed within if condition is to be indented to create a block of code.
mark = 60

# Traditional
if mark >= 50:
    status = "Pass"
else:
    status = "Fail"

print(status)  # Output: Pass

# Ternary Syntax
status = "Pass" if mark >= 50 else "Fail"

print(status)  # Output: Pass

Python if else and elif to execute code blocks based on condition checking True or False

Using elif

If the first if condition check fails then we can go for for one more check by using elif before using else.
if elif else condition check
x = 10
y = 20

if(x > y):
    print("x is greaeter than y")
elif(y > 15):
    print("y is greater than 15")

else:
    print("y is greater than x but less than 15")
Output is here
y is greater than 15

What is the Difference Between else and elif?

The else block handles the fallback scenario without evaluating any condition—its code executes whenever all preceding if or elif conditions evaluate to False. Because else acts as the final terminal path of a conditional chain, no further conditions are checked and no subsequent conditional blocks are executed. In contrast, an elif (short for "else if") introduces an additional condition to test when the previous condition fails.

Avoiding Redundant Checks:

Without elif, you would have to rely on multiple separate if statements, causing Python to evaluate every single condition sequentially—even if an earlier condition was already met. Using an if-elif-else chain ensures that as soon as one condition evaluates to True (or the else block is reached), Python skips all remaining checks in the chain, preventing redundant computations.

Read more how to use elif to get the grade from input mark.

Using Short code

x = 10
y = 20

if(y > x): print("x is greater than y")
Output is here
x is greater than y
This one is easy.
x = 10
y = 20

print("y is big") if(y > x) else print("x is big")
One liner if else
mark = 45
status = 'Pass' if mark > 50 else 'Fail'
print(status) # Fail
# Chained Comparison (Pythonic style)
score = 85
if 80 <= score <= 100:
    print("Grade A")
Write one user defined function to filter odd and even numbers.
def my_check(x):
    return True if x % 2 == 0 else False

Absolute value of input number

i = int(input("input a number "))
if(i < 0): i = -i
print("Absolute value is : ", i)

Nested Conditional Statements: Sort three user input numbers

Using logical operators and, or
#n1=int(input('First Number : '))
#n2=int(input('Second Number : '))
#n3=int(input('Third Number : '))
n1 = 1
n2 = 2
n3 = 3

if(n1 >= n2 and n1 >= n3):
    if(n2 >= n3):
        print(n1, n2, n3)
    else:
        print(n1, n3, n2)  
elif(n2 >= n1 and n2 >= n3):
    if(n1 >= n3):
        print(n2, n1, n3)
    else:
        print(n2, n3, n1)  
elif(n3 >= n1 and n3 >= n2):
    if(n1 >= n2):
        print(n3, n1, n2)
    else:
        print(n3, n2, n1)
month = "Feb"
if month == "Jan" or month == "Feb" or month == "March":
    print("This is last quarter of the Financial year")
else:
    print("Financial year is not ending now")
Output
This is last quarter of the Financial year

Boolean Logic:

Using and
temperature = 30
is_sunny = True

if temperature > 25 and is_sunny:
    print("It's a hot and sunny day!")
else:
    print("It's not a hot and sunny day.")
Using or
is_weekend = False
is_holiday = True

if is_weekend or is_holiday:
    print("Today is a holiday")
else:
    print("It might be a working day.")
Using not
grade = 'A'
if not(grade == 'A'):
    print("You are not in grade A ")
else:
    print("You are in A grade ")
Output
You are in A grade
Using and , or and not
is_weekend = True
is_holiday = False
weather_is_good = False

if (is_weekend or is_holiday) and not weather_is_good:
    print("It's a perfect day for staying indoors and reading a book.")
else:
    print("Let's go outside and enjoy the day!")

Checking condition inside a loop

for x in range(5, 25, 5):
    if(x == 15):
        break
    print(x)
Output is
5
10

Handling None value

In Python, None is a special singleton object that often represents the absence of a value. When used in a condition check, such as within an if statement, None is treated as False.
my_variable = None

if my_variable:
    print("The variable has a truthy value.")
else:
    print("The variable is None or otherwise falsy.")
Output
The variable is None or otherwise falsy.

Check Divisibility

i = int(input("Enter any number : "))
if (i % 5 == 0): print("Number is divisible by 5")
else: print("Number is not divisible by 5")

The pass Statement in Conditional Blocks

Python if blocks cannot be empty. If you are drafting code and want to leave a block empty without causing a syntax error, use the pass statement:

age = 15

if age < 18:
    pass # To be implemented later
else:
    print("Adult")
High School Python ( part of Syllabus) View and Download if_else_elif ipynb file ( .html format )

  • Python Basics - if else elif to check condition
    19-Feb-2024 quixote


Podcast on Python Basics


Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer