Variables

One of the main ideas that we want to learn from this tutorial is the idea of a variable. You are familiar with variables in a mathematical sense: we use them in functions. I.E. if we have a function $y=x^2$ we say that $y$ is the dependent variable and $x$ the independent variable. So the point here is that as the variable $x$ varies the variable $y$ also varies: This is why they are called variables and not constants.

The concept of a variable in programming is slightly different: in fact it is a much broader concept as variables don't just have to be numbers- they can be arrays (vectors or matrices), strings of letters (words) or even logical objects (true or false).

To assign a variable in Python, we use the '=' sign, i.e to create a variable x which has the value 3, we type

In [1]:
x=3
print(x)
3

Now, we can use x exactly as we would have used the 3. Consider a quadratic equation, evaluated for $x=3$

print(x**2+2*x-3)

Why is this useful? Well suppose we did this without variables: we would type

print(3**2+2*3-3)

And then suppose we wanted to know the value of the same quadratic equation for a new value- 2. Then we would have to retype

print(2**2+2*2-3)

to get our new answer. However, with variables, we can define

x=3
print(x**2+2*x-3)

and then go back and change x=3 to x=2. This saves us finding everywhere we've typed '3' and replacing it with a '2'. (It also saves confusion over which 3's to change- as in the example we're using here the final constant is also a 3, and that should not be changed to a 2). We change it once in the variable definition, and the rest is taken care of.

We don't have to assign variables as simple numbers: we can save the result of a complicated expression as a variable:

x=(3**12)/(2*-13)

Or use variables we've already assigned to define a new variable

In [ ]:
a=3.0
b=2.0
c=a+b
d=c/a

Data types

We've been working so far with variables which are either integers or real numbers (floating point numbers, or floats as they are called in python). There are two more types which will be useful:

Complex numbers

Python uses the variable 1j to signify $\sqrt{-1}$ or $i$ as we would ordinarily refer to it. The reasons for this are historical, and have to do with the way i is used in older codes. It's used differently from other variables. E.G. we can type 2j or 2.0j instead of 2*1j, although the result is the same. This allows to write complex numbers in a recognisable format- with real and imaginary parts:

In [ ]:
C=1.0+3.0j
print (C)

Strings

strings are used for storing words and sentences. For instance we might write some code which uses a person's name, and we want to store that as a variable so that it works for different names. Strings are defined in the same way as text is printed, i.e. with double or single quotes:

In [1]:
my_name="Donald Trump"
my_job="a circus clown"
print ("My name is", my_name, "and I am", my_job)
My name is Donald Trump and I am a circus clown

Functions

So far, mathematically we haven't done anything more complicated than combine numbers- simple algebra. In order to build more complex and interesting programs we're going to need more tools. The main tool (or object as it's known in python and other languages) is the function. Again, like with variables we have an idea from mathematics of what a function is: For instance a simple one-to-one function of one variable takes one number and maps it to another. e.g. $f(x)=3x$ maps the number $x$ to $3x$. Common examples of functions include

  • Trigonometric functions
    • $\sin, \cos, \tan, \arctan, \sec$ etc.
  • Logarithmic/exponential functions
    • $\log, \ln, \exp$
  • polynomials
  • factorial

Calling a function in python requires typing the name of the function followed by the input variable in brackets: e.g. log(3), or sin(13). Now, if you type

log(3)

into python- it will give you an error, because on its own it doesn't know what $\log, \sin$ etc. are. However, there are so-called libraries which we can use to give us access to these functions. There are many libraries which give you access to all kinds of different functionality: we'll look at ones for plotting in this course for instance, but there are libraries for building graphical user interfaces, running parallel jobs on supercomputers... It is one of the strengths of the python language that all of these libraries exist, and can be used by ordinary users to build even more advanced applications.

For now, we will use just one library which is useful for working with numbers. It's called 'numpy' (pronounced num-pie). To load the library we execute

In [ ]:
import numpy as np

This does two things: imports commands from the numpy library, and tells python that we want to refer to those commands via the shortcut 'np'. If you don't enter the 'as np' then you have to type 'numpy' every time and that gets annoying. Now if we want to run commands from numpy we can do it by typing 'np.' followed by the name of the function.

In [ ]:
print (np.log(3))
print (np.sin(13))
print (np.sqrt(49))

Typing something like 'np.log' means "look in the numpy library, and use the function called log". (This is important as there may be a command called log in another library, and we need to specifiy which log we want to use).

Note that log means 'natural log' (there's no function called 'ln'). For base 10 logarithms use log10. This configuration also gives us access to some important constants, which we access in the same way: i.e. np. followed by the name of the constant.

In [ ]:
print (np.pi)
print (np.e)
print (np.cos(2*np.pi))
print (np.log(np.e**2))

You can find a full list of the functions available from the numpy website

Now we can use these functions to build more complicated expressions:

In [ ]:
x=(3**2)+np.log(7)*np.tan(np.pi/3)
print (x)

The inverse trignometric functions are defined as

np.asin()
np.atan()
np.acos()

sec and cosec are not defined, but you can just use

1./np.sin(x)

to evaluate cosec(x) for instance. Remember that while we write $\sin ^2 x$ to avoid confusion between $\sin(x^2)$ and $\sin(x)^2$, the real meaning of $\sin^2(x)$ is $(\sin(x))^2$:

In [ ]:
(np.sin(3))**2

Exercises

You should now be able to attempt exercises 1.0 and 1.1 on variables