DEV Community

VINOTH
VINOTH

Posted on

Python3 - Conditional Statements if, if-else, elif

What is an if condition in Python?

  1. An if statement is a condition statement used to check a condition, and execute it if the condition holds true.

Should I use elif or if?

  • Use elif when you want to check multiple conditions but only execute one specific block of code from a group. Use if when you want to evaluate multiple conditions independently, allowing more than one code block to run.
mark1 = 100
mark2 = 100

if mark1 > mark2:
    print("Mark1 is greater")
elif mark2 > mark1:
    print("mark2 is greater")
else:
    print("Both Are Equal")

Enter fullscreen mode Exit fullscreen mode
Keyword Meaning When it runs Condition required?
if Checks the first condition Runs if the condition is True ✅ Yes
elif "Else If" – checks another condition Runs only if the previous if/elif conditions are False ✅ Yes
else Executes if no conditions matched Runs when all previous conditions are False ❌ No

Top comments (0)