1. Instant Hacking: The Basics
I ts time to start hacking. In this chapter, you learn how to take control of your computer by speaking a language it understands: Python. Nothing here is particularly difficult, so if you know the basic principles of how your computer works, you should be able to follow the examples and try them out yourself. Ill go through the basics, starting with the excruciatingly simple, but because Python is such a powerful language, youll soon be able to do pretty advanced things.
To begin, you need to install Python, or verify that you already have it installed. If youre running macOS or Linux/UNIX, open a terminal (the Terminal app on a Mac), type in python , and press Enter. You should get a welcome message, ending with the following prompt:
>>>
If you do, you can start entering Python commands immediately. Note, however, that you may have an old version of Python. If the first line starts with Python 2 rather than Python 3 , you might want to install a newer version anyway, as Python 3 introduces several breaking changes.
The details of the installation process will of course vary with your OS and preferred installation mechanism, but the most straightforward approach is to visit www.python.org , where you should find a link to a download page. It is all pretty self-explanatoryjust follow the link to the most recent version for your platform, be it Windows, macOS, Linux/UNIX, or something else. For Windows and Mac, youll download an installer that you can run to actually install Python. For Linux/UNIX, there are source code tarballs that youll need to compile yourself, by following the included instructions. If youre using a package manager such as Homebrew or APT, you can use that to streamline the process.
Once you have Python installed, try to fire up the interactive interpreter. If youre using the command line, you could simply use the python command, or perhaps python3 if you have an older version installed as well. If youd rather use a graphical interface, you can start the IDLE app that comes with the Python installation.
The Interactive Interpreter
When you start up Python, you get a prompt similar to the following:
Python 3.5.0 (default, Dec 5 2015, 15:03:35)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.1.76)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
The exact appearance of the interpreter and its error messages will depend on which version you are using. This might not seem very interesting, but believe me, it is. This is your gateway to hackerdomyour first step in taking control of your computer. In more pragmatic terms, its an interactive Python interpreter. Just to see if its working, try the following:
>>> print("Hello, world!")
When you press the Enter key, the following output appears:
If you are familiar with other computer languages, you may be used to terminating every line with a semicolon. There is no need to do so in Python. A line is a line, more or less. You may add a semicolon if you like, but it wont have any effect (unless more code follows on the same line), and it is not a common thing to do.
So what happened here? The >>> thingy is the prompt. You can write something in this space, like print "Hello, world!" . If you press Enter, the Python interpreter prints out the string Hello, world! and you get a new prompt below that.
What if you write something completely different? Try it out:
>>> The Spanish Inquisition
SyntaxError: invalid syntax
>>>
Obviously, the interpreter didnt understand that. (If you are running an interpreter other than IDLE, such as the command-line version for Linux, the error message will be slightly different.) The interpreter also indicates whats wrong: it will emphasize the word Spanish by giving it a red background (or, in the command-line version, by using a caret, ^ ).
If you feel like it, play around with the interpreter some more. For some guidance, try entering the command help() at the prompt and pressing Enter . You can press F1 for help about IDLE. Otherwise, lets press on. After all, the interpreter isnt much fun when you dont know what to tell it.
Algo... What?
Before we start programming in earnest, Ill try to give you an idea of what computer programming is. Simply put, its telling a computer what to do. Computers can do a lot of things, but they arent very good at thinking for themselves. They really need to be spoon-fed the details. You need to feed the computer an algorithm in some language it understands. Algorithm is just a fancy word for a procedure or recipea detailed description of how to do something. Consider the following:
SPAM with SPAM, SPAM, Eggs, and SPAM: First, take some SPAM.
Then add some SPAM, SPAM, and eggs.
If a particularly spicy SPAM is desired, add some SPAM.
Cook until done -- Check every 10 minutes.
Not the fanciest of recipes, but its structure can be quite illuminating. It consists of a series of instructions to be followed in order. Some of the instructions may be done directly (take some SPAM), while some require some deliberation (If a particularly spicy SPAM is desired), and others must be repeated several times (Check every 10 minutes.)
Recipes and algorithms consist of ingredients (objects, things) and instructions (statements). In this example, SPAM and eggs are the ingredients, while the instructions consist of adding SPAM, cooking for a given length of time, and so on. Lets start with some reasonably simple Python ingredients and see what you can do with them.
Numbers and Expressions
The interactive Python interpreter can be used as a powerful calculator. Try the following:
>>> 2 + 2
This should give you the answer 4. That wasnt too hard. Well, what about this:
Still not impressed? Admittedly, this is pretty standard stuff. (Ill assume that youve used a calculator enough to know the difference between 1 + 2 * 3 and (1 + 2) * 3 .) All the usual arithmetic operators work as expected. Division produces decimal numbers, called floats (or floating-point numbers ).
If youd rather discard the fractional part and do integer division, you can use a double slash.
>>> 1 // 2
>>> 1 // 1
>>> 5.0 // 2.4
2.0
In older versions of Python , ordinary division on integers used to work like this double slash. If youre using Python 2. x , you can get proper division by adding the following statement to the beginning of your program (writing full programs is described later) or simply executing it in the interactive interpreter:
>>> from __future__ import division
Note
In case its not entirely clear, the future in the instruction is surrounded by two underscores on both sides: __future__ .
Another alternative, if youre running an old Python from the command line, is to supply the command-line switch -Qnew . There is a more thorough explanation of the __future__ stuff in the section Back to the __future__ later in this chapter.