
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
What is the syntax of Python if...elif...else statement?
In Python program if..elif..else construct is used if it has ;arge number of logical expressions to be evaluated so that earlier expressions turn out to be true. If you use nested if - else structure, then indent level of subsequent blocks goes on increasing and program becomes difficult to read. This is where elif is handy. Each block of statements under elif is executed if earlier condition false. Last else block is executed when all previous logical expressions fail. All the blocks have same level of indent.
if expression1==True: statement(s) elif expression2==True: statement(s) elif expression3==True: statement(s) else: statement(s)
- Related Articles
- What is basic syntax of Python if...else statement?
- What is correct syntax of Python if statement?
- How to optimize nested if...elif...else in Python?
- What is the if...else statement in JavaScript?
- What is if...else if... statement in JavaScript?
- What is the ‘if...else if...else’ statement in Java and how to use it?
- Where to put comments in an if...elif..else construct?
- How to use #if..#elif…#else…#endif directives in C#?
- What is the resemblance of COALESCE() function with IF-THEN-ELSE statement?
- Java if-else statement
- Java if-else-if ladder statement
- IF ELSE statement in a MySQL Statement?
- Java if-then-else statement
- How to indent an if...else statement in Python?
- What is the ‘if else’ statement in Java and how to use it?

Advertisements