#5 Flow Control Statement: if...else...

#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.

  • if: Starts by testing the first condition and executes the block (indented code) if the condition passes.
  • elif: Checks another condition if the first one is not true.
  • else: Runs when none of the previous conditions are 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.

Flow chart explaining the conditional branching example used in this newsletter
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

?? if Statements | Official Documentation
?? Defining Functions in Python ?(x)
?? Variables and Data Types in Python
?? Experimenting Python notebooks on Colaboratory

How to run the code from the Learning Notebook?

  1. Click to open the Learning Notebook
  2. Click on the "Sign In" blue button at the top right corner.
  3. Play on with the Learning Notebook by?writing YOUR OWN CODE

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.


要查看或添加评论,请登录

SWARUP Selvaraj的更多文章

  • #4 Defining Functions in Python ?(x)

    #4 Defining Functions in Python ?(x)

    A function is a small part of the whole system, but its effectiveness multiplies when reused across the system. In the…

  • #3 Variables and Data Types in Python ??

    #3 Variables and Data Types in Python ??

    Learn by doing; Do, Learn, Improve In our previous newsletter, we explored how to use Python notebooks in Colaboratory…

    2 条评论
  • #2 Experimenting Python notebooks on Colaboratory ??

    #2 Experimenting Python notebooks on Colaboratory ??

    A journey of a thousand miles begins with a single step Acquiring a new skill (particularly programming ??) can be…

    6 条评论
  • #1 Top reasons why Engineers must learn Python!

    #1 Top reasons why Engineers must learn Python!

    Invest in yourself; Knowledge Compounds Engineer's Work Expectation (generated by GPT-4) An engineer applies scientific…

    4 条评论
  • Deep link your Email Signature

    Deep link your Email Signature

    Businesses rely on Emails for communicating with clients. Smartphones are widely used to read and respond to Emails on…

    4 条评论