Hacking Secret Ciphers with Python

(Ann) #1

50 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


...this means you are running the program with Python 2, instead of Python 3. This makes the
penguin in the first chapter sad. (The error is caused by the input() function call, which does
different things in Python 2 and 3.) Please install Python 3 from http://python.org/getit before
continuing.


Opening The Programs You’ve Saved


Close the file editor by clicking on the X in the top corner. To reload a saved program, choose
File ► Open from the menu. Do that now, and in the window that appears choose hello.py and
press the Open button. Your saved hello.py program should open in the File Editor window.


How the “Hello World” Program Works


Each line that we entered is an instruction that tells Python exactly what to do. A computer
program is a lot like a recipe. Do the first step first, then the second, and so on until you reach the
end. Each instruction is followed in sequence, beginning from the very top of the program and
working down the list of instructions. After the program executes the first line of instructions, it
moves on and executes the second line, then the third, and so on.


We call the program’s following of instructions step-by-step the program execution, or just the


execution for short. The execution starts at the first line of code and then moves downward. The
execution can skip around instead of just going from top to bottom, and we’ll find out how to do
this in the next chapter.


Let’s look at our program one line at a time to see what it’s doing, beginning with line number 1.


Comments


hello.py



  1. This program says hello and asks for my name.




This line is called a comment. Comments are not for the computer, but for you, the programmer.
The computer ignores them. They’re used to remind you of what the program does or to tell
others who might look at your code what it is that your code is trying to do. Any text following a


sign (called the pound sign) is a comment. (To make it easier to read the source code, this


book prints out comments in a light gray-colored text.)


Programmers usually put a comment at the top of their code to give the program a title. The IDLE
program displays comments in red text to help them stand out.

Free download pdf