A BEGINNER’S GUIDE TO PYTHON PROGRAMMING

WHY PYTHON?

Python is one of the most popular programming languages available, others include MatLab, Javascript, etc. It is used by a large number of working and student chemical engineers, due to its versatility, flexibility and easy integration with most software you can think of.

Python provides student and working engineers with the ability to automate repetitive tasks, generating a faster data processing time in comparison to other software such as Microsoft Excel. Picture this; you are a chemical engineering student who has just spent their entire day running 10 experiments in the lab and you are clearly tired, but you have just about enough energy to process your 10 lab results. Would you prefer to use Excel to process each result, knowing that each result will take about 10 minutes to process (a total of 100 minutes or 1 hour 40 minutes !!!) OR would you rather spend 30 minutes writing up some fancy code on python that processes all your 10 lab results in less 5 minutes?

The scenario above does not in anyway take away the benefits of Microsoft Excel; all software have their own benefits. Learning to use more than one software makes you a cooler engineer/scientist πŸ˜‰.

BENEFITS OF CODING

As a student/ working engineer, having the ability to code provides a lot of advantages

  • It help you work efficiently and smarter; reducing time spent performing mundane/repititive tasks.
  • It allows you to handle extremely large datasets.
  • It improves data visualisation and result presentation.
  • It improves your employability; improving your skillset and career opportunities.

CHEMICAL ENGINEERING AND PYTHON

Due to its flexibility, robustness and ease-of-use, Python serves a power tool in the science and engineering space, especially in chemical engineering. it provides the avenue to simulate and/or model chemical reaction systems, flow of materials in a pipe, heat transfer process and many more. Furthermore, it allows the optimisation of the process parameters to improve production quality, production rate and reduce cost: a statement you will hear a lot when you you take up a chemical engineering degree!.

PYTHON DOWNLOAD

Before you get into some cool python stuff, you need to download the Spyder application which will let us use Python.

For both windows and mac OS, click the link below to download Spyder, then follow the download resource for either Windows or mac OS below, to setup the Spyder application.

https://docs.spyder-ide.org/current/installation.html

LETS GET STARTED!

The video below is tailored to get you to speed with some core functions within the Spyder application to help you use it.

PYTHON TUTORIALS

BASIC MATHEMATICAL OPERATORS ON PYTHON

“+” addition

“-” subtraction

“/” division

“*” multiplication

“**” ….to the power of… i.e. 2^2 is written as 2**2

“=” to assign a value to a variable

BASIC PYTHON KNOWLEDGE:

VARIABLES

A variable simply defined as an empty box that can store data which can be easily change, the data can be either a single numerical value, a single word or a single character or a combination of them. There are different type of variables, for example: 02476669632

a = 1, here “a” is a variable that contains a single value or number.

a = [1,2], here “a” contain more than one numerical data, hence it is know as a numerical array. As long as an array contains more than one numerical value and only numerical values, it is a numerical array i.e. b = [2,3,5,6,7] and c = [1,1,1,1,1,1,1,1,1,1,1]; b and c are both numerical arrays.

a = hello, here “a” is a variable that contains a single word/ non-numerical value. This type of variable is know as a string.

a = [hello, tobi, matt, lois, sam] is an array that contains words only. it is therefore a string array.

see video below on how to make variables in python.

HOW TO MAKE VARIABLES

PYTHON LIBRARIES

A python library can be described as a dictionary that contains a list of words, these words allow you to perform a specific task/function during coding. Some common python libraries include NumPy, Pandas and Matplotlib, amongst many others. Before we look into how this libraries work, watch the video below to see how we introduce the common libraries mentioned into our python worksheet.

