ENG202 Numerical Methods in Engineering
Brief Introduction to
Python
ENG202 Numerical Methods in Engineering ◊ Boris Golman, ChME
Python Syntax
• Comments in Python: A comment begins with a hash character(#)
• Joining two lines: Use backslash character(\)
• Multiple Statements on a Single Line: use a semicolon (;)
• Indentation: uses spaces to define program blocks
• Reserved words:
https://www.w3resource.com/python/python-syntax.php
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 2
Python Operators I
• Arithmetic Operators:
https://www.w3resource.com/python/python-syntax.php
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 3
Python Operators II
• Comparison Operators:
https://www.w3resource.com/python/python-syntax.php
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 4
Python Operators III
• Logical Operators:
https://www.w3resource.com/python/python-syntax.php
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 5
Python Operators IV
• Assignment Operators:
https://www.w3resource.com/python/python-syntax.php
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 6
Python Data Types I
• Integer
x = 2 type int
• Floating point number
x = 2.0 type float
• String
x = ‘this is a string’ type str
• Type conversion
x = int(y); x = float(y); x = str(y)
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 7
Python Data Types II
• Implicit type conversion
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 8
Branching I
Swim advisor
Start
Input
Water temperature, tw
Yes
tw > 24
No
Great, jump in!
Stop
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 9
Branching II
Swim advisor
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 10
Branching III
Swim advisor
Start
Input
Water temperature, tw
No Yes
tw > 24
No
tw > 20
Yes
Do not swim, Not bad, put Great,
too cold! your toe in first! jump in!
Stop
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 11
Branching IV
Swim advisor
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 12
Looping I
While loop
while (expression) :
statement_1
statement_2
....
The while loop runs as long as the expression (condition) evaluates to True
and execute the program block.
The condition is checked every time at the beginning of the loop and the
first time when the expression evaluates to False, the loop stops without
executing any remaining statement(s).
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 13
Looping II
for loop
for <variable> in range(<number>):
range(a) : Generates a sequence of
numbers from 0 to a, excluding a,
incrementing by 1.
for "variable" in range("start_number", "end_number"):
range(a,b): Generates a sequence of
numbers from a to b excluding b,
incrementing by 1.
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 14
Looping III
for loop
for "variable" in range("start_number", "end_number",
"increment"):
range(a, b, c): Generates a sequence of numbers from a to b
excluding b, incrementing by c.
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 15
Looping IV
for loop
for variable_name in sequence :
statement_1
statement_2
....
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 16
Looping V
Break statement – to exit while or for loops
while (expression1) : for variable_name in sequence :
statement_1 statement_1
statement_2 statement_2
...... if expression3 :
if expression2 : break
break
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 17
Looping VI
Continue statement - to take the control to the top of the loop
without executing the rest statements inside the loop
while (expression1) : for variable_name in sequence :
statement_1 statement_1
...... ......
if expression2 : if expression2 :
continue continue
statement_2 statement_2
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 18
Numerical arrays I
• Array creation
linspace function from numpy could be used to generate an
array of evenly distributed numbers from an interval [a, b].
zeros function from numpy may be used to generate an array
with zeros.
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 19
Numerical arrays II
• Slicing Array
x[i:j] address all elements from index i (inclusive) to j
(exclusive) in an array x
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 20
Numerical arrays III
• Two-dimensional arrays (matrix)
i=0
i=1
i=2
j=0 1 2
https://www.pythoninformer.com/python-libraries/numpy/index-and-slice/
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 21
Numerical arrays IV
• Two-dimensional arrays (matrix)
a2[0,1] = a2[i=0, j=1]
i=0
i=0 i=1
i=1 i=2
j=0 1 2
i=2
j=0 1 2 a2[2,1] = a2[i=2, j=1]
https://www.pythoninformer.com/python-libraries/numpy/index-and-slice/
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 22
Numerical arrays V
• Two-dimensional arrays (matrix)
Column slicing:
Row slicing:
https://www.pythoninformer.com/python-libraries/numpy/index-and-slice/
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 23
Function
arguments
return values
calling function
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 24
Function
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 25
Function
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 26
Function
Variables defined inside a function are not visible from
outside
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 27
Function
Variables defined inside a function are not visible from
outside
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 28
Function
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 29
Function
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 30
Function
Variables declared outside of the function are global
variables and they can be accessed inside or outside of the
function.
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 31
Function
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 32
Function
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 33
Function
ENG202 Numerical Methods in Engineering ◊ Introduction to Python ◊ Boris Golman, ChME 34