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

Lecture 6

Uploaded by

Rebecca Morris
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lecture 6

Uploaded by

Rebecca Morris
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

ENGG1810/9810

ENGG1810/9810
Introduction to Engineering Computing
Week 6: Functions II

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

Name Part Para2


def funt
I
With Functions St N
Matplotlib: Basic Plotting with Functions
Matplotlib: Times Series Line Graph with Functions

glsus2
Errors and Exception Handling
Name a
Errors Types funtion y
Exception Handling
Try Except
Many Exceptions
Else and Finally
Raising Exceptions (with Functions)
ENGG1810/9810

Python Plotting
We know how to draw the bar chart!

É
ENGG1810/9810 Students

BTS_V Emma Spongebob

Dictionary (based on their lab test 1 mark!)


ENGG1810/9810

Python Plotting
Bar chart Scatter plot Line graph
ENGG1810/9810

Python Plotting (with Function)

Function Signature

Function call
ENGG1810/9810

With Functions
Matplotlib: Basic Plotting with Functions
Matplotlib: Times Series Line Graph with Functions
Errors and Exception Handling
Errors Types
Exception Handling
Try Except
Many Exceptions
Else and Finally
Raising Exceptions (with Functions)
ENGG1810/9810

Python Plot Animation


The following is the dataset that shows the number of Air Passengers (1949 - 1960)

# of months, # of passengers

mmmm
ENGG1810/9810

Python Plot Animation


The following is the dataset that shows the number of Air Passengers (1949 - 1960)

# of months, # of passengers
animation version of matplot
asaxis
Makes an animation by
navies repeatedly calling a function func.
https://matplotlib.org/stable/api/_as_gen/matplotlib.animatio
n.FuncAnimation.html#matplotlib.animation.FuncAnimation
ENGG1810/9810

Python Plot Animation


The following is the dataset that shows the number of Air Passengers (1949 - 1960)

# of months, # of passengers
Makes an animation by
repeatedly calling a function func.
https://matplotlib.org/stable/api/_as_gen/matplotlib.animatio
The text file contains: n.FuncAnimation.html#matplotlib.animation.FuncAnimation

0,112\n1,118\

You can check how it looks


like by printing it

str.split, by default, splits by all the whitespace characters,


\n or \t
ENGG1810/9810

Python Plot Animation


The following is the dataset that shows the number of Air Passengers (1949 - 1960)

# of months, # of passengers

Will use this for appending the values


A for x-points and y-points

..
. before comma (,) after comma (,)

a
ay
You can check how it looks
like by printing it
ENGG1810/9810

Python Plot Animation


The following is the dataset that shows the number of Air Passengers (1949 - 1960)
Now, we got all the x points and y points value
# of months, # of passengers

return the figure return the axes (Axes object


layout or an array of Axes objects)

o su
setting
limits nz us
ENGG1810/9810

Python Plot Animation


The following is the dataset that shows the number of Air Passengers (1949 - 1960)
Now, we got all the x points and y points value
# of months, # of passengers

return the figure return the axes (Axes object


layout or an array of Axes objects)
ENGG1810/9810

Python Plot Animation


The following is the dataset that shows the number of Air Passengers (1949 - 1960)
Now, we got all the x points and y points value
# of months, # of passengers

return the figure return the axes (Axes object


layout or an array of Axes objects)

wash
quietest
rforanimation
ENGG1810/9810

Python Plot Animation


The following is the dataset that shows the number of Air Passengers (1949 - 1960)
Now, we got all the x points and y points value
# of months, # of passengers

will fig: The figure object used to get needed events, such as draw or resize.
draw func (animate): The function to call at each frame.

Egitto
init_func: A function used to draw a clear frame
frames: iterable, int, generator function
Interval: Delay between frames in milliseconds.
ENGG1810/9810

Python Plot Animation


The following is the dataset that shows the number of Air Passengers (1949 - 1960)
Now, we got all the x points and y points value
# of months, # of passengers

qname

https://matplotlib.org/stable/api/_as_gen/matplotlib.animation.Animatio
n.html#matplotlib.animation.Animation.save
fps: Movie frame rate (per second).
init_func: A function used to draw a clear frame
extra_args: Extra command-line arguments passed to the underlying movie encoder
ENGG1810/9810

With Functions
Matplotlib: Basic Plotting with Functions
Matplotlib: Times Series Line Graph with Functions
Errors and Exception Handling
Errors Types
Exception Handling
Try Except
Many Exceptions
Else and Finally
Raising Exceptions (with Functions)
ENGG1810/9810

Python Exceptions (Recap)


Covered in Lecture 4

Remember? how to handle the error if the file does not exist.
i.e. File is not found in the directory

If there is no file test.txt If there is no file test.txt

throw

catch
ENGG1810/9810

Python Exceptions (Recap)


Covered in Lecture 4

Remember? how to handle the error if the file does not exist.
i.e. File is not found in the directory

