Hacking Secret Ciphers with Python

(Ann) #1
Chapter 5 – The Reverse Cipher 59

Figure 5 - 2. The parts of a while loop statement.

To understand while loops, we will first need to learn about Booleans, comparison operators,
and blocks.


The Boolean Data Type


The Boolean data type has only two values: True or False. These values are case-sensitive
(you always need to capitalize the T and F, and leave the rest in lowercase). They are not string
values. You do not put a ' quote character around True or False. We will use Boolean values
(also called bools) with comparison operators to form conditions. (Explained later after
Comparison Operators.)


Like a value of any other data type, bools can be stored in variables. Type this into the interactive
shell:





spam = True
spam
True
spam = False
spam
False





Comparison Operators


In line 8 of our program, look at the expression after the while keyword:


reverseCipher.py


  1. while i >= 0:


The expression that follows the while keyword (the i >= 0 part) contains two values (the
value in the variable i, and the integer value 0 ) connected by an operator (the >= sign, called the
“greater than or equal” operator). The >= operator is called a comparison operator.


The comparison operator is used to compare two values and evaluate to a True or False
Boolean value. Table 5 - 1 lists the comparison operators.

Free download pdf