Hacking Secret Ciphers with Python

(Ann) #1

134 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


Table 6-3: The not operator's truth table.
not A is Entire statement
not True is False
not False is True

The and and or Operators are Shortcuts


Just like for loops let us do the same thing as while loops but with less code, the and and or
operators let us shorten our code also. Type in the following into the interactive shell. Both of
these bits of code do the same thing:





if 10 > 5:
... if 2 < 4:
... print('Hello!')
...
Hello!


if 10 > 5 and 2 < 4:
... print('Hello!')
...
Hello!





So you can see that the and operator basically takes the place of two if statements (where the
second if statement is inside the first if statement’s block.)


You can also replace the or operator with an if and elif statement, though you will have to
copy the code twice. Type the following into the interactive shell:





if 4 != 4:
... print('Hello!')
... elif 10 > 5:
... print('Hello!')
...
Hello!


if 4 != 4 or 10 > 5:
... print('Hello!')
...
Hello!




Free download pdf