
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
- Python 3 Advanced Tutorial
- Python 3 - Classes/Objects
- Python 3 - Reg Expressions
- Python 3 - CGI Programming
- Python 3 - Database Access
- Python 3 - Networking
- Python 3 - Sending Email
- Python 3 - Multithreading
- Python 3 - XML Processing
- Python 3 - GUI Programming
- Python 3 - Further Extensions
- Python 3 Useful Resources
- Python 3 - Questions and Answers
- Python 3 - Quick Guide
- Python 3 - Tools/Utilities
- Python 3 - Useful Resources
- Python 3 - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python 3 - IF...ELIF...ELSE Statements
An else statement can be combined with an if statement. An else statement contains a block of code that executes if the conditional expression in the if statement resolves to 0 or a FALSE value.
The else statement is an optional statement and there could be at the most only one else statement following if.
Syntax
The syntax of the if...else statement is −
if expression: statement(s) else: statement(s)
Flow Diagram

Example
#!/usr/bin/python3 amount = int(input("Enter amount: ")) if amount<1000: discount = amount*0.05 print ("Discount",discount) else: discount = amount*0.10 print ("Discount",discount) print ("Net payable:",amount-discount)
Output
In the above example, discount is calculated on the input amount. Rate of discount is 5%, if the amount is less than 1000, and 10% if it is above 10000. When the above code is executed, it produces the following result −
Enter amount: 600 Discount 30.0 Net payable: 570.0 Enter amount: 1200 Discount 120.0 Net payable: 1080.0
The elif Statement
The elif statement allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE.
Similar to the else, the elif statement is optional. However, unlike else, for which there can be at the most one statement, there can be an arbitrary number of elif statements following an if.
syntax
if expression1: statement(s) elif expression2: statement(s) elif expression3: statement(s) else: statement(s)
Core Python does not provide switch or case statements as in other languages, but we can use if..elif...statements to simulate switch case as follows −
Example
#!/usr/bin/python3 amount = int(input("Enter amount: ")) if amount<1000: discount = amount*0.05 print ("Discount",discount) elif amount<5000: discount = amount*0.10 print ("Discount",discount) else: discount = amount*0.15 print ("Discount",discount) print ("Net payable:",amount-discount)
When the above code is executed, it produces the following result −
Enter amount: 600 Discount 30.0 Net payable: 570.0 Enter amount: 3000 Discount 300.0 Net payable: 2700.0 Enter amount: 6000 Discount 900.0 Net payable: 5100.0