Python_01b

This page covers are the absolute basics of using the Spyder/Python IDE for simple calculations

Simple arithmetic and introduction to built in functions

Python can be used as a simple calculator.
Just type the calculation in the console window which has a prompt that looks like this 

In [1]:

and then hit return which we will show with this character

↲ 

and the answer comes out in the same window with a prompt like this:

Out[1]:

Example:

In [1]: 2 * 2 
Out[1]: 4

If you type something that doesn't make sense you will get an error message:

In [1]: 2 * / 5

    2 * / 5
        ^
SyntaxError: invalid syntax

Notice that Python attempts to show you where you have made a mistake with a caret pointing to the symbol it doesn't understand. Press the up arrow on the keyboard to edit and re-type the line correcting the error.

Python has many hundreds of functions accessed through loading, or importing, libraries eg:

In [1]: import math

In [2]: sqrt(2)

Out[2]: 1.4142135623730951

A definitive list of the functions is available in the product help. The list of available functions depends which libraries you have imported. Here are some for you to try:

math.sin(3)

math.exp(1)

math.log(10)

If you want to know more about the functions and how to use them then type 'help' followed by the name of the function in brackets:

help(math.log)

Or you can press F1 to look up the function in the help files.

You can type in expressions of more or less any length and complexity. For example if you want to know the length of the hypotenuse of a triangle with sides 3 and 4 you might try

In [1]: math.sqrt(3*3 + 4*4)

Out[1]: 5.0

For some expressions it is possible to type things that give answers, but do not give you the answers you want! This might be because of the order in which the calculation is done. For example:

In [1]: 7 + 4 / 2

Out[1]: 9

the mean value of 7 and 4 is not 9.  This expression gives you 7 + (4 / 2) not (7 + 4) / 2.  To calculate the mean value of 7 and 4 we need:

In [1]: (7 + 4) / 2

Out[1]: 5.5

The brackets ensure that the addition is done before the division.

Introduction to Variables

Values that need to be stored for later use are given names. These are called 'variables' because they can hold any value and the value can be changed at any time:

In [1]: my_age = 50;

In [2]: my_savings = 4500.66;

These variables can be used anywhere you would use the value they contain, just type the name where you want to use it's value:

In [3]: avg_savings_per_year = my_savings/my_age;

In [4]: my_age = my_age + 1;

For longer sequences of commands it is convenient to type them in to file editing window - this is by default on the left of the screen:

To see the result of this code segment just click on the 

▶ 

button (which indicates 'run') visible above the code on the toolbar; the result can be seen in the console.

Sets of commands that are typed in the editing window can be loaded, saved, named, and so on just like documents. They are plain text files which do not allow formatting - the colour highlighting of the individual elements of the code you can see in the example is provided automatically by the editor - although this is customisable.