Swipe to navigate through the chapters of this book
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.
Please log in to get access to this content
To get access to this content you need the following product:
Hacking is not the same as cracking, which is a term describing computer crime. The two are often confused, and the usage is gradually changing. Hacking, as I’m using it here, basically means “having fun while programming.”
After all, no one expects the Spanish Inquisition . . .
The slightly less simple story is that the rules for identifier names are in part based on the Unicode standard, as documented in the Python Language Reference at https://docs.python.org/3/reference/lexical_analysis.html .
In case you’re wondering—yes, it does do something. It calculates the product of 2 and 2. However, the result isn’t kept anywhere or shown to the user; it has no side effects, beyond the calculation itself.
Note the quotes around storage. Values aren’t stored in variables—they’re stored in some murky depths of computer memory and are referred to by variables. As will become abundantly clear as you read on, more than one variable can refer to the same value.
Function calls can also be used as statements if you simply ignore the return value.
If you don’t understand this sentence, you should perhaps skip the section. You don’t really need it.
This behavior depends on your operating system and the installed Python interpreter. If you’ve saved the file using IDLE in macOS, for example, double-clicking the file will simply open it in the IDLE code editor.
Actually, str is a class, just like int. repr, however, is a function.
Raw strings can be especially useful when writing regular expressions. You learn more about those in Chapter 10.
This is an important method of compression in general, used for example in Huffman coding, a component of several modern compression tools.
- Instant Hacking: The Basics
Magnus Lie Hetland
- Sequence number
- Chapter number
- Chapter 1