Hi. Welcome back to another amazing tutorial on this platform. If you are new here, please check out our previous articles. Today, we will be discussing conditional statements in python.
Python has 4 main conditional statements and these conditional statements are mostly used in decision making. Below are the different types of conditional statements, with syntaxes and examples.
if statements
This statement always starts with a Boolean expression followed by a statement or a group of statements that tells what action should be done if the test expression is true.
SYNTAX
if expression:
statement(s)
Take note that the compiler will only execute the program only if the ‘if expression’ is true and you must take into consideration the indentation of the statements on the body of the ‘if expression’.
Let us look now at an example
val = 25
if ( val == 25 ) : print (" I am 25 years old")
print ("Good bye!")
The compiler prints the statements that are in the brackets (()) if the inputs meets the condition, which in our case here is the ‘val’
OUPUT
If….else statements
This statement is the same if statement. The only difference that comes in here is when the condition is false, then the compiler will execute the statements in the else block.
SYNTAX
if test expression:
statement(s)
else:
statement(s)
To better illustrate, let’s look at an example with the ‘if…else structure
a = 25
b = 30
if a > b:
print("kelvin is younger than rayan")
else:
print("rayan is older than kelvin")
The compiler in this case did not found the given if statement to be true, so it takes the statement in the else block
OUTPUT
If…elif…else statements
An ‘elif’ statement is mostly used to check multiple expressions. It first checks if the given ‘if statement’ is true and if found true, the compiler will execute the statement in the ‘if’ block. If false, it tests the condition in the ‘elif’ block. If the ‘elif’ statement is true, then python will execute the elif block. Otherwise, then the else statement will be executed.
SYNTAX
if expression:
if block
elif expression:
elif block
else:
else block
EXAMPLE
num = 1122
if 9 < num < 99:
print("Two digit number")
elif 99 < num < 999:
print("Three digit number")
elif 999 < num < 9999:
print("Four digit number")
else:
print("number is <= 9 or >= 9999")
OUTPUT

Nested if ….elif….else statements
This particular statement is used is to check for other statements when the first if statement is true.
SYNTAX.
If test expression1:
If test_expression-a:
statement _block1-a
elif test_expression-b
statement_block1-b
else
statement_block1-c
elif test_expression2:
else :
statement_block3
var = 400
if var < 140:
print ("Expression value is less than 140")
if var == 150:
print ("Which is 150")
elif var == 75:
print ("Which is 75")
elif var == 50:
print ("Which is 50")
elif var < 50:
print ("Expression value is less than 50")
else:
print ("Could not find true expression")
print ("Good bye!")
OUTPUT
Comments
Post a Comment
Please do not enter any spam link in the comment box.