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

TL;DR
The if-elif-else statement lets you choose among more than two alternatives. Python checks condition 1 first; if it fails, it checks the elif condition; if that also fails, the else block runs. In Python the keyword for else-if is 'elif', and only the first satisfied branch executes.
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?
The if-else statement is used when a decision has to be made among two alternatives: either the statements inside if execute or the statements inside else execute. The if-elif-else statement is used when the decision has to be made between more than two alternatives. It adds an elif (else if) check between the if and the else, letting you evaluate a second condition before falling through to the else block.
Q: How does execution flow through an if-elif-else statement?
Condition 1 in the if is always checked first. If it is satisfied, the if block executes and the remaining conditions are not evaluated. If condition 1 is not satisfied, then only condition 2 in the elif is checked. If condition 2 is satisfied, the elif block executes; otherwise, if neither condition is satisfied, the else block executes. Only one branch runs.
Q: What keyword does Python use for else-if?
Python uses the keyword 'elif' for else-if. In the general syntax you might call it 'else if', but in Python the keyword is written as elif. You place it after the initial if check, followed by its own condition, so that if the first condition fails Python can go ahead and evaluate the elif condition before reaching the else statement.
Q: How do you check if X is equal to, less than, or greater than 5 using if-elif-else?
You use three alternatives. The if checks whether X is equal to 5 and prints 'X is 5'. The elif checks whether X is less than 5 and prints 'X is less than 5'. The else prints 'X is greater than 5'. The else needs no condition because if X is not equal to 5 and not less than 5, it is guaranteed to be greater than 5, so else safely handles that case.
Q: How do you test whether a number is divisible by 2 or 3 in Python?
Store the number in a variable such as n. Use the mod operator, which returns the remainder. If n mod 2 equals 0, the number is divisible by 2, so print that message. Elif n mod 3 equals 0, the number is divisible by 3, so print that. Else, print that the number is neither divisible by 2 nor 3. A final print statement outputs 'done' regardless.
Q: Why does the else branch in these examples not need a condition?
The else branch executes only when none of the preceding conditions are satisfied, so it does not need its own condition. In the X example, if X is not equal to 5 and not less than 5, it is guaranteed to be greater than 5. In the divisibility example, if the number is not divisible by 2 and not divisible by 3, else confirms it is neither, so an extra check would be redundant.
Q: What does the mod operator do in the divisibility program?
The mod operator returns the remainder of a division, not the quotient. To test divisibility by 2, you compute n mod 2; if the remainder is 0, the number is divisible by 2. To test divisibility by 3, you compute n mod 3; if that remainder is 0, the number is divisible by 3. A remainder of 0 confirms the number divides evenly by that value.
Q: What output does the divisibility program produce for n=10, 15, and 17?
For n=10, the number is divisible by 2, so it prints 'number is divisible by 2' followed by 'done'. For n=15, it is not divisible by 2 but is divisible by 3, so it prints 'number is divisible by 3' then 'done'. For n=17, a prime number, it is neither divisible by 2 nor 3, so the else runs printing 'number is neither divisible by 2 nor 3' then 'done'.
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





