How Does the if-elif-else Statement Work in Python?

TL;DR
The if-elif-else statement lets Python choose among more than two alternatives by checking conditions in order and executing only the first matching branch. If neither the if nor elif condition succeeds, the else block runs. Examples compare x with 5 and test whether n is divisible by 2 or 3 using the % operator; read on to understand the execution flow and outputs.
Transcript
foreign statement so without any further delay let's get started the first topic of this presentation is Introduction to the if lfl statement the second topic is program divisibility test let's get started with the first topic that is Introduction to the if lfl statement we know from the last lecture that the if else statement is used when decision... Read More
Key Insights
- The if-elif-else statement is used when a decision has to be made between more than two alternatives, unlike the if-else statement which handles only two alternatives.
- In Python the keyword for 'else if' is 'elif'. It lets you chain an additional condition check after the initial if condition when more branches are needed.
- Execution follows a strict order: condition 1 is checked first, and only if it is not satisfied is condition 2 (the elif) checked, and only if that fails does the else block run.
- Only one branch executes in an if-elif-else structure. Once a condition is satisfied, its statement runs and the remaining conditions are not evaluated.
- Checking whether X equals 5, is less than 5, or is greater than 5 uses three alternatives, so if handles equal, elif handles less than, and else handles greater than.
- The else branch needs no condition because when X is not equal to 5 and not less than 5, it is guaranteed to be greater than 5, so else safely covers that case.
- The mod operator (%) returns the remainder, not the quotient. If n mod 2 equals 0 the number is divisible by 2, and if n mod 3 equals 0 it is divisible by 3.
- A statement placed after the if-elif-else block, like printing 'done', runs regardless of which branch executed, because it sits outside the conditional structure.
Install to Summarize YouTube Videos and Get Transcripts
Explore YouTube Video Summarizer or Get YouTube Transcript Extractor
Questions & Answers
Q: What is the difference between if-else and if-elif-else in Python?
An if-else statement chooses between two alternatives. An if-elif-else statement handles more than two alternatives by adding another conditional branch between if and else.
Q: How does an if-elif-else statement work in Python?
Python checks the if condition first and executes its block when the condition is satisfied. Otherwise, it checks the elif condition; if that also fails, it executes the else block. Only the first matching branch runs.
Q: What keyword does Python use for else-if?
Python uses the keyword elif for else-if. It appears after the initial if branch and introduces another condition that Python checks only when the preceding condition fails.
Q: How do you check whether x equals, is less than, or is greater than 5?
Use if to check whether x equals 5 and elif to check whether x is less than 5. The else branch then handles x being greater than 5 because the first two possibilities have already been ruled out.
Q: Why does the else branch not require a condition?
The else branch runs only when none of the preceding if or elif conditions are satisfied. For the comparison with 5, if x is neither equal to 5 nor less than 5, the example concludes that x is greater than 5.
Q: How do you test whether a number is divisible by 2 or 3 in Python?
First check whether n % 2 equals 0; if so, n is divisible by 2. Otherwise, use elif to check whether n % 3 equals 0, and use else when neither remainder is 0.
Q: What does the mod operator do in the divisibility test?
The mod operator (%) returns the remainder rather than the quotient. A remainder of 0 from n % 2 or n % 3 indicates that n is divisible by 2 or 3, respectively.
Q: Why does a print statement after the conditional always run?
A statement placed after the entire if-elif-else structure is outside all three branches. Therefore, the example prints "done" regardless of whether the if, elif, or else block executes.
Summary & Key Takeaways
-
The if-else statement handles decisions between two alternatives, executing either the if block or the else block. The if-elif-else statement extends this to more than two alternatives. Its syntax checks condition 1 in the if, condition 2 in the elif, and runs else when neither condition is satisfied.
-
Execution always begins with condition 1. If it is satisfied the if block runs; if not, only then is condition 2 checked. If condition 2 is satisfied the elif block runs; otherwise the else block runs. Only the first matching branch executes, and remaining conditions are skipped.
-
A divisibility program checks whether n is divisible by 2 or 3. If n mod 2 equals 0 it prints divisible by 2; elif n mod 3 equals 0 it prints divisible by 3; else it prints neither. For n=10 it prints divisible by 2, n=15 divisible by 3, n=17 neither, each followed by done.
Read in Other Languages (beta)
Share This Summary 📚
Summarize YouTube Videos and Get Video Transcripts with 1-Click
Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator
Explore More Summaries from Neso Academy 📚






Summarize YouTube Videos and Get Video Transcripts with 1-Click
Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator