Lecture 7
Lecture 7
ENGG1810/9810
Introduction to Engineering Computing
Week 7: Libraries and Modules I
COMMONWEALTH OF AUSTRALIA
WARNING
The material in this communication may be subject to copyright under the Act.
Any further reproduction or communication of this material by you may be the
subject of copyright protection under the Act.
Week 5: Functions I
Week 6: Functions II Functions
Week 7: Libraries and Modules I
and Packages
Week 9: Application I
module1
a
module2
variables variables
functions functions
.
.
.
Python Modules
To develop a complex program
Python Modules
re matplotlib.animation
*covered in Lecture 4
*covered in Lecture 6
in Lecture 3
ENGG1810/9810
Import everything from module_name module but everything Use keyword as to replace prefixed module_name with
we want must be prefixed with module_name own identifier
ENGG1810/9810
If no as <identifier>
ENGG1810/9810
A module within
the matplotlib library
A library in python
for data visualisation write the name on every call
ENGG1810/9810
Approach 1 Pros:
Less maintenance of your import
import <module_name> statements. Don't need to add any
additional imports to start using
Import everything from module_name module
another item from the module
but everything we want must be prefixed with module_name
in Lecture 6
Approach 2 Pros:
Less typing to use plt
from <module_name> import <attribute>
More control over which items of a
module can be accessed
Imports only the attribute that is specified from module_name
Python Modules
To develop a complex program
Script Module
using
two pale
Python file intended to be run directly Python file meant to be imported
Contains statements outside the scope of into scripts and other modules
any class or function Defines classes, functions, variables,
and other members for us in scripts
that import it
Script Module
Script Module
Script Module
Python Math
There are several common built-in Math functions in Python
Python Math
There are several common built-in Math functions in Python
Python Math
import math
By importing the math module,
you can start using methods and constants of the math module.
Pythagorean theorem
Pythagorean theorem
ENGG1810/9810
2
ENGG1810/9810
Python Numpy
NumPy provides a high-performance multidimensional array object,
and tools for working with these arrays.
A NumPy array is:
a grid of values, all of the same type.
indexed by a tuple of nonnegative integers.
import Numpy as np
5 4 3
5 4 3
5 5 4 3 5 4 3
3 4 2
3 2 4
24 5 3 8 3 3 2 4 3 2 4
10 7 5
10 7 5
10 10 7 5 10 7 5
Scalar Row Vector Column Vector Matrix Tensor
ENGG1810/9810
Scalar
Vector
Matrix
Tensor
ENGG1810/9810
O 0
JC
list concatenation element-wise addition
ENGG1810/9810
1 2 3 4 5 + 1 2 3 4 5 1 2 3 4
+ + + + = 2 4 6 8
= 1 2 3 4 5 1 2 3 4 5 1 2 3 4
ENGG1810/9810
fist aint
a int
ENGG1810/9810
5 5 5
+ + +
1 2 3 6 7 8
=
4 5 6 9 10 11
+ + +
5 5 5
10 10 10
* * *
1 2 3 10 20 30
=
4 5 6 40 50 60
* *
10 10 10
*
ENGG1810/9810
PERFORMANCE
Numpy data structures perform better in:
Size - Numpy data structures take up less space.
Performance - have a need for speed and are faster than lists
Functionality - have optimized functions such as linear algebra operations built in.
ENGG1810/9810
01 2 34 S
O 0 1 2 3 4 5
I 6 7 8 9 10 11
2 12 13 14 15 16 17
18 19 20 21 22 23
3
4 24 25 26 27 28 29
s 30 31 32 33 34 35
ENGG1810/9810
Od goes
numpy.arange
numpy.arange([start, ]stop, [step, ]
Return evenly spaced values within a given interval.
ENGG1810/9810
https://numpy.org/doc/stable/reference/generated/numpy.sin.html?highlight=sin#numpy.sin
https://numpy.org/doc/stable/reference/generated/numpy.cos.html?highlight=cos#numpy.cos
ENGG1810/9810
https://numpy.org/doc/stable/reference/generated/numpy.sin.html?highlight=sin#numpy.sin
https://numpy.org/doc/stable/reference/generated/numpy.cos.html?highlight=cos#numpy.cos
ENGG1810/9810
THANKS FOR
WATCHING
Good luck in studying