Try to do what we intend to do try:


<statements we intend to do>
except <ExceptionName>:
<statements for handling errors>

If it fails or something goes wrong,


program executes codes in except block

NOTE: If nothing goes wrong, the execute block does not run and
program moves to the first line after the try-except block.
ENGG1810/9810

With Functions
Matplotlib: Basic Plotting with Functions
Matplotlib: Times Series Line Graph with Functions
Errors and Exception Handling
Errors Types
Exception Handling
Try Except
Many Exceptions
Else and Finally
Raising Exceptions (with Functions)
ENGG1810/9810

Python Error Types


Errors or mistakes in a program are often referred to as bugs.
Errors can be classified into three major groups:
qpr.net

01 Syntax Error 02 Runtime Error 03 Logical Error

Syntax is a set of rules that A problem was not detected Does not throw an error like
govern a language. when the program was syntax or runtime error.
Python cannot interpret the parsed (free of syntax errors), Program executes but output
instruction if the rules but is only revealed when a obtained is not the solution
followed. particular line is executed. you expect.
ENGG1810/9810

Python Error Types: (1) Syntax Error

Examples
What is Syntax Error?
Syntax is a set of rules that govern a language.

O
Common Python syntax errors include:
putting a keyword in the wrong place
leaving out a symbol (e.g. colon, comma or brackets)
misspelling a keyword O
incorrect indentation
empty block O

O
ENGG1810/9810

Python Error Types: (2) Runtime Error

Examples
What is Runtime Error?
If a program is syntactically correct (free of syntax errors), it will be run
by the Python interpreter.
The program may exit unexpectedly (a.k.a. it has crashed) during
execution if it encounters a runtime error: a problem which was not
detected when the program was parsed, but is only revealed when a
particular line is executed.

É
Some examples of Python runtime errors:
division by zero
performing an operation on incompatible types
using an identifier which has not been defined
accessing a list element, dictionary value or object attribute
ENGG1810/9810

Python Error Types: (2) Runtime Error

Examples
What is Runtime Error?
If a program is syntactically correct (free of syntax errors), it will be run
by the Python interpreter.
The program may exit unexpectedly (a.k.a. it has crashed) during
execution if it encounters a runtime error: a problem which was not
detected when the program was parsed, but is only revealed when a
particular line is executed.

Some examples of Python runtime errors:


division by zero
performing an operation on incompatible types
using an identifier which has not been defined
accessing a list element, dictionary value or object attribute

FileNotFoundError
got
ENGG1810/9810

Python Error Types: (3) Logical Error


Examples
What is Logical Error? (The most difficult to fix)
Logical error occurs when the program runs without crashing, but produces
an incorrect result (not the output that you expect). willcontinue o at

git
The error is caused by a mistake in the names leading
message, because no syntax or runtime error has occurred.
to o prints

6 times
Some examples of Python logical errors: No syntax or runtime error!
using the wrong variable name
indenting a block to the wrong level
using integer division instead of floating-point division
getting operator precedence wrong
making a mistake in a Boolean expression
off-by-one, and other numerical errors
ENGG1810/9810

Python Error Types: (3) Logical Error


Examples
What is Logical Error? (The most difficult to fix)
Logical error occurs when the program runs without crashing, but produces
an incorrect result (not the output that you expect). Where i <
num:
The error is caused by a mistake in the
message, because no syntax or runtime error has occurred.

6 times
Some examples of Python logical errors: No syntax or runtime error!
using the wrong variable name
indenting a block to the wrong level
using integer division instead of floating-point division
getting operator precedence wrong
making a mistake in a Boolean expression
off-by-one, and other numerical errors
ENGG1810/9810

With Functions
Matplotlib: Basic Plotting with Functions
Matplotlib: Times Series Line Graph with Functions
Errors and Exception Handling
Errors Types
Exception Handling
Try Except
Many Exceptions
Finally and Else
Raising Exceptions (with Functions)
ENGG1810/9810

Python Error and Exception Handling

Suppose you are travelling from your house to office but in the midway your car get punctured
hence your car will stop this scenario is called Exception. Mechanic will come and repair your car
and you will reach office and process of repairing the puncture is known as Exception Handling.

Stopped. Puncture
Car punctured. Repaired.

Exception Exception
Handled
ENGG1810/9810

Python Exceptions (Recap)


Covered in Lecture 4

Exception Handling Syntax


try: Try to do what we intend to do
<statements we intend to do>
except <ExceptionName>:
<statements for handling errors>

If it fails or something goes wrong, program


executes codes in except block

NOTE: If nothing goes wrong, the execute block does not run
and program moves to the first line after the try-except block.
ENGG1810/9810

Python Exceptions (Recap)


Covered in Lecture 4

try: Try to do what we intend to do


<statements we intend to do>
except <ExceptionName>:
<statements for handling errors>

If it fails or something goes wrong, program


executes codes in except block

