Prologue to Python

Some logistics and warmup

Jack Holland
Understanding computer science
6 min readApr 15, 2015

--

This is an ongoing series. Please check out the collection for the rest of the articles.

We’re on the verge of an exciting new chapter, but first we need to buckle down and cover some basics. Learning Python will open up a large landscape to explore, so it’s worth spending the time to learn Python in detail.

If you’ve been reading this from the beginning, it will help to remember Cake, the made-up language we used before. If you want a more basic introduction to the ideas behind programming, check out the earlier posts that use Cake.

I want to start by saying that Python is a rich and intricate language. It won’t take long to learn the fundamentals, but it will take a long time to explore every nook and cranny of the language. I’ve been using Python for years and I’m still discovering new features and details. That said, Python is also designed to be intuitive and approachable — it tries to avoid the arcane corner cases you’ll find in C and the bureaucratic rigidity Java is often accused of having, for instance. We’ll discuss language design in much more detail down the road, but without further ado, let’s talk about Python.

Obligatory XKCD comic

To start, let’s use Python’s interactive mode. This is a way for you to converse with Python in a back-and-forth style; you say something to Python and it says something back. It works very much like the terminal we learned about in the last few posts, so check those posts out if anything that follows is unclear.

Speaking of the terminal, I encourage you to follow along by opening your terminal and entering

You will now be in Python’s interactive mode and can follow along and experiment as you like. Actually, Python has two different versions in widespread use: version 2 and version 3. The two versions have some subtle but significant differences, and I’m using version 3, so you’ll want to use version 3 as well if you want to follow along exactly. You can check your version with this command:

If the result is something starting with a 2, try entering this instead:

If the terminal doesn’t recognize this command, make sure you’ve installed version 3 of Python.

When you open the interactive mode, Python prompts you with a triple greater-than-sign (>>>) and you can ask it to evaluate something:

Luckily, this is not the limit of what Python can do

In this case, I asked Python to evaluate “1 + 1” and it informs me that the answer is 2. In general, addition, subtraction, multiplication, division, and modulo work as expected:

You can use parentheses to control the order of operations:

And you can even nest your parentheses:

1 + (1 + (1 + 1)) simplifies to 1 + (1 + 2), which simplifies to 1 + 3, which is 4

Python uses two asterisks for exponentiation:

2*2*2=8

It doesn’t matter if you separate operators with spaces; you can make it compact like this if you want:

Python isn’t just a glorified pocket calculator, of course. For starters, you can compare things just like in Cake:

As before, == means ‘equal to’ and != means ‘not equal to’. < stands for ‘less than’ and <= for ‘less than or equal to’. (Unsurprisingly, > and >= stand for ‘greater than’ and ‘greater than or equal to’, respectively).

As you can see from the results above, Python has Boolean values as well. It’s important to note that they are capitalized — Python won’t know what you’re talking about if you enter true instead of True. You can compare Booleans:

While this is a silly example, comparing Booleans is useful when you’re comparing Boolean variables whose values you aren’t sure about

And you can evaluate combinations of them with and and or:

Python’s xor operator isn’t called “xor” like in Cake. Instead, Python uses the caret symbol (^), like this:

Since ^ is often used as the exponentiation operator in math, it initially may be confusing to see it used for xor, but you’ll get used to it, especially since most programming languages employ this usage. As mentioned above, ** is the exponentiation operator.

You can also perform xor by comparing the two Booleans with !=, as you can see below:

An alternative to ^

Remember that xor is the exclusive or operator: x xor y is True when either x or y is True, but not when both or neither are True. So if x and y are both True, then x != y is False. If x and y are both False, then x != y is False. The only way for x != y to return True is if one of the variables is True and the other is False — that is, they don’t equal each other, which is exactly what != tests for. Take a look at my earlier explanation of xor if you’d like a more thorough discussion on it.

Feel free to play around in Python’s interactive mode. If you’re not sure about something, experiment! You won’t break anything (but read the last post if you’d like to). When you’re done, you can exit to the regular terminal by calling the special exit function:

Next time, I’ll introduce how to create standalone programs with Python since the interactive mode we used in this post isn’t suited for writing many lines of code at once. Then I’ll replicate most of what we did with Cake using Python. This should give you a better feel for basics and finish the transition from Cake to Python. After that, we’ll delve a little more deeply into Python before exploring a few more basic (but interesting) algorithms.

Metanote: We’re entering significantly more interesting territory now, but this means that the complexity is going to be increasingly hard to manage. So far, I’ve basically been winging it in terms of our overall lesson structure — I’ve always planned several articles in advance, but several isn’t going to cut it soon.

With that in mind, I’ve put together a more formal lesson plan in the hope of not accidentally skipping important intermediate steps and having to backtrack. I am obsessively determined to make this the most intuitive, gradual explanation of computer science feasible. If at any point you’re confused by what I’m saying, please let me know and I’ll write a better explanation. My intended audience is not geniuses or experts, it’s beginners!

I’ve also allocated a small but fixed part of my day to work on this, since lately I’ve had the discipline of a cat when it comes to this blog. Since I’ll never get to the cool stuff with these weeks-long lapses, I’ve committed to a slow but steady pace. Feel free to nag me if you find yourself waiting too long in between articles.

Image credit: Python logo, XKCD

--

--