Control Through True and False#

One of the five primitive data types in Python are booleans — i.e., expressions that resolve to either True or False. Thus, the five primitive data types are:

  1. int (integer)

  2. float (float)

  3. str (string)

  4. bool (boolean)

  5. None

Mentioned at the start of this part is the need for control over programs. Booleans are fundamental to that control and are required for:

  • while loops, and

  • conditional statements (if, elif, and else)

That comprises the ability to repeat operations and the ability to determine whether something is or is not something else and they are fairly simple in both syntax and operation.

Boolean Basics#

Boolean expressions resolve to either True or False and often resolve based on comparison operators (cf. logical operators below) :

Operator

Example

Meaning

Result

==

a == b

Equal to

True if a is equal to b, False otherwise

!=

a != b

Not equal to

True if not equal, False otherwise

<

a < b

Less than

True if a is less than b, False otherwise

<=

a <= b

Less than or equal to

True if a is less than or equal to b, False otherwise

>

a > b

Greater than

True if a is greater than b, False otherwise

>=

a >= b

Greater than or equal to

True if a is greater than or equal to b, False otherwise

print(1 == 1)
# True

print(1 < 2)
# True

print(1 > 2)
# False

print (1 <= 2)
# True

# can compare string values also
print('cat' != 'dog')
# True
True
True
False
True
True

Use in Conditional Statements#

As mentioned above, among other uses, booleans as the result of comparison are used in conditional statements marked by keywords if, elif, and else:

# using for conditional statements

# data is a string of hexadecimal characters
data = '6e6f77206973207468652074696d6520746f2073656520746865206b696e67207468652074696d6520746'

# but the length will determine what we do with it ...

if len(data) > 200:
    print('Data is too large to process.')

elif len(data) > 50 and len(data) <= 200:
    print('Process data with XYZ protocol.')

else:
    print('Data is useless.')
    
Process data with XYZ protocol.

The conditional statements begin with if, may continue with elif, and can finish with else.

A series of conditional statements always begins with if. It may end there, i.e., you may want to know only whether something is True and, if it isn’t, will forego performing some other operation.

But if you do want to explore more than just one thing is True or you have a default operation to perform, then depending on the number of possibilities, you use either else or elif or both.

Let’s look closer at the syntax, first the if:

if <boolean expression>:
    <statement>
  • if is the keyword that kicks off the code

  • <boolean expr> is an expression that evaluates to either True or False

  • : colon is required immedately after the expression; it causes Python to anticipate an indented code block to follow

  • <statement> is a Python statement, i.e., the command to follow; however, that command is only executed if and only if the expression above can be rendered true.

The elif is identical but for the keyword and for its place in the conditional statement hierarchy.


# Note: immediately preceding the elif comes an if statement 
# then ... if logic demands:

elif <boolean expression>:
    <statement>

The else is merely a default and takes no boolean. It, like the elif must be preceded by an if.

Use in while Loops#

In addition to controlling the direction of a program, i.e., what to do given a change in the data, you often need to repeat a block of code several times over.

In fact, depending on the data you are processing, you may have no way of knowing at any given time the number of iterations you’ll have to make. For such situations the while loop was ready made.

Let’s look closer at the while loop’s syntax:

while <boolean expression>:
    <statements>
    

Example use:

counter = 0

while counter < 5:
    print("In the loop")
    counter += 1
In the loop
In the loop
In the loop
In the loop
In the loop

See the next few pages for some brief videos on while loop basics.