There come situations in real life when we need to do some specific task and based on some specific conditions and, we decide what should we do next. Similarly there comes a situation in programming where a specific task is to be performed if a specific condition is
True. In such cases, conditional statements can be used. The following are the conditional statements provided by Python.
Let us go through all of them.
If the simple code of block is to be performed if the condition holds true than if statement is used. Here the condition mentioned holds true then the code of block runs otherwise not.
if condition: # Statements to execute if # condition is true
10 greater than 5 Program ended
Indentation(White space) is used to delimit the block of code. As shown in the above example it is mandatory to use indentation in Python3 coding.
In conditional if Statement the additional block of code is merged as else statement which is performed when if condition is false.
if (condition): # Executes this block if # condition is true else: # Executes this block if # condition is false
Example 2: You can also chain if..else statement with more than one condition.
letter is A
Nested if Statement
if statement can also be checked inside other if statement. This conditional statement is called nested if statement. This means that inner if condition will be checked only if outer if condition is true and by this, we can see multiple conditions to be satisfied.
if (condition1): # Executes when condition1 is true if (condition2): # Executes when condition2 is true # if Block is end here # if Block is end here
Bigger than 5 Between 5 and 15
The if-elif statement is shoutcut of if..else chain.While using if-elif statement at the end else block is added which is performed if none of the above if-elif statement is true.
if (condition): statement elif (condition): statement . . else: statement
letter is A
- Python | Check if a nested list is a subset of another nested list
- Python3 Intermediate Level Topics
- How to implement Dictionary with Python3?
- Python3 for GUI application | An Overview
- How to write Comments in Python3?
- Python2 vs Python3 | Syntax and performance Comparison
- Automate the Conversion from Python2 to Python3
- Convert String to Double in Python3
- Introduction to Python3
- Why PyPy3 is preffered over Python3?
- Different Input and Output Techniques in Python3
- New '=' Operator in Python3.8 f-string
- Positional-only Parameter in Python3.8
- Itertools in Python3
- Setting python3 as Default in Linux
- How to print spaces in Python3?
- How to use Variables in Python3?
- Guide to install TuriCreate in Python3.x
- What is Three dots(...) or Ellipsis in Python3
- Loops and Control Statements (continue, break and pass) in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.