#### 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.

• REPL stands for read-evaluate-print-loop:
• Read one line of input
• Evaluate the expression
• Print the resulting value
• Loop (repeat).

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

`python`

You should see something like:

``````Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>``````

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

``````5
5 + 7
5 - 3
4 * 4
15 // 5
5 * 2 + 3
3 + 2 * 5
(3 + 2) * 5``````

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 write integers in the obvious way
• We’ve met some basic operators ( +, -, * and // ). Integer operators in this case.
• We use * for multiplication and // for division
• There’s something funny about * happening before +
• We also use ( and ) for grouping
• Integer expressions evaluate to Integer values

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”:

• Integer literals
• Integer operators
• Binary operators: +, -, * and //
• Unary operators: - (negation)
• Operands: 5 + 3
• Integer expressions
• Integer values

## 2.2 Operator precedence

Lets take another look at some integer expressions:

``````3 + 2 * 5
(3 + 2) * 5``````

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:

``````1 + 2 - 4 + 7
(1 + 2) - 4 + 7     # Evaluate 1 + 2 first
((1 + 2) - 4) + 7   # Evaluate 3 - 4 next
(((1 + 2) -4) + 7)  # Evaluate -1 + 7``````

## 2.3 Syntax

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

``````4 + 3 +
(1 + 1))
5 + 3 2
9 + - 3
8 + + 9
1 * * 6``````

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:

``>>> (1 + (// 4))``

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:

``````2 ** 2
3 ** 2
4 % 3
13 % 10``````

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
• Is the ** (power) operator left or right associative? (Try figuring this out in the REPL)
##### Question 2
• Is the % operator left or right associative? (Try figuring this out in the REPL)
##### Question 3
• Where in the precedence hierarchy do ** and % appear?
##### Question 4
• How are the following expressions evaluated?
• `1 + 4 - 5 * 7 // 3`
• `2 ** 3 * 4 + 8 // 3`
• `4 % 3 ** 5 - 2 ** 2`
##### Question 5
• From the following list of integer expressions, which expression(s) have invalid syntax?
• `((4 * 6 - (4 // 2))`
• `9 * - 11 + 6`
• `6 - +4`

#### Help support the author by donating or purchasing a copy of the book (not available yet) Previous Chapter - Next Chapter