So, there are many other


Built-in Exceptions

NOTE: If nothing goes wrong, the execute block does not run
and program moves to the first line after the try-except block.
ENGG1810/9810

Python Built-in Exceptions


More details can be found here: https://docs.python.org/3/library/exceptions.html#Exception
ENGG1810/9810

Python Exceptions

Exception Handling Syntax Example:


try: The try block will generate an exception since uos_code is not defined.
<statements we intend to do>
except <ExceptionName>:
<statements for handling errors> undefined
y
The following state will raise an error as
uos_code is not defined.
ENGG1810/9810

Python Exceptions

Exception Handling Syntax Example:


try: (With Built-in Exceptions NameError)
<statements we intend to do> The try block will generate an exception since uos_code is not defined.
except <ExceptionName>:
<statements for handling errors>

The following state will raise an error as Attenuators


uos_code is not defined.
ENGG1810/9810

Python Exceptions

Exception Handling Syntax Example:


try: (With Built-in Exceptions NameError as e)
<statements we intend to do> The try block will generate an exception since uos_code is not defined.
except <ExceptionName>:
<statements for handling errors>

The following state will raise an error as


uos_code is not defined.

What if we have other errors?

without crashing the program?


ENGG1810/9810

With Functions
Matplotlib: Basic Plotting with Functions
Matplotlib: Times Series Line Graph with Functions
Errors and Exception Handling
Errors Types
Exception Handling
Try Except
Many Exceptions
Finally and Else
Raising Exceptions (with Functions)
ENGG1810/9810

Python Many Exceptions

Exception Handling Syntax Example:


try:
<statements we intend to do>
except <ExceptionName>:
<statements for handling errors>

The following state will raise an error as Try this?


uos_code is not defined.

Fixed the NameError issue

Another issue!
ENGG1810/9810

Python Many Exceptions

Exception Handling Syntax Example:


try:
<statements we intend to do>
except <ExceptionName>:
<statements for handling errors>

The following state will raise an error as


Or this?
uos_code is not defined.
Fixed the NameError issue

What if we have two exceptions


Another issue!
at the same time?
ENGG1810/9810

Python Many Exceptions

Exception Handling Syntax Example:


try:
<statements we intend to do> Stop at the point you got the issue and directly handle the exception
except <ExceptionName>:
<statements for handling errors>

The following state will raise an error as


uos_code is not defined.

Stop at the point you got the issue and directly handle the exception
ENGG1810/9810

With Functions
Matplotlib: Basic Plotting with Functions
Matplotlib: Times Series Line Graph with Functions
Errors and Exception Handling
Errors Types
Exception Handling
Try Except
Many Exceptions
Finally and Else
Raising Exceptions (with Functions)
ENGG1810/9810

Python Exceptions - Finally


Finally: will be executed regardless if the try block
raises an error or not This is quite useful to close objects and clean up resources

try:
<statements we intend to do>
except <ExceptionName>:
<statements for handling errors>
finally:
<statements executed regardless if
the try block raises an error or not>
ENGG1810/9810

Python Exceptions - Else


Else: To define a block of code to be executed
if no errors were raised: Interesting Example to effectively use Else

try:
<statements we intend to do>
except <ExceptionName>:
<statements for handling errors>
else:
<statements if no exception occurs>
ENGG1810/9810

With Functions
Matplotlib: Basic Plotting with Functions
Matplotlib: Times Series Line Graph with Functions
Errors and Exception Handling
Errors Types
Exception Handling
Try Except
Many Exceptions
Finally and Else
Raising Exceptions (with Functions)
ENGG1810/9810

Python Exceptions Raise an Exception


As a Python developer you can choose to throw an
exception if a condition occurs. Rather than using if statement, you can try this:

number
Examples a
raise NameError

https://docs.python.org/3/tutorial/errors.html#raising-exceptions
ENGG1810/9810

Python Exceptions Raise an Exception (with Function)


As a Python developer you can choose to throw an
exception if a condition occurs.

Examples
raise NameError

https://docs.python.org/3/tutorial/errors.html#raising-exceptions
ENGG1810/9810

Python Exceptions Raise an Exception (with Function)


As a Python developer you can choose to throw an
exception if a condition occurs.

Examples
raise NameError

https://docs.python.org/3/tutorial/errors.html#raising-exceptions
ENGG1810/9810

Python Exceptions Raise an Exception (with Function)


As a Python developer you can choose to throw an
exception if a condition occurs.

Examples
raise NameError

https://docs.python.org/3/tutorial/errors.html#raising-exceptions
ENGG1810/9810

Python Exceptions Raise an Exception (with Function)


As a Python developer you can choose to throw an You can return the error as string as well!
exception if a condition occurs.

Examples
raise NameError

https://docs.python.org/3/tutorial/errors.html#raising-exceptions
ENGG1810/9810

THANKS FOR
WATCHING
Good luck in studying

You might also like