What is an if condition in Python?
- 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")
| 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)