The Conditional statement
In writing codes in any programming language, controlling the flow of the program is very important that is why the conditional statements are used.
In Python, there is the if, else and the elif statements and they are used to control the flow of a program and set conditions to meet a particular rule or requirements.
The if statement is used to check if one of the conditions set is either true or false.
In writing the if statement, the if keyword is used, followed by the condition and the colon.
if condition: do this
The body of the if statement is indented and follows the python indentation rule of being consistent in either using four spaces or a tab.
The if statement cannot be written empty without any codes in it, instead the pass statement is written to avoid errors .
if condition: pass
condition = 'name' if condition=='name': print('Hello')
If statement can also be nested. This means that you can write an if statement inside another if statement.
if condition: do this: if condition2: do this
The else statement is used when the code does not meet the set condition or requirements.
Just like the if statement, the else statement is followed by colon and indented.
else: do this
In writing a program to check the weather condition.
weather = 'is_hot' if weather== 'is_cold': print("The weather is cold") else: print("The weather is so hot!")
The weather is so hot!
The elif statement is used to try another condition if the code does not meet a set condition or requirement. Multiple elif statement can be used in a statement to check for several other conditions. It is usually written before the else statement in a block of code. If the requirement for the elif is not met, the the else statement runs.
In writing the elif statement, the condition is written followed by the colon.
elif condition: do this
Modifying the above program:
weather='is_hot' if weather == 'is_cold': print("The weather is cold") elif weather == 'is_hot': print("The weather is so hot!") else: print ("Looks like a great weather today")
The weather is so hot!
Multiple elif can also be written in a program.
if weather =='is_cold': print ("The weather is cold") elif weather == 'is_hot': print("The weather is so hot!") elif weather == 'is_mild': print("The weather is neither cold nor hot.") else: print("Great weather today!")
The weather is neither cold nor hot.
The flowchart above gives more explanation on running the if, elif and else.
Thank you for reading, kindly like this article or leave a comment.