# Python: An Introduction to Scientific Programming

## PDF Book

I have been putting a pdf book together which is geared towards beginners learning the basics of python (as I have been doing over the last year). However the section covering the basics of core python, the numpy library and the pandas libray are mainly complete and it is important beginners get a strong grounding here before proceeding.

This pdf is incomplete and should be considered "a work in progress" however I would of course welcome any comments highlighting typos.

## Anaconda Installation on Windows and Linux

In these set of guides we will use the open-source Anaconda which is a Python distribution compiled for data scientists. It has the Spyder 4 graphical user interface inbuilt as well as the numerical numpy, the fundamental package for scientific computing and matplotlib which is required for plotting. These can be used as an Open Source alternative to replace much of the functionality of the commercial software MATLAB.

Most of the written guides below should be considered drafts and I plan to incorporate them into the pdf above. Please leave any comments if you find any typos in the pdf as it hasn't been proof read.

## Python Basics: Using the IPython Console As a Basic Calculator

In this guide we look at some of the fundamentals of core python such as datatypes. These are numerical – integers (whole numbers) and floats (numbers with a decimal point), strings (text input) and Boolean (logical – true or false). We also look at the assignment of variables and the use of elementary mathematics between scalars. Some basic functions are also introduced.

## Python Basics: Scripts

Earlier we typed in all code in the IPython Console. Now we will have a look at using a script, with comments and how to print commands to the console.

## Python Basics: 0 Order Indexing

Python uses 0 order indexing. When starting with Python (particularly if one is used to MATLAB). we need to spend a moment relearn how to count to get used to zero order indexing.

## Python Basics: Dictionaries

Creating a dictionary and indexing from it. In this example we will create a dictionary of a custom colour palette and use it to print out coloured text in the console.

## Python Basics: Scalars, Vectors and Matrices

In this guide we look further at some of the fundamentals of core python and examine numerical data in the form of a list. This list can be a scalar, vector, matrix, book or more complicated multi-dimensional array. We discuss some of the basics behind indexing and examining the dimensions of an array.

## Python Basics: Lists, Tuples and Sets

Here we look at the concept of mutability (changeability) and discuss that sometimes this is not desirable. In such cases we create a tuple opposed to a list. We also have a look at sets.

## Numerical Python: NumPy (Basics)

This section looks at using the Numerical Python NumPy to create vectors and matrices of data. It looks at indexing of rows or columns from such data, which is of course a perquisite when it comes to plotting. NumPy arrays for working with time (dates and duration) are also discussed.

## Numerical Python: Mathematical Operations

This section looks at using fundamental mathematical operations when working with scalars, vectors and matrices. It looks at element by element operations where each element in the array is treated individually for example in the operations of addition, subtraction, element by element division and element by element multiplication. It then moves on to look at the fundamentals behind array multiplication and array division. Finally it moves onto look at the use of a number of NumPy basic statistical functions which can be used with scalars, vectors and matrices.

## Pandas: Introduction to DataFrames

Pandas is used to create dataframes within Python which are like tables in Microsoft Excel. There are a number of advantages to creating a dataframe opposed to using a multi-column variable when it comes to sorting and organising data. We discuss how to import data from excel to a dataframe.

## MatPlotLib: Plotting

This section is all about plotting data, we revise some of the basics such as indexing a column within a variable, or selecting a column with a column. We start with a Histogram plot, the progress to a bar graph, piechart, line plot and scatter plot. Next we look at saving plots and axes to variables and how doing so allows us to modify a figure that has already been plotted. This gives us some of the perquisites to move onto 3D plotting, where we look at 2D contour plots and 3D contour plots, surface plots and wireframe plots. We also discuss the meshgrid function and discuss how we encode colour.

## MatPlotLib: Interpolation

In this section we look at a practical example. Here we practice linear algebra of matrices to interpolate a single point in a curve. We then see how we interpolate a series of points from a data curve using interpolation functions. This also reinforces some of the basics when it comes to plotting.

## MatPlotLib: Curve Fitting

In this section we look at curve fitting using the polyfit and polyval functions. Again this gives practice for plotting and here we also introduce subplotting.

## MatPlotLib: Trigonometry

Here we practice plotting using right angle triangles. We then plot a series of normalised right angle triangles on a circle to obtain the ratio of sides with respect to the angle. Then use rules of symmetry to obtain equally spaced values around the entire circle. This demonstrates the properties of the sin, cos and tan waveforms.

## Python Intermediate: If (if), Else If (elif) and Else (else)

Here we discuss how to only run code only if a certain condition is met. We can also specify what code to run if the condition is not met and we can also express multiple conditions.

## Python Intermediate: Creating a Custom Function

Here we will discuss how to define a custom function, how to load the custom function and how to run the custom function. We will discuss also the assignment of function input arguments and the creation of function output arguments.

## Python Intermediate: For Loop

In this section we look at using a for loop to automate some repetitive tasks. We see how we can use a for loop to perform the same calculations for a series of lines in a variable or dataframe. We also touch upon using For Loops for plotting.

## Python Intermediate: While Loop

In this section we compare a while loop to a for loop and then move on to combining a for loop with if, else if and else statements to create a homeostasic system.

## MatPlotLib: Integration and Differentiation

Here we visualise how we perform integration, employing indexing, plotting techniques and for loops.

## Creating Animated Plots

Creating an animated line and scatter plot manually, using a for loop and using the custom functions init and animate. init and animate can be used with FuncAnimate to call the custom functions and save to an animated gif or a video file

## Python, NumPy and MatPlotLib: Image Processing Fundamentals

An image as discussed earlier is a 3D array with the 0th page being the intensity of the primary colour red for each pixel, the 1st page being the intensity of the primary colour green for each pixel, the 2nd page being the intensity of the primary colour blue and the 3rd page being the alpha or transparency. All other colours as discussed before are made up using different combinations of red, green and blue. We will look at importing a .png file, deconstructing this 3D array and performing some very basic photo editing by amending the numeric values in this 3D array.

## Not Complete Yet

Use a for loop to save a series of frames of a figure while it rotates, see if there is a way to make a mp4 of it like in MATLAB

Use if, else, elseif to find the 2nd max, 3rd max, 4th max and their location etc etc do this under a for loop

Time/Date under Pandas

covariance after plotting?

## 2 thoughts on “Python: An Introduction to Scientific Programming”

1. Bubba Yarfkowitz says: