Control Structure

Boolean Expression

Boolean expression is an expression that returns a value of True or False, using relational/comparison operators, as well as logical operators. Additionally, Boolean expressions can also use membership operators and identity operators in some cases.

Comparison Operators

Comparison operators are operators that compare two values. These operators are also known as relational operators and are often used to create logic or conditions. Below is a list of Arithmetic Operators in Python:

Operator Symbol
Bigger then >
Lower then <
Equal to ==
Not equal to !=
Bigger then or equal to >=
lower then or equal to <=

Logical Operators

Logical operators are operators used to create logic in the programs we write. Logical operators are also referred to as Boolean Algebra Operators and are typically used to create branching operations in programs. Logical operators include AND, OR, and NOT.

Logical operators consist of:

Operator Symbol
AND Logic and
OR Logic or
Negation/Opposite Logic not

Branching Constructs & Program Blocks

A branching construct is a program that, when executed, will cause branching into its sub-branches, which contain a block of code according to the requested conditions and logic. Generally, branching constructs in the Python programming language can be created using the keywords if/elif/else. Here’s the table:

Information Keyword
There is 1 decision option if
There is 2 decision option if/else
There are more than 2 decision options if/elif/else

A program block contains a set of expressions and statements to be executed by the computer. In the Python programming language, a program block can be identified by a colon (“:”) following the declaration of if/elif/else, for, while, or when defining a function. The program block under the if condition will be executed if the requested condition is true.

The program block under the elif condition, which stands for else if, means that if it does not match the previous condition, it will adjust to other conditions that may be true.

The program block under the else condition will be executed when the value of the previous condition (if/elif) is false.

Below is a simple example of a branching construct program that uses comparison operators:

Code Editor:

1
2
3
4
5
6
7
credits = 45
if (credits >= 120):
print("Senior")
elif (credits >= 90):
print("Junior")
else:
print("New College Student")

MODULE

Download Module 3