### Become an OU student

Digital thinking tools for better decision making

Start this free course now. Just create an account and sign in. Enrol and complete the course for a free statement of participation or digital badge if available.

# 1 Meet the interactive shell

The next activity introduces you to the Python system you will use to do calculations.

## Activity 1 Warm-up time

During this session you will use an interactive shell, also known as a console. One of these shells has been provided for each activity where you need to do calculations. The first console is below.

Active content not displayed. This content requires JavaScript to be enabled.
Interactive feature not available in single page view (see it in standard view).

Brython is just a particular version of the language Python. This is being used as it is easy to include a Brython console on a web page.

The console above is live and you can use it to do calculations. Try it now. Click the mouse just to the right of the prompt

>>>

When you see the flashing cursor type in a simple sum such as 1+1, so that you see this in the console:

>>> 1+1

Now press the Enter (i.e. Return) key.

Python does the calculation and prints the result. It then moves to the next line and displays the prompt again, showing that it is waiting for further input from you.

>>> 1+1

2

>>>

The Python console can be used in a very similar way to a calculator. However, on a computer keyboard, there are no × or ÷ keys, so instead you have to use * and /, respectively.

Try a few calculations of your own before going on. Remember, the same sequence is repeated:

• You enter something at the prompt >>> and press Enter.
• Python works out the result and prints it.
• Then it moves on the next line and displays the prompt again.

Like a calculator, Python works out multiplications or divisions before additions or subtractions.

>>> 1+2*3

7

>>>

If you want a different order, you use brackets. Anything in brackets is performed first.

>>> 1+2*3

7

>>> (1+2)*3

9

>>>

Python can calculate with decimals and produces either an exact answer or one that cuts off after a certain number of digits.

Try the following divisions (remember that in the console you have to use / for division).

• 1.2 ÷ 3
• 12 ÷ 3
• 2 ÷ 3

You may notice that the last digit is not always correct. This is common in computer arithmetic. It happens because computers represent numbers using 0s and 1s, instead of the more familiar decimal system. This is seldom an issue and won't matter for the calculations you will be doing

Try some calculations of your own. It doesn’t matter much what you choose. The aim is just to play around and get comfortable with working in the Python console.

A handy feature of the Python console is that it always stores the result of the previous calculation. To access the last result, type an underscore _. Try this for example:

>>> 60*60

3600

>>> _

3600

This is useful for chaining together a series of steps without having to enter the whole calculation at the start. The calculation above found the number of seconds in a hour. Instead of just displaying the result again, you can use _ to access the value. Multiplying it by 24 then gives the number of seconds a day, 86,400.

>>> 60*60

3600

>>> _*24

86400

Of course, if you prefer, you can enter the whole calculation at once: 60*60*24.

Now move onto the next actvity below where you are asked to calculate the total number of seconds over a period of time.

## Activity 2 Geological time

Using the console below, calculate the number of seconds in:

• a.one year (assume a year is exactly 365 days)
• b.900 years.
• c.65 million years.
Active content not displayed. This content requires JavaScript to be enabled.
Interactive feature not available in single page view (see it in standard view).

### Discussion

• a.You can work it out like this:
• >>> 60*60*24*365
• 31536000
• b.Then you can use the answer from (a) and multiply by 900:
• >>> _*900
• 28382400000
• c.The best way to find the seconds in 65 million years is probably to repeat the calculation from (a) and multiply by 65,000,000:
• >>> 60*60*24*365*65000000
• 2049840000000000