Now let us look at some well known python libraries, these libraries are already part of the Spyder application package you downloaded

  • NumPy: NumPy stands for Numerical Python, this library allows you to perform a vast range of mathematical operation on only mathematical data/array of any size. This ranges from simple tasks like addition or multiplication to more complex operations such as arranging the data in an array in ascending or descending order, calculating the mean, mode and standard deviation standard deviation in an array of numbers. The video below shows you how to introduce Numpy into your python work sheet and some simple function you can perform using it.
  • Matplotlib: is a library that allows the its user to plot graphs and perform data visualisation graphs. This can range from simple x,y graphs (2 dimensional), to x,y,z graph (3 dimensional) and to animated and interactive graphs. See video below for some examples.

The video below shows how to add the python libraries explained above.

PYTHON PRACTICE AND TIPS:

Now that you have some fundamental understanding of how python work, let’s try to piece together all you have learnt and apply it to an example. Here, you will learn the following:

  • How to set up and save a python worksheet
  • Good practices when coding
  • Applying your python knowledge to simple data analysis

PRACTICE EXERCISE

Newton’s second law of motion:

“The net force acting on an object is equal to the mass of the object multiplied by acceleration”

Force = mass x acceleration

F = m a

Acceleration (a) is the measure of how quickly a body changes velocity with time(t), from its initial velocity (u) to its final velocity(v)

 a = \frac{final\,\,velocity \,\,\,\, - \,\,\,\,initial\,\,velocity }{time}\,\,= \,\ \frac{v\,\,\,\ - \,\,\,\ u}{t}

F = m a = m\, \frac{v\,\,\,\ - \,\,\,\ u}{t}

Velocity is speed in a given direction; has magnitude and direction. Hence a vector quantity. This makes acceleration is a vector quantity and thus making Force a vector quantity as well.

Newton’s second law applies to many scenarios in our daily life. For example, when shopping; the velocity(speed and direction) at which your shopping cart moves is dictated by how much force you push the cart. Similarly, when a footballer strikes the ball, the how fast the ball moves depends on the mass of the ball and the striking power (force applied by the footballer’s foot).

Using the concept of Newton’s 2nd Law above, alongside the tutorial video below the questions, attempt the following questions. The tutorial aims at helping you approach real life scenario questions from a coding perspective using Python. In addition this tutorial will show you how to implement some the function in the Numpy and Matplotlib library.

PRACTICE QUESTION:

Consider 5 balls of masses (m) 0.1, 0.01, 0.2, 0.3 and 0.35 kg respectively. A player kicks all 5 balls, one after the other. The force (F) applied by the player’s foot generates an acceleration (a) of 20 m/s2 on each of the ball.

  • Calculate the force (F) each ball experiences.
  • Knowing that the all 5 balls have the same acceleration, help the player calculate the velocity of each ball after 4 seconds. Hint: all 5 balls are initially at rest.
  • plot a scatter plot of the ball masses (m) and the force (F) they experience.

PRACTICE QUESTION TUTORIAL VIDEO

PYTHON WORKSHEET

Now that you have some basic understanding of coding, have a go at the worksheet below:

Simple Python modelling video

Tip for worksheet

  • Get into the habit of search for things online to see how it is done and try to understand and replicate it.
  • Its okay if it takes you a while to set up the correct code.
  • Have fun with it.
  • Learn from your mistakes.
  • There is more than one method to getting the right answer

In your own time, attempt to solve the exercises in the EngBAM python worksheet, but this time using Microsoft Excel; what are you personal observations about Python and Excel.

Below are video solutions to the the EngBAM python worksheet.

WORKSHEET SOLUTION – QUESTION 1

 

WORKSHEET SOLUTION – QUESTION 2

 

WORKSHEET SOLUTION – QUESTION 3

 

WORKSEET SOLUTION – QUESTION 4

 

WORKSHEET SOLUTION – QUESTION 5

 

WORKSHEET SOLUTION – EXTENSION QUESTION 1

 

WORKSHEET SOLUTION – EXTENSION QUESTION 2 & 3

 

WORKSHEET SOLUTION – EXTENSION QUESTION 4

 

WORKSHEET SOLUTION – EXTENSION QUESTION 5

 

WORKSHEET SOLUTION – EXTENSION QUESTION 6

 

WORKSHEET SOLUTION – EXTENSION QUESTION 7