Lecture 6
Lecture 6
ENGG1810/9810
Introduction to Engineering Computing
Week 6: Functions II
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
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
Python Plotting
Bar chart Scatter plot Line graph
ENGG1810/9810
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
# of months, # of passengers
mmmm
ENGG1810/9810
# 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
# 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\
# of months, # of passengers
..
. before comma (,) after comma (,)
a
ay
You can check how it looks
like by printing it
ENGG1810/9810
o su
setting
limits nz us
ENGG1810/9810
wash
quietest
rforanimation
ENGG1810/9810
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
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
Remember? how to handle the error if the file does not exist.
i.e. File is not found in the directory
throw
catch
ENGG1810/9810
Remember? how to handle the error if the file does not exist.
i.e. File is not found in the directory
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
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
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
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
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.
FileNotFoundError
got
ENGG1810/9810
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
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
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
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
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
Python Exceptions
Python Exceptions
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
Another issue!
ENGG1810/9810
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
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
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
number
Examples a
raise NameError
https://docs.python.org/3/tutorial/errors.html#raising-exceptions
ENGG1810/9810
Examples
raise NameError
https://docs.python.org/3/tutorial/errors.html#raising-exceptions
ENGG1810/9810
Examples
raise NameError
https://docs.python.org/3/tutorial/errors.html#raising-exceptions
ENGG1810/9810
Examples
raise NameError
https://docs.python.org/3/tutorial/errors.html#raising-exceptions
ENGG1810/9810
Examples
raise NameError
https://docs.python.org/3/tutorial/errors.html#raising-exceptions
ENGG1810/9810
THANKS FOR
WATCHING
Good luck in studying