It’s 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. I’ll go through the basics, starting with the excruciatingly simple, but because Python is such a powerful language, you’ll soon be able to do pretty advanced things.
To begin, you need to install Python, or verify that you already have it installed. If you’re 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.
Once you have Python installed, try to fire up the interactive interpreter. If you’re using the command line, you could simply use the python command, or perhaps python3 if you have an older version installed as well. If you’d 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 hackerdom—your first step in taking control of your computer. In more pragmatic terms, it’s an interactive Python interpreter. Just to see if it’s working, try the following:>>> print("Hello, world!")
When you press the Enter key, the following output appears:Hello, world!
>>>
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 won’t 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 didn’t understand that.2 (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 what’s 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, let’s press on. After all, the interpreter isn’t much fun when you don’t know what to tell it.
Comments
Post a Comment