Python Tutorial Part 7 | Conditional Flow: If Statements

The next part of our Python learning journey will touch upon control flow, critical to implementing logic in programs. The concept is broad and the first topic to cover will be if/elif statements.

Conditional statements are the first form of control flow we will learn. They are known formally as if / else statements. The underlying logic of if / else statements is likely something you have encountered before in the controls automation space. Take, for example, the pressure relief valve below.
Pressure relief valve in action

Figure 1. Pressure relief valve. 

The valve is a safety mechanism to prevent a system from over-pressurizing and exploding. If the pressure of the system exceeds a certain value, the spring force of the valve is overcome by the gas, and a blow-off of the excess pressure occurs. However, if the pressure remains under the critical threshold the spring pressure keeps the valve closed to the system. The logic can be visualized below.
Pressure relief valve logical flow

Figure 2. Pressure relief valve logical flowchart. 

In Python code, the implementation of the logic that would run a digital version of our pressure relief valve might look like what is shown below:
The input function will allow us to enter a system pressure for the simulated process in the terminal. The pressure variable will be compared against the setpoint variable to determine if it is greater than the setpoint pressure. If greater, the valve will be set high to an open state. If the pressure is less than the setpoint, the valve will remain low in a closed state.
The ‘open’ state appears as below:
Similarly, the ‘closed’ state appears as below:
This example is a great starting point for converting these concepts into actual practice. We did gloss over some fine details of if / else statements, which we will explore further.

Indentation

Did you notice the indentation or spacing of code below the if and else keywords in the example? The spacing is highlighted in yellow in a high-contrast section of our code below:
Indentation in python code
There is significance in the indentation. The indentation represents the code to execute when either the if or else statement evaluates to ‘True.’ The code is conditionally executed; not every line of code executes in the program. To visually represent this, the if block execution is denoted by red highlighting, whereas the else block is denoted in orange in this high-contrast image:
IF and ELSE components of python code
Depending on which condition evaluates to true, the indented code for either the if (red) block or else (orange) block executes, but not both. So, in our previous example, when the pressure was greater than the setpoint, the valve flipped high and activated. The else block was skipped since the if statement evaluated to true:
Similarly, when the pressure was less than the setpoint, the else block evaluated to ‘True’ and the orange single-line print statement was executed, “skipping” the if block:

Assignment versus equality

A simple yet important concept to understand is assignment versus equality. This often chalks up to a common question: “do I use 1 equal sign or 2 equal signs here?”. So far, we’ve primarily worked with variables, where a data type or structure is assigned to a variable. Assignment uses one single equal sign, like this:
setpoint = 125 #psi
However, when using if / else statements, comparisons of equality are denoted by two consecutive equals signs. The back-to-back equal signs are referred to as the ‘equality operator’ used to compare one value to another.

Elif statement and conditional operators

You’ve seen the equality operator and greater than or equal operator thus far. The full list of operators that can be used in conditional statements is given in the table below:

Operator Significance
== Equal to
!= Not equal to
>= Greater than or equal to
<= Less than or equal to
> Greater than
< Less than

Multiple linear, exclusive conditions may also be passed into a conditional statement using the elif keyword. Let’s expand on the pressure relief valve example. Let’s say a monitoring system tied to the process could color code different pressure ranges like below:

Pressure Color
Less than 100 psi Green
Equal or greater than 100 psi but less than 125 psi Yellow
Equal to 125 psi and above Red

Using the elif statement and comparison operators this would appear like below:
Evaluating for the between elif condition:

Nested if statements

In the situation where there are multiple conditions in which one depends on another, nested if statements can be used. Our prior example had one condition: the pressure relief valve must reach 125 psi or greater to open. However, once this occurs, let’s say the process temperature must also be greater than or equal to 400° F for the pressure relief valve to open. Visually, this scenario is represented below:
Pressure and temperature relief valve logical flow

Figure 3. A scenario where temperature depends on pressure conditions. 

Programmatically, the logic for this dependency can be given by the nested if statement:
To test the valve open case, the following inputs were entered:

Wrapping Up

In this article, we introduced the concept of Python program flow control using conditional if / else statements. In the next article, we’ll examine loops and some further examples of control flow that are common in the Python language.


Copyright Statement: The content of this website is intended for personal learning purposes only. If it infringes upon your copyright, please contact us for removal. Email: admin@eleok.com

Leave a Comment