When writing computer commands that you can type, the font will change to command
. For example, x=2**3.4/5
is a command that can be typed in.
When we talk about a general command, or some piece of missing data that you should complete, we will use angle brackets as in <command>
or <variable>
. For example, <location>
could mean "Southampton"
.
When showing actual commands as typed into Python, they will start with In [<number>]:
. This is the notation used by the IPython console. The <number>
allows you to refer to previous commands more easily. The output associated with that command will start with Out [<number>]:
.
When displaying code, certain commands will appear in different colours. The colours are not necessary. They highlight different types of command or variable. When using the spyder editor you may find the colours match up and are useful: if not, either ignore them or switch them off.
Start with using Python as a calculator. This is similar to typing commands directly into an Excel worksheet. Look at the console in the bottom right part of spyder. Here we can type commands and see a result. Simple arithmetic gives the expected results:
In [1]:
2+2
Out[1]:
In [2]:
(13.5*2.6-1.4)/10.2
Out[2]:
If we want to raise a number to a power, say $2^4$, the notation is **
:
In [3]:
2**4
Out[3]:
There is an issue with division. If we divide an integer by an integer, Python 3.X
will do real (floating point) division, so that:
In [4]:
5/2
Out[4]:
However, Python 2.X
will do division in the integers:
In []: 5/2
Out []: 2
If you are using Python 2.X
and want the division operator to behave in this way, start by using the command
In [5]:
from __future__ import division
If you really want to do integer division, the command is //
:
In [6]:
5//2
Out[6]:
A variable is an object with a name and a value:
In [7]:
x = 2
This should look familiar to VBA, with one big difference: you don't have to declare the type of the variable ahead of time. So no statements like
DIM x AS INTEGER
are needed. In fact, the same variable can be assigned variables of different types without any problem:
In [8]:
x = 2.3
x = "hello"
We see that there's no output when you define a variable. To get output, use the print
function:
In [9]:
print(x)
x = 3.4
print("x =", x)
x = 7
print("x = {}".format(x))
Variable names have certain rules and conventions, which are very similar to VBA.
Variables must
!@#$%^&*()\|
)Variables should
More detail can be found in PEP8.
It is strongly recommended that variables do not use any extended characters, but only the basic latin characters, numbers, and underscores.
Check that you are happy manipulating variables in the console. For example,
In [10]:
x = 3
y = 17.5
In [11]:
x+y
Out[11]:
In [12]:
x/y
Out[12]:
In [13]:
x**(-2/y)
Out[13]:
Assigning a value to one variable does not change the value of other variables. Let's set the value of variable y
to be a function of the variable x
.
In [7]:
x = 1.5
y = x**2
print('x: {}, y: {}'.format(x, y))
If we change x
by assigning it a new value, we see that y
is not automatically updated. Once a value is assigned to a variable, it does not 'remember' where this value came from. This is different from how spreadsheets work.
In [8]:
x = 3
print('x: {}, y: {}'.format(x, y))
Also try doing some things that you expect to break, so you understand the error messages:
In [14]:
x / 0
In [15]:
z = "hello"
y * z
A lot of VBA depends on subroutines and functions to repeatedly execute commands. The Python console is no use for this. Instead we must save the commands to a file, sometimes called a script. This will be a plain text file, containing Python commands. The name of the file follows the same rules as the names of Python variables.
The editor in spyder is a good way of creating Python files, as it has additional tools to make it easier.
As a simple example, we will use the three lines
In [16]:
x = 3
y = 11.5
x+y
Out[16]:
Go to the editor and type in the three lines:
x = 3
y = 11.5
x+y
Save the file in a suitable location with the name script1.py
.
To run this program, either
F5
;In the console you should see a line like
runfile('/Users/ih3/msc-or-python/script1.py', wdir='/Users/ih3/msc-or-python')
appear, and nothing else. To check that the program has worked, check the value of y
. In the console just type y
:
In [17]:
y
Out[17]:
Also, in the top right of the spyder window, select the "Variable explorer" tab. It shows the variables that it currently knows, which should include y
, its type (float) and its value.
Why did running the script not produce output? By default, output is only produced in the console. If you want a script to really produce output, use the print
function.
Edit your script to read
x = 3
y = 11.5
print(x+y)
Save it and run it again: you should now see output in the console.
One single file containing Python commands isn't much help. Being able to re-use previous files (and their variables and functions) is what we really want to do. To do this we need to get the information from a file.
In the console type
In [18]:
import script1
This finds the file script1.py
and runs all the commands in it. As the file has been edited to explicitly print
the result of x+y
, we see output.
What we're really interested in is the values defined in the file. Type
In [19]:
script1.y
Out[19]:
We see the value in the file has been stored in <filename>.value
. This means we never have to worry about the value in the file over-writing the value we currently have, as they have different names. Check this by typing:
In [20]:
y = 17.2
print(y)
print(script1.y)
We can use import
to get access to a huge range of in-built Python packages. For example, the numpy
package gives us access to more useful mathematical and numerical functions:
In [21]:
import numpy
print("pi is", numpy.pi)
print("log(y) is", numpy.log(y))
Check that you are happy with tab completion and/or using the help to work out how to compute $\tanh(e)$ using numpy
commands.
In [22]:
numpy.tanh(numpy.e)
Out[22]:
Write a script file that import
s another script (eg, script2.py
that contains the line import script1
). Check that you can, inside script2
, access and print variables defined in script1
. Then run the file and check that this means you can access the variables from both script1
and script2
in the console, and check how they appear.