#5 Flow Control Statement: if...else...
In Engineering, we design for every scenario; in Python, if...else helps us do the same.
In the previous edition of the newsletter, we learned about Defining and Using Functions in Python with the help of Colaboratory learning notebook.
In this newsletter edition, we expand into Conditional Branching statements in Python.
In this newsletter, you will be introduced to one concept
if... elif... else... statement
1.1 What are Flow Control Statements and Why should You use it?
Flow control helps your program decide what to do next based on the situation.
Conditional branching statements are subset of Flow control statements that help your program make decisions based on conditions, allowing it to "branch" into different paths of execution.
Quite often we need to perform different calculations depending on the scenario. Conditional branching statements help you achieve this.
1.2 if.. elif.. else..
The if... elif... else... statement is a way for your program to make decisions. It allows the program to check multiple conditions and take different actions depending on which condition is true.
A very simple implementation of conditional branching statement could be for the checking the adequacy of a beam based on the allowable and applied moment.
Here is a flowchart to help you grasp how the if.. elif.. else.. statement work.
You can add as many elif statements as you need between the if and else statements
We will be using Functions explained in the previous edition of newsletter.
def beam_safety_check(applied_moment, allowable_moment):
# Calculate the moment unity check value
unity_check = applied_moment / allowable_moment
if unity_check <= 0.7:
return f"Beam is safe. Unity Check = {unity_check}"
elif unity_check < 1: # Will be checked only if the previous condition failed
return f"Beam is safe, but nearing capacity. Unity Check = {unity_check}"
else:
# Will be executed only if all the previous conditions failed
return f"Beam is unsafe. Unity Check = {unity_check}"
Let us test the function with different inputs to see if it executes the correct branch of code according to the given condition.
print(beam_safety_check(applied_moment=50, allowable_moment=100))
# Beam is safe. Unity Check = 0.5
print(beam_safety_check(applied_moment=90, allowable_moment=100))
# Beam is safe, but nearing capacity. Unity Check = 0.9
print(beam_safety_check(applied_moment=120, allowable_moment=100))
# Beam is unsafe. Unity Check = 1.2
1.4 Takeaways
1 / Flow Control in Python
a / Flow control statements like if, elif, and else help your program make decisions based on conditions.
b / These statements allow branching logic, enabling your program to handle multiple scenarios dynamically.
2 / Conditional Statements
a / if: Checks the first condition. If true, executes the corresponding block.
b / elif: Checks subsequent conditions if the previous ones were false.
c / else: Executes when all preceding conditions are false.
3 / Best Practices
a / Keep conditions simple and clear to improve readability.
b / Use elif instead of multiple if blocks to avoid unnecessary checks.
1.5 References
How to run the code from the Learning Notebook?
You do not have to install python to work with this learning notebook
You can work with this learning notebook even from your smart phone
Have a Question / Suggestion?
??? Post a comment below??. I will try my best to respond within 24 hours.