Help support the author by donating or purchasing a copy of the book (not available yet)



Chapter 2 - Integers, Operators and Syntax

2.1 Integers

Before we begin, getting to grips with the material in this chapter is really important yet it’s very easy! There isn’t really anything difficult in this chapter. However, I will say, some of the exercises at the end may catch you out! so pay particular attention to the content.

As I said at the end of the previous chapter, forget that hello world program. In this chapter we’re going to start off with the very basics and that means using Python as an integer calculator.

In the first part of this chapter we’re going to use something called the Python REPL to write our programs.

At the command prompt (in the terminal window), type the following (then ‘Enter’):

python

You should see something like:

Now lets try some integer calculations by typing them at the REPL prompt:

These are known as integer expressions. They are expressions which evaluate to an integer value.

A single value on its own such as the first expression in the above list of expressions is called an integer literal.

An integer is a positive or negative whole numbers.

So, what have we learned so far?

We can also write negative numbers in the same way we normally would.

5 + -5 evaluates to 0.

For completeness, the minus sign before the second 5 in the above expression is called the unary negation operator. It acts on a single value. The + is a binary operator as it operates on two things called operands (5 and -5 in this case).

In computer programming, it is very important that you understand how to read your programs in terms of a programs “parts of speech”. So far we have the following “parts of speech”:

2.2 Operator precedence

Lets take another look at some integer expressions:

What did you notice when you entered these expressions into the REPL?

They give different answers. This is because of something called precedence. Precedence defines the order in which operators are evaluated and operators are evaluated from highest precedence to lowest.

The orders of precedence are as follows:

  1. (…)
  2. *, /
  3. +, -

So, (…) happens first, then * and //, then + and -

For completeness, + and - are said to have equal precedence. Similarly with * and //. However, precedence doesn’t tell us the order of evaluation for operators of equal precedence and it can be important!

Something called associativity determines how operators of equal precedence are grouped (in the absence of parentheses).

The integer operators we have met are left associative meaning they are evaluated from left to right.

To demonstrate this, an expression such as 1 + 2 - 4 + 7 is evaluated as follows:

2.3 Syntax

Now lets throw a spanner in the works. In the Python REPL try these expressions:

You’ve just got your first Syntax Error.

In programming, the syntax of a language is concerned with how we put “characters” of the language together to form valid “words”.

Another way of looking at syntax is, the set of rules which govern what is a valid program and what is not.

A syntax error occurs when we don’t obey these rules (or form “meaningless words”). It’s much the same with English for example. If someone wrote down the word “thwoelaman” you’d probably look at them as if they had two heads and that’s essentially how the interpreter is looking at you, it can’t make sense of what you’ve written!

For integer expressions however, the syntax is pretty obvious.

When you write code that doesn’t follow the Python syntax and you get a syntax error, the python interpreter usually throws out a very useful error message. For example, if we try to evaluate the following expression:

You’ll get a syntax error that looks like this:

  File "<stdin>", line 1
    (1 + (// 4))
          ^
SyntaxError: invalid syntax

We are told the syntax error has probably occurred on line 1. We’re then shown where on line 1 it has probably occurred. This is shown by the ^ character below the expression we tried to evaluate. Python is quite good at pointing out where these errors occurred. In fact, Python is quite good at pointing out where most errors occurred compared to many other languages (at least in my opinion).

Important Side Note

I want to point out two other operators. The ** (power) operator and the % (modulus) operator.

The function of the power operator (**) is quite obvious but the modulus operator (%), maybe not so much. Try these expressions in the REPL:

That’s right, the % operator returns the remainder after division. In the case of 13 % 10, this returns the remainder from 13 divided by 10 which is 3.

2.4 Exercises

Question 1
Question 2
Question 3
Question 4
Question 5


Help support the author by donating or purchasing a copy of the book (not available yet)



Previous Chapter - Next Chapter