In this tutorial, you will learn decision-making using different if-else statements in the Python programming language. Let’s one by one explore them.
In python, decision making performs by the following statements.
Statement | Description |
if Statement | The if-block is used to test a particular condition. If the condition is true, a code block will execute. |
if-else Statement | The if-else statement is similar to the if statement except that it also contains the code block to verify the condition’s false case. If the condition is given in the if statement is false, then else statement will execute. |
Nested if Statement | Nested if statements allow us to use an if-else statement in an outer if statement. |
We’ll be covering the following topics in this tutorial:
Indentation in Python
Python does not allow the use of block-level code parentheses. In Python, a block declares by indentation. If two statements are at the same level of indentation, they are part of the same block. In general, four spaces give to indent the statements.
Indentation is the most common part of the python language because it declares the code block. All one-block statements have the same indentation level. We will see how the actual indentation takes place in Python.
What is Python If Statement?
Python if Statement is the most simple decision-making statement. It decides whether a specific statement or particular block of code will be executed or not based on the condition provided in the if Statement. If a specific condition is true, then a block of Statements is executed; otherwise, not.
Python if Statement Syntax
if [conditional expression]: statement(s)
The if” statement is used to make decisions. Once the limitations have been defined, the code’s body will only run when the ‘If’ statement is true.
The first line of the statement, i.e., if [conditional expression]: it ends with a colon, and the condition is a boolean expression that evaluates either True or False. The program evaluates the conditional expression and will only execute statement(s) if the expression is true. The statement(s) will not execute if the test expression is false.
In the next line, we have a block of statements. A block is just a set of one or more declarations. When a block of statements followed by an if clause, it is known as an if block.
Please note that each statement within the if block is indented to the right of the if keyword by the same amount. Many languages such as C, C++, Java, PHP use curly braces ({}), but Python uses indentation. Each statement must block inside if the same number of spaces. Otherwise, a syntax error will occur.
Python interprets Non-zero values as True. None and 0 interpret as False.
Python if Statement Flowchart
First, the test expression checks whether the expression is true, the if-statement is executed. If it is false, the statement present after the if-statement executes. In either case, an external if-statement code line evaluates by default.
Example: Python if Statement
#Let’s see an example of the implementation of an if statement.
num = 5 if (num <10): print (“5 is less than 10”) print (“Statement after if statement”)
Output:
In the above example, the condition expression is num < 10.
The body of if performed if this is true.
When the variable num is less than to 10, condition expression is true and statements inside the body of if are executed.
If the variable num is greater than 10, condition expression is false and statements inside the body of if are skipped.
The print() statement falls outside of the if block (unindented). Hence, it is executed regardless of the test expression.
Python if…else Statement
You can combine else statement with an if statement. Else statement executes a code if the conditional expression in a if-statement is 0 or a FALSE. The else statement is an optional statement.
Syntax of if…else
if expression: block_of_code_1 else: block_of_code_2
In Python, if statement informs the program what to do if the condition is true. If the condition is false, the program only executes what comes after if statements. In situations where we want the program to execute some statements if the condition is true and some other statement only if the condition is false, we use if else in Python.
The else keyword must be on its line and be at the same level indentation level as the if keyword. A colon must follow the else keyword. Any code included in the else statement must be marked with the same level.
Python if..else Flowchart
As depicted by the flowchart above, the Python program first evaluates the test expression. If the condition is true, then the if statement executed. If the condition is false, then the else statement is executed. The body of if and else statements start with indentation.
Example of Python if…else
num = 5 if num >= 0: print("Positive or Zero") else: print("Negative number")
Output :
In the above example, when num is equal to 3, the test expression is true if-statement is executed and else part is skipped.
If num is equal to -5, the test expression is false and else part is executed and if statement is skipped.
If num is equal to 0, the test expression is true and if statement is executed and else part is skipped.
Python if…elif…else Statement
Here, the elif stands for else if in Python. 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. Only one block among the several if…elif…else blocks is executed according to the condition.
Similar to the else, the elif statement is optional. However, unlike else, for which there can be at most one statement, there can be an arbitrary number of elif statements following an if.
Syntax of if…elif…else
if test expression: Body of if elif test expression: Body of elif else: Body of else
Flowchart of if…elif….else in Python programming
Example of if…elif…else
num = 1.5 if num > 1: print("+ve Number") elif num == 0: print("Zero") else: print("-ve number")
Output :
Python Nested if statements
We can have an if…elif…else statement inside another if…elif…else statement. This is called nesting in computer programming.
Any number of these statements can be nested inside one another. Indentation is the only way to figure out the level of nesting. They can get confusing, so they must be avoided unless necessary.
Python Nested if Example
num = float(input("Enter any Number: ")) if num >= 0: if num == 0: print("Zero") else: print("+ve Number") else: print("-ve Number")
Output: