W1 - Introduction to Numerical Programming - Course Notes
W1 - Introduction to Numerical Programming - Course Notes
Week 1 Overview
In week 1, we will be getting comfortable with the different programming environments that we
will use this semester, and learning many different aspects of basic Python “syntax” – the
grammar rules of how to write commands. We will also introduce the package “Numpy” and its
basic capabilities in working with matrices.
1
ENG1014 Engineering Numerical Analysis Course Notes
• The course notes are the “guide” that will tell you what is included as part of the course.
Now you’re at university, you shouldn’t limit yourself to just learning what is in your units;
if there are topics that you find interesting or that you think will be important, you should
try to learn these as well for your own benefit. The course notes however give you the
limits on what we will examine you on.
• Within the notes, there will be signposts to other resources, such as examples, practice
questions and quiz sections. There may also be clickable links to external resources like
videos or online resources.
• We do not recommend that you just read these notes from start to finish. You should
experiment to work out what study routine works best for you. You may wish to start with
the examples and practice questions in the Jupyter notebooks, or you may skim read the
notes first, make summary notes, etc. There are many possibilities. “Study” means “play
with the material in a variety of ways until you understand it”, not just “read the assigned
document”. You don’t need to do everything; we provide a range of resources and it’s up
to you to choose which of them are useful for you.
• There are often many ways to solve problems with code, particularly with python – it is a
very flexible language. We have made various choices about what to include in the notes,
and what to leave out. We acknowledge that there will be different points of view on what
is important, and other courses will teach things in different ways. If you want to use
other study methods such as the many online courses for learning basic python, that’s
good – they are good resources and university study should encourage exploration. But
we may not always be able to provide support, and in some cases we may require you to
use (or not use) particular methods for your assessments. Some choices we’ve made
are:
o We do not teach any details of object orientation.
o We try to avoid where possible using several types of variables; we don’t teach
tuples, dictionaries or sets, and in most cases we will use numpy arrays rather
than lists. We have avoided any introduction to the “pandas” package and
dataframes.
2
ENG1014 Engineering Numerical Analysis Course Notes
Related Resources
Beyond the course notes document, there are other resources to help your study.
• Course notes examples notebook: these are simply pieces of code that are written to
show you how a particular concept works. For week 1, this is a file called “W1 -
Introduction to Numerical Programming - Course Notes Examples.ipynb”, which can be
opened using JupyterLab.
• Course notes practice questions notebook: this is a similar filetype to the examples,
but they are interactive – you will have to fill in small sections of code to make the code
work correctly. For week 1, this is a file called “W1 - Introduction to Numerical
Programming - Course Notes Practice Questions.ipynb”, which can also be opened
using JupyterLab.
• Weekly quiz: there is a quiz to help you practice each week. Each quiz is worth 0.5% of
your semester mark, so each individual question is only worth a tiny amount. You have
unlimited time to complete the quiz and can close it and come back to it later if you wish
(as long as you don’t “submit”). You can immediately check whether your answer to an
individual question is correct, and often there will be feedback to help you.
• Python resources: a list of useful documents and websites
Examples of signposts used in the notes to help you navigate between the resources:
3
ENG1014 Engineering Numerical Analysis Course Notes
4
ENG1014 Engineering Numerical Analysis Course Notes
Concept Definition
Syntax The set of “grammar rules” of a programming language that tells a computer
how to interpret code and tell us how to write code so that the computer
understands it correctly.
5
ENG1014 Engineering Numerical Analysis Course Notes
1.2. Variables
1.2.1. What are variables?
Variables are containers for storing data values.
You have used variables in algebra. For example:
The Lecturer's age is twice the age of the Student.
𝐿 = 2 × 𝑆
𝐿 and 𝑆 would be considered variables.
Variables are used all throughout programming.
6
ENG1014 Engineering Numerical Analysis Course Notes
Complete Practice Questions 1-5 from the W1 - Introduction to Programming Course Notes
Practice Questions
7
ENG1014 Engineering Numerical Analysis Course Notes
1.3. Operators
Operators are used to perform operations on variables and values. There are several groups of
operators; the operators in green you will become familiar with in later weeks, the operators in
grey will not be covered in ENG1014.
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
+ Addition 1+2 1 + 2 3
- Subtraction 5–1 5 – 1 4
* Multiplication 6×7 6 * 7 42
/ Division 33 ÷ 7 33 / 7 4.714
% Remainder 33 mod 7 33 % 7 5
** Exponentiation 24 2 ** 4 16
8
ENG1014 Engineering Numerical Analysis Course Notes
= x = 5 x = 5
+= x += 3 x = x + 3
-= x -= 3 x = x – 3
*= x *= 3 x = x * 3
/= x /= 3 x = x / 3
%= x %= 3 x = x % 3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
9
ENG1014 Engineering Numerical Analysis Course Notes
10
ENG1014 Engineering Numerical Analysis Course Notes
11
ENG1014 Engineering Numerical Analysis Course Notes
Useful trick: if you want to stop a line of code from running but think you might want to use
that line of code later, you can “comment out” the code.
Function Description
type(<data>) Get the data type of any variable or data value (refer to 1.4. Python
Data Types).
12
ENG1014 Engineering Numerical Analysis Course Notes
input(<message>) Get input from a user. An optional message can also be printed. The
input() function always returns a string, so if you want a number to
be inputted, it will need to be converted to a number.
help(<function>) Python is a huge language, and chances are good that you won't
remember every detail of every command you will ever use.
Fortunately, Python has good documentation of its various
functions which you can quickly use to find the details you need.
If you know the name of the built-in function, you can display the
documentation of said function using the help() function.
We will learn more about Python documentation in Week 3.
Once the module has been imported, its functions and variables can be called by using a dot
operator (.) along with the module name.
Syntax:
import <module>
<module>.<function>
<module>.<variable>
1.7.1. Module aliases
The module being imported can also be renamed using the as keyword. Modules are often
imported under an alias when the module name is too large to use repeatedly.
Syntax:
import <module> as <module alias>
<module alias>.<function>
<module alias>.<variable>
It is common practice in the Python community to abbreviate certain modules with particular
aliases. When we introduce these modules, we will indicate the usual aliases the Python
community uses.
13
ENG1014 Engineering Numerical Analysis Course Notes
Method 1: You can choose to import the top-level package, since submodules are imported if
you import the top-level package.
Syntax:
import <package> as <A>
<A>.<B>.<function>
Method 2: You can choose to only import the submodule. Often you don't need all the
functionality in a submodule, so this can reduce clutter in your code. This also allows you to
define an alias specifically for the submodule.
Syntax:
import <package>.<submodule> as <B>
<B>.<function>
As shown in Figure 1.1, packages can contain submodules, functions, and variables. Figure 1.2
shows the structure of the Python package NumPy (refer to 1.8. What is NumPy?) with a couple
of example submodules, functions, and variables. Essentially, the dot operator means going
inside something (package, module, submodule), to get some attribute of it (submodule,
function, variables). On Figure 1.1 and Figure 1.2, the dot operator is represented by the arrows.
14
ENG1014 Engineering Numerical Analysis Course Notes
We will learn more about modules and packages in Week 3. For now, you just need to know how
to import them and how to use the dot operator to call the functions and variables defined in
modules and packages.
15
ENG1014 Engineering Numerical Analysis Course Notes
Figure 1.3 - Examples of different matrix structures. In traditional mathematics, B is a 1×4 row
vector, C is a 4×1 column vector, and D is a 3×4 matrix.
NumPy (Numerical Python) is a Python package used for working with matrices.
In NumPy, we refer to a matrix as an "array" or "ndarray". In ENG1014, we will use the terms
"array" and “ndarray” interchangeably. The term "ndarray" is short for "N-dimensional array", as
NumPy allows for arrays with any number of dimensions. In ENG1014, we will mainly use 1D
and 2D arrays.
Unlike traditional mathematics, NumPy treats 1D arrays differently to 2D arrays. As shown in
Figure 1.4, in NumPy, 1D arrays can be visualised like a list, while 2D arrays can be visualised
like a table.
1D array 2D array
16
ENG1014 Engineering Numerical Analysis Course Notes
While in traditional mathematics row vectors and column vectors are often treated differently,
1D arrays in NumPy do not differentiate between them. Visually, they will always be printed as
a horizontal list.
We will continue to explore the differences between 1D and 2D arrays in NumPy throughout the
rest of the course notes. Note that when we use the terms "array" or "ndarray", we are referring
to both 1D and 2D arrays, and we will specify when we are referring to just one or the other.
It is important to note that arrays can only contain one data type.
[0,0,0,0] np.zeros()
[1,100000,10000000000] np.logspace()
[0.0231,0.1243,0.4824,0.8321] np.random.rand()
[0,7,4,8,4] np.random.randint()
17
ENG1014 Engineering Numerical Analysis Course Notes
18
ENG1014 Engineering Numerical Analysis Course Notes
19
ENG1014 Engineering Numerical Analysis Course Notes
Complete Practice Questions 7-9 from the W1 - Introduction to Programming Course Notes
Practice Questions
Refer to Examples 6 & 7 from the W1 – Introduction to Python – Course Notes Examples
20
ENG1014 Engineering Numerical Analysis Course Notes
21
ENG1014 Engineering Numerical Analysis Course Notes
22
ENG1014 Engineering Numerical Analysis Course Notes
é a b ù é e f ù é a+ e b+ f ù
ê ú+ê ú=ê ú
ë c d û êë g h úû êë c + g d + h úû
é a b ù é e f ù é ae + bg af + bh ù
ê ú´ê ú=ê ú
ë c d û êë g h úû êë ce + dg cf + dh úû
Figure 1.6 – Basic matrix operations
However, often we may have data that is stored in matrices, where we want perform
multiplication, division or exponentiation on corresponding elements between matrices
instead.
23
ENG1014 Engineering Numerical Analysis Course Notes
For example:
Item Sale price per item Number of items sold Total income per item
A $1000 12 ?
B $5 52 ?
C $72 48 ?
D $1750 24 ?
Command Output
print(arr1 - arr2) [9 9 9 9 9]
print(arr1 % 2) [0 1 0 1 0]
print(arr1 % arr2) [0 1 0 1 4]
print(arr1 // 2) [5 5 6 6 7]
24
ENG1014 Engineering Numerical Analysis Course Notes
25
ENG1014 Engineering Numerical Analysis Course Notes
Constant Value
np.pi π (~ 3.1416)
Function Description
np.round(value, decimals) evenly rounds the value to the given number of decimals
using “banker’s rounding” 1
1
https://medium.com/@nibasnazeem/did-you-know-this-about-the-python-round-function-9323412abf4
26
ENG1014 Engineering Numerical Analysis Course Notes
Function Description
27