Python for Asshats

Chapter 1

You're probably here because you want to learn the basics of Python programming, or because I sent you this guide and you're reading it because you want to be nice to me. I'm assuming you have absolutely no background in programming, but some amount of common sense, and not a shred of dignity.

Prepare for the Python: It won't hurt if you relax

The first step to learning Python is, understandably, getting Python. Most of you will have to download it from here. I am writing this guide using Python version 3.6.8, but any 3.x.x version will work. Just run the installer, it'll walk you through what you need to do.

Now, if you're some boomer mainframe programmer, you can program in a plain text editor and run it from the command line. The alternative is being a sensible human being and downloading an Integrated Development Environment. These "IDE"s, as the hip kids call them let you write, run, and debug your code all in one place. Amazing! When you installed Python, it came bundled with an IDE called IDLE. If you're a fancy Linux kid and Python came bundled with your distro, you might need to install it using whatever fancy package manager you have. On Ubuntu, it would be done with sudo apt-get install idle3. IDLE, as far as IDEs go, feels rough and simplistic, but it's great for finding your footing.

When opening IDLE, you will be greeted with a friendly Python shell. The interactive shell is good for testing, but I want to build good habits early on, so let's create a new file by pressing ctrl-n. Name it test.py or something. The filename is irrelevant for now, just make sure to use the .py file extension.

Now you will be aquainted with your new best friend — the print function. As your programs runs, it works silently, but whenever it encounters print(), it outputs whatever you put inbetween those parentheses. Enter this lovely bit of code into your program:

print("hello world")

Now save and run it using F5. The following should appear in the console:

hello world

Look on my works, ye Mighty, and despair! We can now output text to the console!

Thinking like a snake

Programming languages have the infamous tendency of doing exactly what you tell them to do and never what you fucking want them to. To avoid the cycle of despair and often alcoholism that stems from unexpected program output, this guide focuses greatly on teaching you to think like the program.

Python is an interpeted language, which means that a process, the eponymous "interpreter" reads through the program file, from top to bottom, and executes each line of code in sequence. If it encounters something that isn't valid code as it reads, it stops to yell at you, then exits. Try running the following program:

print("hello...")
print("...world!")

It should output something like this:

hello...
...world!

It is important to know what the interpreter ignores, namely, comments. Any part of a line following a # is completely disregarded by the interpeter. This seems useless, but they are critical in making it easier for other programmers to figure out what your code does, and helping you remember what the fuck this mess is that you wrote 2 months ago. For now all you need to remember is that this is equivalent to the program I showed above:

print("hello...")
# comments can be on their own lines
print("...world!")  # or after other lines

So far, your programs are, frankly, shit. You can only print lines of text to the console one at a time. In the next chapter, I will show you how to make programs that can actually get things done.