0% found this document useful (0 votes)
4 views

Lecture 7

Uploaded by

Rebecca Morris
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lecture 7

Uploaded by

Rebecca Morris
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

ENGG1810/9810

ENGG1810/9810
Introduction to Engineering Computing
Week 7: Libraries and Modules I

Dr. Imdad Ullah


Faculty of Engineering
ENGG1810/9810

COMMONWEALTH OF AUSTRALIA

Copyright Regulations 1969

WARNING

This material has been reproduced and communicated to you by or on behalf of


the University of Sydney pursuant to Part VB of the Copyright Act 1968 (the Act).

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.

Do not remove this notice.


INTRODUCTION
ENGG1810/9810

What will you learn in this course?


Week 1: Introduction to Python

Week 2: Storing Data and Making Decisions Programming


Week 3: Repeating Actions I Basics

Week 4: Repeating Actions II

Week 5: Functions I
Week 6: Functions II Functions
Week 7: Libraries and Modules I
and Packages

Week 8: Libraries and Modules II

Week 9: Application I

Week 10: Application II Advanced


Week 11: Case Study I
Topics

Week 12: Case Study II

Week 13: Revision and Exam Guide


ENGG1810/9810

Libraries and Modules


Python Terminology: Script, Module, Package, and Libraries
Modules
Import Modules
Creating own Module
Math
Built-in Math functions
The Math modules
Numpy: Basics
List vs Array
Vectors and Matrices
Numpy with matplotlib
ENGG1810/9810

Python Libraries, Packages, Modules


matplot
Library peg
Package Package

module1
a
module2

classes classes Module

variables variables

functions functions
.
.
.

Will learn about the class next week


These are not strict definitions. Many people feel these terms are somewhat open to interpretation.
ENGG1810/9810

Libraries and Modules


Python Terminology: Script, Module, Package, and Libraries
Modules
Import Modules
Creating own Module
Math
Built-in Math functions
The Math modules
Numpy: Basics
List vs Array
Vectors and Matrices
Numpy with matplotlib
ENGG1810/9810

Python Modules
To develop a complex program

The code I wrote The code written by others


ENGG1810/9810

Python Modules

We learned so far: Remember?


Variables We sometimes used the code, wrote by others!
Data Collections
Functions
sys matplotlib
*covered in Lecture 3
*covered in Lecture 3, 4, 6

re matplotlib.animation
*covered in Lecture 4
*covered in Lecture 6

We call those as Modules!


ENGG1810/9810

Python Import Modules Approach 1


How did we use this? Remember?

import <module_name> import <module_name> as <identifier>

in Lecture 3
ENGG1810/9810

Python Import Modules Approach 1


How did we use this? Remember?

import <module_name> import <module_name> as <identifier>

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

Python Import Modules Approach 1


How did we use this? Remember?

import <module_name> as <identifier>

What if we have very


long module name?

Use keyword as to replace prefixed module_name with


own identifier

If no as <identifier>
ENGG1810/9810

Python Import Modules Approach 1


Interpret the following command as a human language

A module within
the matplotlib library

import matplotlib.pyplot as plt


bring in

A library in python
for data visualisation write the name on every call
ENGG1810/9810

Python Import Modules Approach 1

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

import <module_name> as <identifier> Cons!


Typing sys.argv in your code can be
Use keyword as to replace prefixed module_name tedious and redundant (tedium can be
with own identifier minimized by using import module as)
ENGG1810/9810

Python Import Modules Approach 2


How did we use this? Remember?

from <module_name> import <attribute>

from <module_name> import <attribute> as <identifier>

in Lecture 6

Imports only the attribute that is specified from module_name


ENGG1810/9810

Python Import Modules Approach 2

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

from <module_name> import <attribute> as <identifier> Cons!


To use a new item from the module you
Use keyword as to replace prefixed module_name have to update your import statement
with own identifier
ENGG1810/9810

Python Import Modules


Technically, you can import modules by using several approaches

import matplotlib.pyplot as plt


plt.plot(x, y)
All those are equivalent
import matplotlib.pyplot (if x and y are already defined).
matplotlib.pyplot.plot(x, y)
What do you like the most?
import matplotlib as mpl
mpl.pyplot.plot(x, y)

from matplotlib import pyplot


pyplot.plot(x, y)

from matplotlib import pyplot as plt


plt.plot(x, y)
ENGG1810/9810

Libraries and Modules


Python Terminology: Script, Module, Package, and Libraries
Modules
Import Modules
Creating own Module
Math
Built-in Math functions
The Math modules
Numpy: Basics
List vs Array
Vectors and Matrices
Numpy with matplotlib
ENGG1810/9810

Python Modules
To develop a complex program

The code I wrote The code written by me before


