Loops, functions and conditional statements

This far, python has worked more or less like an advanced calculator. One of the strong advantages of using a programming language for mathematics is the possibility to write your own functions and to get the computer to repeat a task many times. We will start with the second bit.

Loops

In mathematics we often define objects using indexes over a cenrain domain. We might sum the integers from $0$ to $n$ or define a sequence $(\frac{1}{k})_{k = 1}^N$. How do we do this in python? We use loops for this. A loop is a set of instructions that tell the interpreter to repeat some code until a condition is met. The most common ones in python are the for-loop and the while-loop. We will look at the for-loop, there is plenty documentation on while-loops on the web. The basic structure of the for-loop is this:

for ELEMENT in LIST:
    do this
    and do this
this is not performed in the loop

What it means is that ELEMENT takes of each value in LIST and the indented code is excecuted for each step. After ELEMENT has taken on each value of LIST the loop is finished and the iterpreter goes to the next line of code. Here is an example:


In [13]:
simplelist = [1,3.4,['a',5],"somethin'",("bar","foo")]

for thing in simplelist:
    print thing
print "\n" #Add an extra new line
print "This is after the loop"


1
3.4
['a', 5]
somethin'
('bar', 'foo')


This is after the loop

Notice that the indentation is crucial. This is how python knows that certain lines of code belong to the loop. The standard is to use four spaces for indentation and I strongly urge you to do the same, but as long as the indentation is consistent within each block the code will run. Blocks of code can be conditions, loops or functions and they can be within other blocks of code.


In [4]:
anotherList = [[1,2],[3,2],[3,4]]
for first in anotherList:
    for second in first: #first is a list so it can be used in a loop
        print second


1
2
3
2
3
4

while lists are the most common to loop over, you can loop over any object that is ordered.


In [6]:
simpletuple = (2,3,"2")
for i in simpletuple:
    print i


2
3
2

In [7]:
some_string = "Dust tastes good"
for i in some_string:
    print i


D
u
s
t
 
t
a
s
t
e
s
 
g
o
o
d

Often you want to just repeat the code a certain amount of times or do it from, say, $0$ to $n$. To do this we can use the range function.


In [8]:
range(10)


Out[8]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [9]:
range(1,11) #instead create a list from 1 up to but not including 11


Out[9]:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

To use this to find the factorial of $54$ we could write


In [10]:
solution = 1
for i in range(1,55):
    solution = solution * i
print solution


230843697339241380472092742683027581083278564571807941132288000000000000

We can also use loops to create lists. Here is an example of the sequence $(\frac{1}{n})_{n=1}^{14}$


In [12]:
sequence = []
for i in range(1,15):
    sequence.append(1.0/i)
print sequence


[1.0, 0.5, 0.3333333333333333, 0.25, 0.2, 0.16666666666666666, 0.14285714285714285, 0.125, 0.1111111111111111, 0.1, 0.09090909090909091, 0.08333333333333333, 0.07692307692307693, 0.07142857142857142]

There is another way to create the same list which might be a bit easier for mathematicians to write and to read. You can create it like this:


In [14]:
sequence = [1.0/i for i in range(1,15)]
print sequence


[1.0, 0.5, 0.3333333333333333, 0.25, 0.2, 0.16666666666666666, 0.14285714285714285, 0.125, 0.1111111111111111, 0.1, 0.09090909090909091, 0.08333333333333333, 0.07692307692307693, 0.07142857142857142]

Functions

We have already used several functions in python. They work very much the same as functions behave in mathematics. For example, if we have the function $f(x,y) = 2x + y - xy$ we expect it to demand to values for $x$ and $y$ and return $2x + y - xy$ as a single value. In python, the structure of a function is as follows:

def FUNCTION(VARIABLES):
    the function does
    several things
    and usually ends with
    return RESULT

Which means that if we want to define the function above we simply write. Remember that the code of the function has to be indented!


In [15]:
def some_function(x, y):
    theResult = 2*x + y - x*y
    return theResult

The return statement means that the function will return that as the answer. It is not neccessary to include it as some functions might print things instead, but it is very common for it to be included. If we wish to use the function we simply write


In [20]:
theResult = some_function(3,1)
print theResult


4

A function can call other functions and can even call on itself. If we wanted to create a function that calculated ${n\choose k} = \frac{n!}{k!(n-k)!}$ we could first define a function that calculated the factorial which we use to calculate the original function


In [21]:
def factorial(x):
    result = 1
    for i in range(1, x + 1): # Remember that range(a,b) creates the list [a, a + 1, ..., a - 1]
        result *= i
    return result

def choose(n, k):
    result = factorial(n)/(factorial(k)*factorial(n - k))
    return result

print choose(10,5)


252

Importing libraries

Even though there are many functions in python from the start, there are many more we might need. The function factorial does not exist in python from the start which meant we had to write it, and it is the same with many other useful functions. Most of these have actually already been written by someone else. To include them we need to import the library that they are in. A library can be seen as a set of functions and when we import the library we make the functions available to the interpreter. The function factorial exists in the library math which we can import and use the function from there. To use a function from a library we first have to state the library and then that function with a dot in between

LIBRARY.FUNCTION

In [30]:
import math

math.factorial(5)


Out[30]:
120

We can also import specific functions from a library by writing


In [31]:
from math import cos, pi
cos(2*pi)


Out[31]:
1.0

Notice that in this case we do not use the function by first friting the name of the library.

Conditional statements

Conditional statements are often called if-statements. You write them as follows:

if CONDITION:
    do this
elif ANOTER_CONDITION:
    do that
else:
    this should be done

the interpreter goes through each statement until it finds a condition that is true and executes the code in that statement. After that, the interpreter goes past the other elif and else statements. If None of the statementents are true it executes the code in the else part. When using if-statements we need to know some things about relational symbols first. These are <, >, ==, and !. < means less than, > means greater than, == means equal (not the same as = which is assignment) and ! means not.


In [32]:
4 > 3


Out[32]:
True

In [33]:
7 == 1


Out[33]:
False

In [38]:
4 >= 4


Out[38]:
True

In [39]:
4 != 4


Out[39]:
False

As you can see some can be combined to mean greater than or equal and not equal. Try it out and see which combinations work and which don't. Using these we can write some simple conditional statements:


In [40]:
if 3 > 6:
    print "{} is larger than {}".format(3, 6)
elif 3 < 5:
    print "{} is smaller than {}".format(3, 5)
elif 3 != 12:
    print "{} is not equal to {}".format(3, 12)
else:
    print "Tomatopaste"


3 is smaller than 5

Notice that even though $3 \neq 12$, it did not print that statement. It only executed the first thing that held true.