Hacking Secret Ciphers with Python

(Ann) #1

132 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


transpositionDecrypt.py


  1. plaintext[col] += symbol

  2. col += 1 # point to next column


As the first step in this loop we concatenate symbol to the string at index col in the


plaintext list. Then we add 1 to col (that is, we increment col) on line 41 so that on the
next iteration of the loop, symbol will be concatenated to the next string.


The and and or Boolean Operators


The Boolean operators and and or can help us form more complicated conditions for if and
while statements. The and operator connects two expressions and evaluates to True if both
expressions evaluate to True. The or operator connects two expressions and evaluates to True
if one or both expressions evaluate to True. Otherwise these expressions evaluate to False.
Type the following into the interactive shell:





10 > 5 and 2 < 4
True
10 > 5 and 4 != 4
False





The first expression above evaluates to True because the two expressions on the sides of the
and operator both evaluate to True. This means that the expression 10 > 5 and 2 < 4
evaluates to True and True, which in turn evaluates to True.


However, for the second above expression, although 10 > 5 evaluates to True the expression
4 != 4 evaluates to False. This means the whole expression evaluates to True and
False. Since both expressions have to be True for the and operator to evaluate to True,
instead they evaluate to False.


Type the following into the interactive shell:





10 > 5 or 4 != 4
True
10 < 5 or 4 != 4
False





For the or operator, only one of the sides must be True for the or operator to evaluate them
both to True. This is why 10 > 5 or 4 != 4 evaluates to True. However, because both

Free download pdf