1. Introduction
1.1 Review of Programming
We will mostly make use of the python
programming language in this introduction and for much of the course (but not always as we will also look at Mathematica
).
Since a basic course in programming is a pre-requisite for this course, some level of familiarity with programming is assumed.
The first step is to make sure that you have the proper environment setup on your computer. Make sure that you have python 3
installed; also install the numpy
package. One way to make sure that these two packages are installed is to install the Anaconda Python Distribution
. Alternatively, for quick running of code on the web, you could make use of Google Colab, which will open a notebook interface in which you can run python code. Google Colab uses a modified versin of Jupyter Notebook; you can try to install/run jupyter notebook on your computer if you find the notebook interface useful. Alternatively,
Once you have the proper programming environment setup, let's begin with the following basic tasks that most programming languages can do - output, loop and condition.
1.1.1 Printing Output
- import the numerical library
numpy
for mathematical operations np.log10(10)
calculates the base-10 logarithm whereasnp.log(10)
calculates the natural logarithm of 10.print
function is used to print the two outputs to the screen.
You should expect two numbers as the output: one for log base 10 i.e. \(\log_{\rm 10}\) and the other for natural logarithm i.e. \(\ln(10)\). For the above code to run, you may save the code in a file with name such as filename.py
and on the command line enter:
numpy
library installed, you can install using
1.1.2 The for
loop
Let us look at one more example python
code to illustrate the use of variables and for
loop.
total = 0 # (1)
for i in range(1, 101): # (2)
total = total + i # (3)
print (total) # (4)
- variable
total
is initially assigned a value of zero - note the natural language-like syntax for
for
loop in python; also, note thatrange(1, 101)
excludes101
. - note that indentation of code is important in python
- the
total
variable is printed on the screen once the program exits thefor
loop after adding 1 to 100.
1.1.3 The if
condition
Consider the following python
code that checks whether an integer is odd or even.
In the code above we have also used def
to define a new function. Inside the def
function block, we have used three double-quote """
format for docstring.
1.1.4 The while
loop
The while
loop runs as long as the condition given in its syntax is satisfied. The while
loop can be used instead of the for
loop to get the sum of integers; see example below:
Fibonacci numbers
Write a python code to print out Fibonacci numbers upto a certain value say all the Fibonacci numbers less than 10000. The first two Fibonacci numbers are 1,1 and the rule for Fibonacci sequence is that the next number is obtained by a sum of previous two numbers.
1.2 Lists and Arrays
1.2.1 In-built Lists in Python
A list can contain different types of elements. As you have seen in the example above, List3
consists of both integers and an string. Elements can also be added or removed from a list i.e. a list can grow or shrink during the execution of a program.
1.2.2 Arrays
The array
module in python supports an object type called Array
that can do more mathematical operations than the in-built python lists. However, the numpy
module has support for a similar object array
with support for more mathematical operations (including vector and matrix operations). Therefore, we will look at the numpy.array
object:
import numpy as np
# first a simple 1D array
arr1 = np.array([1.0, 2.0, 3.0])
print (arr1)
# a simple 2D array
arr2 = np.array([[1.0, 2.0, 3.0], [1.5, 2.5, 3.5]])
print (arr2)
# to access the element of an array we can use indexing as follows:
print (arr2[0, 0]) # outputs 1.0 (the first row, first column)
print (arr2[0, 1]) # outputs 2.0 (the first row, second column)
print (arr1[1]) # outputs 2.0 (the second element of a 1D array)
Now, let us make use of the numpy.array
and numpy.sum
to calculate the sum of integers from 1 to 100.
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
91, 92, 93, 94, 95, 96, 97, 98, 99, 100])
5050
In the example above, instead of arr2 = np.array([i for i in range(0, 101)])
we could have used arr2 = np.array(range(0, 101))
or simply arr2 = range(0, 101)
and still obtained the desired result of 5050
. However, the way the syntax is written in the example is more powerful than these substitutes. For example, you can obtain the sum of squares of integers from 1 to 100 \(\left(\sum_{i=1}^{100} i^2\right)\) by the following code:
1.2.3 Numpy arrays as vectors and matrices
A numpy array of one dimension can be thought of as a vector, and numpy supports vector operations such as dot product.
1.2.4 Initializing a multidimensional array
Sometimes it is useful to initialize a multidimensional array -- for example a \(N\times N\) matrix with all entries as zeros. Such an array can be updated later in the code with appropriate values for each element as needed.
The numpy.zeros(shape)
function can be used to generate arrays of given shape filled with zeros.
Multidimensional arrays can be generated by giving the shape -- for example a \(m \times n\) matrix of zeros can be created using np.zeros((m,n))
Similar to np.zeros
, there is also np.ones
that fills the array with ones.
1.3 Version Control
Computer codes (and even documents) may require updating at times. At the early stages of developing a computer code, the frequency of updates may be very high. Even for a matured piece of code, there may be maintenance updates that will require you to update the code time and again. Version control tools help us with this process. They can also be useful if we decide to revert a change at a later time and in case that multiple people are collaborating on the same piece of software or document at once.
We will make use of github
for version control. The underlying version control software utilized by github
is called git
.
Please take the following github skills
course to begin.
1.4 Documentation
The code you write will likely be read by other people that may include friends, collegues, professors, customors and users. Therefore, good documentation of code is very important and perhaps even more so for large projects. Large projects typically also follow a certain style guide so that the code and documentation written by numerous contributors have consistent style.
For example, there is PEP 8 Style Guide for Python Code and PEP 257 Docstring Conventions that are generally followed.
Another popular style guide for python is the Google Python Style Guide.
In this course, we will not be worrying about strictly following a style guide, but it is good to know that these exist and that you can make use of one if necessary.