---
# System prepended metadata

title: 'Python: Integers'

---

# Python: Integers

(under construction)

([home](https://github.com/alexhkurz/introduction-to-programming/blob/master/README.md) ... [previous](https://hackmd.io/@alexhkurz/Bk2c2C2oL) ... [next](https://hackmd.io/@alexhkurz/HJ9zbYZnL))

Before we can go on to more powerful programs, we need to learn more about programming and Python. A great way to do so, is to use the REPL, which is short for Read-Evaluate-Print-Loop.

We will also learn about the four basic ingredients of programming: variables, functions, conditionals, and loops. We have seen examples of all of them in the [previous session](https://hackmd.io/@alexhkurz/Bk2c2C2oL) but we didn't have time to explain these concepts in more detail.``

## The Python Console (or REPL)

Open a terminal and type

    >python
    
Here, `>` denotes the so-called prompt. The prompt can look different on your machine. You can also define yourself how it looks like. The important thing to know about the line above  is that the prompt is written by the machine, while `python` was typed by myself.

You can see the terminal as an interface in which you can hold a dialogue with the machine, or, more precisely, the operating system running your machine.

After typing `python` and then using the "return" (or "enter") key of the keyboard, my machine replies

	Python 3.7.6 (default, Feb 15 2020, 15:37:21) 
	[Clang 10.0.0 (clang-1000.11.45.2)] on darwin
	Type "help", "copyright", "credits" or "license" for more information.
	>>> 
    
We do not need to know what Clang is or darwin. But we notice that it was version 3.7.6 of Python that was started. This is mildly interesting as some machines still have Python version 2 installed and that can sometimes lead to incompatibilities. 

Most importantly, the prompt changed to `>>>`. This tells us that we are now not talking to the operating system any more but directly to Python, or, more precisely, to the Python REPL.

When we type something into the REPL, then Python reads it, evalates it, and prints it. Then it is our turn again. Hence REPL for Read-Evaluate-Print-Loop.

Let us try this.

	>>> 1+2

From here on, there is no point in just reading what I have written. You need to run all the commands (and many more) yourself.

Moreover, you should never ask a question to Python without asking it also to yourself. For example, above we ask what does `1+2` evaluate to? Of course, we have a guess what that should be, right?

Only after we came clear on what answer we would expect, we type "return" and see what Python answers. 

	>>> 1+2
    3
    
Puuh, we guessed this, right? But as you go on with this game, you will see that sometimes you guess right and sometimes you don't. Each time you guessed wrongly, you have an opportunity to adjust your mental model of of how Python works. It is the wrong guesses where you learn.

**Exercise:** Try some questions on your own. Imagine you were a maths teacher in school and Python was a pupil you had to test. How much maths can Python do? What does Python know? Give it a thorough test. 

[Hint: Python should at least be able to do what your calculator can do.]

Did you make any wrong guesses? What were they? Maybe you got most guesses right because you did not go beyond Python-as-a-calculator and you already know what to expect from a calculator.

## Integers

We have already seen some calculations that Python can do on numbers. Let us pursue this a bit further.

**Activity:** If you have not tried the following questions, do this now. 
- Does Python know about the order of operations? (Try `1+2*3`.) 
- What does `%` compute? (Try `5%2` etc.)
- What does `//` compute (Try `5//2` etc.)
- What does `**` compute (Try `2**2`, `3**2` etc.)

We can also try some crazy things, for example what would you expect from the following.

    2 @ 3 
    2 $ 3
    
In fact, Python always has an answer (it will take some time before we see an exception to this rule). In the first case, it is

	>>> 2 @ 3
	Traceback (most recent call last):
	  File "<stdin>", line 1, in <module>
	TypeError: unsupported operand type(s) for @: 'int' and 'int'

We learn two thing from this. First, there is actually an operation called `@` (if you wonder why we know this look at the error message produced by `$`). Second, this operation cannot be applied to numbers, or, more specifically to **integers**, which are abbreviated by `int`. Integers are the "whole numbers", that is, $\ldots,-2,-1,0,1,2,\ldots$. 

To summarise, we learnt from this experiment that 
- there is something called *types*,
- an example of a type is `int`,
- the type `int` is the type of integers, aka whole numbers.

In the second case, it is 

	>>> 2 $ 3
	  File "<stdin>", line 1
	    2 $ 3
	      ^
	SyntaxError: invalid syntax

This time we get a different error message **invalid syntax**. In other words, Python does not recognize `$` as on of the things its knows about.


## Homework

There are also some "weird" operations that can be fun to figure out.

- What does the Python operation `^` mean? Similar operations are `&` and `||`.

It can be quite hard to figure them out. 

Start by making a table with the values of `0^0`, `0^1`, `0^2`, ... `1^0`,`1^1`,`1^2`, ... `2^0`,`2^1`,`2^2`, ... and then try to discover the pattern. 

The pattern may look weird ... at which point one needs an idea ... this idea may be difficult to extract just from the data, try to think out of the box ...

I will link a hint in the write-up of a later session.












