Hacking Secret Ciphers with Python

(Ann) #1

40 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


A function (like print() in the above example) has code in that performs a task, such as
printing values on the screen. There are many different functions that come with Python. To call
a function means to execute the code that is inside the function.


The instructions in the above example pass a value to the print() function in between the
parentheses, and the print() function will print the value to the screen. The values that are
passed when a function is called are called arguments. (Arguments are the same as values
though. We just call values this when they are passed to function calls.) When we begin to write
programs, the way we make text appear on the screen is with the print() function.


You can pass an expression to the print() function instead of a single value. This is because
the value that is actually passed to the print() function is the evaluated value of that
expression. Try this string concatenation expression in the interactive shell:





spam = 'Al'
print('Hello, ' + spam)
Hello, Al





The 'Hello, ' + spam expression evaluates to 'Hello, ' + spam, which then
evaluates to the string value 'Hello, Al'. This string value is what is passed to the print()
function call.


Escape Characters


Sometimes we might want to use a character that cannot easily be typed into a string value. For
example, we might want to put a single quote character as part of a string. But we would get an
error message because Python thinks that single quote is the quote ending the string value, and
the text after it is bad Python code instead of just the rest of the string. Type the following into the
interactive shell:





print('Al's cat is named Zophie.')
File "", line 1
print('Al's cat is named Zophie.')
^
SyntaxError: invalid syntax





To use a single quote in a string, we need to use escape characters. An escape character is a
backslash character followed by another character. For example, \t, \n or \'. The slash tells

Free download pdf