Hacking Secret Ciphers with Python

(Ann) #1
Chapter 8 – The Transposition Cipher, Encrypting 119

This is why ''.join(eggs).upper().join(eggs) returns the string,
'dogsDOGSCATSMOOSEcatsDOGSCATSMOOSEmoose'.


Whew!


Remember, no matter how complicated an expression looks, you can just evaluate it step by step
to get the single value the expression evaluates to.


Return Values and return Statements


transpositionEncrypt.py



  1. Convert the ciphertext list into a single string value and return it.



  2. return ''.join(ciphertext)


Our use of the join() method isn’t nearly as complicated as the previous example. We just
want to call join() on the blank string and pass ciphertext as the argument so that the
strings in the ciphertext list are joined together (with nothing in between them).


Remember that a function (or method) call always evaluates to a value. We say that this is the
value returned by the function or method call, or that it is the return value of the function. When
we create our own functions with a def statement, we use a return statement to tell what the
return value for our function is.


A return statement is the return keyword followed by the value to be returned. We can also
use an expression instead of a value. In that case the return value will be whatever value that
expression evaluates to. Open a new file editor window and type the following program in and
save it as addNumbers.py, then press F5 to run it:


Source code for addNumbers.py



  1. def addNumbers(a, b):

  2. return a + b



  3. spam = addNumbers(2, 40)

  4. print(spam)


When you run this program, the output will be:


42


That’s because the function call addNumbers(2, 40) will evaluate to 42. The return
statement in addNumbers() will evaluate the expression a + b and then return the evaluated

Free download pdf