This lesson will explain how to use several Python Conditional Statement types to construct decisions in a Python program, along with examples to understand the topic better.
Condition statements in Python take different actions based on whether a particular condition is true or false. Every conditional statement has an evaluation of True or False, so you can run different code blocks depending on how a condition turns out.
Several different categories of conditional statements exist.
In Python, the ‘if statement‘ is the most basic form of a control statement, and it examines a condition and returns True or False. The code block is skipped if the condition is False, and the controller continues to the following line if the condition is True.
Basic syntax
if condition: statement 1 statement 2 ……………….. statement n
In the below example, squareVar > 0
is the test expression.
squareVar
is greater than 0, the condition expression is true, and statements inside the if statement is executed.Example
# Calculate square squareVar = 3 if squareVar > 0: print('Square of',squareVar,'=',squareVar* squareVar)
Output
Square of 3 = 9
If the condition is True, the ‘if statement’ verifies the value and runs the ‘if’ block of the python code. However, if the condition is False, it runs the else code block. We must decide when we wish only to run a piece of code if a specific condition is met. Let’s understand that Python’s if statement syntax uses the following test expression.
Basic Syntax
if condition: statement 1 else: statement 2
If the test expression returns True, the program will only execute the statement(s) in question. Statement 2 will be performed if the test expression is False.
Example
# Calculate square squareVar = 0 if squareVar > 0: print('Square of',squareVar,'=',squareVar* squareVar) else: print("Negative Number Square is not possible")
Output
Negative Number Square is not possible
In the above example, when squareVar
is greater than 0, the test expression is true, the body of if executes, and the body of else skips.
If squareVar
is equal or less than equal to 0, the test expression is false, the body of else runs, and the body of the if statement is skipped, the program will print a Negative Number Square is not possible.
Multiple if blocks allow chaining together various conditions in Python. When you need to check numerous conditions, this is helpful. The elif statement executes the code if a condition is met and tests each condition individually. If-elif-else allows us to make difficult decisions.
Basic Syntax
if condition-1: statement 1 elif condition-2: statement 2 ... else: statement
Example
grade = 'C+' if grade =='A+': print('Outstanding') elif grade =='B+': print('Good') elif grade =='C+': print('Average') else: print("Fail")
Output
Average
A nested if…elif…else expression nested inside another is called a nested if-else statement. These sentences can be nested inside one another in many ways, and they must be avoided until required because they can be mysterious. The nesting level can only be determined by indentation, and the only way to distinguish between levels of nesting is by indentation. When we wish to make a sequence of decisions, the nested if-else construct is helpful.
Basic Syntax
if condition-1: statement 1 if condition-1: statement else: statement else: statement
Example
grade = int(input("Enter a number: ")) if(grade > 50): if (grade > 85 and grade < 100): print('Outstanding') else: print('Average') else: print("Fail")
Output
Enter a number: 96 Outstanding
Indentation is crucial when constructing a block of code containing several if statements. However, there are situations when a block will have a single line of code. We can write a sentence after the colon rather than a text block.
Example
grade = int(input("Enter a number: ")) if(grade > 50):print('Pass') else:print("Fail")
Output
Enter a number: 55 Pass
This concludes the Python Conditional Statements lesson. In the next lesson, you will learn Python Iterative Statements in Python and their usage.