Program Flows in Python – Conditional Statements and Boolean Expressions Controls the Program Flow

What will we cover?

We will cover what a program flow is, how this links to conditional statements and boolean expressions.

Step 1: Let’s recall what boolean expressions are

Boolean expression is the building block of conditional statements. So let’s make a brief recap of it.

Given two variables a and b, we have the following boolean expressions with them.

  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b

Where depending on the values in a and b the expressions will evaluate to True of False.

Step 2: Conditional statements

Let’s demonstrate a few if-statements here.

Equality between variables.

a = 10
b = 10
if a == b:
    print("a equals b")

Notice, that if a was not equal b, then it would not print the statement. That is, the indented code after the if-statement, is only executed if the boolean expression evaluates to True.

Another example with less-than.

a = 10
b = 8
if a < b:
    print("a is less than b")

Again the same principles applies.

You can also have an else-clause.

a = 12
b = 12
if a > b:
    print("a is greater than b")
else:
    print("a is not greater than b")

And a elif-clause.

a = 13
b = 12
if a > b:
    print("a is greater than b")
elif a == b:
    print("a is equal to b")
else:
    print("a is less than b")

And a not-equal statement.

a = 10
b = 10
if a != b:
    print("a does not equal b")

What next?

If this is something you like and you want to get started with Python, then this is part of a 8 hours FREE video course with full explanations, projects on each levels, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE 70+ pages eBook with all the learnings from the lessons.

See the full FREE course page here.

Leave a Reply