Skip to main content

Instant Hacking: The Basics | Python 3.8


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. 

    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-explanatory—just follow the link to the most recent version for your platform, be it Windows, macOS, Linux/UNIX, or something else. For Windows and Mac, you’ll download an installer that you can run to actually install Python. For Linux/UNIX, there are source code tarballs that you’ll need to compile yourself, by following the included instructions. If you’re 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 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

Popular posts from this blog

Strings | Core Python 3.8

Strings  Now what was all that "Hello, " + name + "!" stuff about? The first program in this chapter was simply print("Hello, world!") It is customary to begin with a program like this in programming tutorials. The problem is that I haven’t really explained how it works yet. You know the basics of the print statement (I’ll have more to say about that later), but what is "Hello, world!"? It’s called a string (as in “a string of characters”). Strings are found in almost every useful, real-world Python program and have many uses. Their main use is to represent bits of text, such as the exclamation “Hello, world!” Single-Quoted Strings and Escaping Quotes Strings are values, just as numbers are: >>> "Hello, world!"  'Hello, world!' There is one thing that may be a bit surprising about this example, though: when Python printed out our string, it used single quotes, whereas we used double quotes. What’s the differ...

Execution the Program | Core Python 3.8

Saving and Executing Your Programs  The interactive interpreter is one of Python’s great strengths. It makes it possible to test solutions and to experiment with the language in real time. If you want to know how something works, just try it! However, everything you write in the interactive interpreter is lost when you quit. What you really want to do is write programs that both you and other people can run. In this section, you learn how to do just that.     First of all, you need a text editor, preferably one intended for programming. (If you use something like Microsoft Word, which I really don’t really recommend, be sure to save your code as plain text.) If you are already using IDLE, you’re in luck. With IDLE, you can simply create a new editor window with File › New File. Another window appears, without an interactive prompt. Whew! Start by entering the following: print("Hello, world!") Now select File › Save to save your program (which is, in fact, a pl...

Lists and Tuples | Core Python 3.8

This chapter introduces a new concept: data structures. A data structure is a collection of data elements (such as numbers or characters, or even other data structures) that is structured in some way, such as by numbering the elements. The most basic data structure in Python is the sequence. Each element of a sequence is assigned a number—its position, or index. The first index is zero, the second index is one, and so forth. Some programming languages number their sequence elements starting with one, but the zeroindexing convention has a natural interpretation of an offset from the beginning of the sequence, with negative indexes wrapping around to the end. If you find the numbering a bit odd, I can assure you that you’ll most likely get used to it pretty fast.     This chapter begins with an overview of sequences and then covers some operations that are common to all sequences, including lists and tuples. These operations will also work with strings, which will be use...

Functions and Modules | Core Python 3.8

Functions  In the “Numbers and Expressions” section, I used the exponentiation operator (**) to calculate powers. The fact is that you can use a function instead, called pow. >>> 2 ** 3  8  >>> pow(2, 3)  8 A function is like a little program that you can use to perform a specific action. Python has a lot of functions that can do many wonderful things. In fact, you can make your own functions, too (more about that later); therefore, we often refer to standard functions such as pow as built-in functions.     Using a function as I did in the preceding example is called calling the function. You supply it with arguments (in this case, 2 and 3), and it returns a value to you. Because it returns a value, a function call is simply another type of expression, like the arithmetic expressions discussed earlier in this chapter.6 In fact, you can combine function calls and operators to create more complicated expressions (like I did...

Exceptions | Core Python 3.8

When writing computer programs, it is usually possible to discern between a normal course of events and something that’s exceptional (out of the ordinary). Such exceptional events might be errors (such as trying to divide a number by zero) or simply something you might not expect to happen very often. To handle such exceptional events, you might use conditionals everywhere the events might occur (for example, have your program check whether the denominator is zero for every division). However, this would not only be inefficient and inflexible but would also make the programs illegible. You might be tempted to ignore these exceptional events and just hope they won’t occur, but Python offers an exception-handling mechanism as a powerful alternative.     In this tutorial, you’ll learn how to create and raise your own exceptions, as well as how to handle exceptions in various ways. What Is an Exception?  To represent exceptional conditions, Python uses exception obj...