ENGG1810/9810

Python Script VS Modules

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

Will learn about the class next week


ENGG1810/9810

Python Script VS Modules

Script Module

Python file meant to be imported


into scripts and other modules
Defines classes, functions, variables,
and other members for us in scripts
that import it

Will learn about the class next week


ENGG1810/9810

Python Creating Modules


You can make a module by yourself!

Script Module

from <module_name> import <attribute>

Imports only the attribute that is specified from module_name


ENGG1810/9810

Python Creating Modules


You can make a module by yourself!

Script Module

from <module_name> import <attribute>

Imports only the attribute that is specified from module_name


ENGG1810/9810

Libraries and Modules


Python Terminology: Script, Module, Package, and Libraries
Modules
Import Modules
Creating own Module
Math
Built-in Math functions
The Math modules
Numpy: Basics
List vs Array
Vectors and Matrices
Numpy with matplotlib
ENGG1810/9810

Python Math
There are several common built-in Math functions in Python

min/max abs round


Find the lowest or highest value Returns the absolute (positive) Round number to ndigits
in an iterable: value of the specified number |x| beyond the decimal point.
ENGG1810/9810

Python Math
There are several common built-in Math functions in Python

pow sum int/float


adds the items of an iterable and returns a integer or floating point
returns the sum. number from a number or a string.
ENGG1810/9810

Python Math

Python has also a built-in module called math,


which extends the list of mathematical functions.

To use it, we can use the keyword import module:

import math
By importing the math module,
you can start using methods and constants of the math module.

Complete Math Module Reference


https://docs.python.org/3/library/math.html
ENGG1810/9810

Python Math Module

Calculate the length of the hypotenuse. (a = 3, b= 4)

Pythagorean theorem

** Exponentiation (covered in Lecture 1)


ENGG1810/9810

Python Math Module

Calculate the length of the hypotenuse. (a = 3, b= 4)

math.sqrt() returns the


square root of a number
Pythagorean theorem
ENGG1810/9810

Python Math Module


Make a function hypotenuse()
Calculate the length of the hypotenuse.

Pythagorean theorem
ENGG1810/9810

Python Math Module


The math.ceil() rounds a number upwards to its nearest integer
The math.floor() rounds a number downwards to its nearest integer

2.3 Find the nearest integer

2
ENGG1810/9810

Python Math Module


The math.pi
ENGG1810/9810

Libraries and Modules


Python Terminology: Script, Module, Package, and Libraries
Modules
Import Modules
Creating own Module
Math
Built-in Math functions
The Math modules
Numpy: Basics
List vs Array
Vectors and Matrices
Numpy with matplotlib
ENGG1810/9810

The core library for scientific computing

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.

To use it, we can use the keyword import module:

import Numpy as np

write the name on every call

Numpy Module Reference


https://numpy.org/doc/stable/reference/
ENGG1810/9810

Python Vectors and Matrices

Any single numerical value is known as Scalar.


1-dimensional arrays are known as Vectors.
2-dimensional arrays are known as Matrices.
N-dimensional arrays are known as Tensor. (N > 2)

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

Python Vectors and Matrices

Any single numerical value is known as Scalar.


1-dimensional arrays are known as Vectors.
2-dimensional arrays are known as Matrices.
N-dimensional arrays are known as Tensor. (N > 2)

Scalar
Vector
Matrix
Tensor
ENGG1810/9810

Python List VS Numpy Array: Operations


The operator on NumPy arrays perform an element-wise addition,
while the same operation on Python lists results in a list concatenation.

Python List Numpy Array

O 0

JC
list concatenation element-wise addition
ENGG1810/9810

Python List VS Numpy Array: Operations


The operator on NumPy arrays perform an element-wise addition,
while the same operation on Python lists results in a list concatenation.

Python List Numpy Array

list concatenation element-wise addition

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

Python List VS Numpy Array: Operations


The operators on NumPy arrays perform an element-wise calculation,
while the same operation on Python lists results in a list concatenation.

Python List Numpy Array

fist aint
a int
ENGG1810/9810

Python List VS Numpy Array: Operations


The operators on NumPy arrays perform an element-wise calculation,
while the same operation on Python lists results in a list concatenation.

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

Python List VS Numpy Array

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

Python Numpy Array: Indexing


Numpy Array!

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

Python Numpy Array: Draw Sin/Cos Graph


ENGG1810/9810

Python Numpy Array: Draw Sin/Cos Graph

Od goes

numpy.arange
numpy.arange([start, ]stop, [step, ]
Return evenly spaced values within a given interval.
ENGG1810/9810

Python Numpy Array: Draw Sin/Cos Graph

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

Python Numpy Array: Draw Sin/Cos Graph

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

You might also like