IIQF Python
IIQF Python
2
Why Python?
⚫ Unlike AML and Avenue, there is a
considerable base of developers already
using the language
⚫ “Tried and true” language that has been in
development since 1991
⚫ Can interface with the Component Object
Model (COM) used by Windows
⚫ Can interface with Open Source GIS toolsets
3
Python Interfaces
⚫ IDLE – a cross-platform Python
development environment
⚫ PythonWin – a Windows only interface to
Python
⚫ Python Shell – running 'python' from the
Command Line opens this interactive shell
⚫ For the exercises, we'll use IDLE, but you
can try them all and pick a favorite
4
IDLE – Development Environment
⚫ IDLE helps you
program in Python
by:
– color-coding your
program code
– debugging
– auto-indent
– interactive shell
5
Example Python
⚫ Hello World
print “hello
world”
⚫ Prints hello world to
standard out
⚫ Open IDLE and try it
out yourself
⚫ Follow along using
IDLE
6
More than just printing
⚫ Python is an object oriented language
⚫ Practically everything can be treated as an
object
⚫ “hello world” is a string
⚫ Strings, as objects, have methods that return
the result of a function on the string
7
String Methods
⚫ Assign a string to a
variable
⚫ In this case “hw”
⚫ hw.title()
⚫ hw.upper()
⚫ hw.isdigit()
⚫ hw.islower()
8
String Methods
⚫ The string held in your variable remains the
same
⚫ The method returns an altered string
⚫ Changing the variable requires reassignment
– hw = hw.upper()
– hw now equals “HELLO WORLD”
9
Keywords and identifiers
⚫ Cannot use keyword as variable
⚫ Keywords are case sensitive
⚫ In python 3.9, there were 36 keywords
⚫ All keywords except for True ,False and
None are in lowercase.
⚫ Identifier – Any combination of letter in
lowercase or uppercase or in digits or an
underscore _. Like ‘variable_2’ correct
identifier.
⚫ Identifier cannot start with a digit.1x is not
valid identifier.
Indian Institute of Quantitative Finance
Coding Statements
⚫ Assignment is a coding statement like a=1
⚫ Multi-line assignment – use this (\) and then
go to next line.
⚫ Multiline can be achieved using (),[] or {}.
⚫ For indentation, use (: )
Output:
['iiqf', 243, 3.9, 'Don', 28.9]
iiqf
[243, 3.9]
[3.9, 'Don', 28.9]
[123, 'Don', 123, 'Don']
['iiqf', 243, 3.9, 'Don', 28.9, 123, 'Don']
List Methods
⚫ Adding to the List
– var[n] = object
⚫ replaces n with object
– var.append(object)
⚫ adds object to the end of the list
⚫ Removing from the List
– var[n] = []
⚫ empties contents of card, but preserves order
– var.remove(n)
⚫ removes card at n
– var.pop(n)
⚫ removes n and returns its value
17
List Methods
list[1] = 246
print(list)
list.append(‘xyz’)
print(list)
list[3]=[]
print(list)
list.remove(28.9)
print(list)
list.pop(0)
print(list)
Output:
List vs Tuple
Lists Tuple
Lists are mutable like list. append will Tuples are immutable. There is no
work. method append here.
Iterations in list are slow Iterations are comparatively faster
List is better for addition and removal of Tuple is fast, so better for accessing
elements elements.
Consumes more memory Consumes less memory
Multiple methods in list Less methods in list.
More prone to errors Less prone to errors
var2 = 0
if var2:
print("2 - Got a true expression value")
print(var2)
else:
print("2 - Got a false expression value")
print(var2)
The Nested if...elif...else
Construct
var = 100
if var < 200:
print("Expression value is less than 200")
if var == 150:
print("Which is 150")
elif var == 100:
print("Which is 100")
elif var == 50:
print("Which is 50")
elif var < 50:
print("Expression value is less than 50")
else:
print("Could not find true expression")
Single Statement Suites:
If the suite of an if clause consists only of a single line, it may
go on the same line as the header statement:
x=1
if (x==1): print("x is 1")
5. Python - while Loop
Statements
The while loop is one of the looping constructs available in
Python. The while loop continues until the expression
becomes false. The expression has to be a logical expression
and must return either a true or a false value
The syntax of the while loop is:
while expression:
statement(s)
Example:
x = 0
while (x < 9):
print ('The count is:', x)
x = x + 1
The Infinite Loops:
You must use caution when using while loops because of the
possibility that this condition never resolves to a false value.
This results in a loop that never ends. Such a loop is called
an infinite loop.
An infinite loop might be useful in client/server programming
where the server needs to run continuously so that client
programs can communicate with it as and when required.
var = 10
while var>0:
print('Current var value :', var)
var = var-1
if var == 5:
break
The continue Statement:
⚫ The continue statement in Python returns the control to the
beginning of the while loop. The continue statement rejects
all the remaining statements in the current iteration of the
loop and moves the control back to the top of the loop.
Example:
for letter in 'IIQFNew':
if letter == 'N':
continue
print('Current Letter :', letter)
var = 10
while var > 0:
var = var -1
if var == 5:
continue
print('Current variable value :', var)
The else Statement Used with Loops
45
Why do we need NumPy
⚫ Python does numerical computations slowly.
⚫ 1000 x 1000 matrix multiply
– Python triple loop takes > 10 min.
– Numpy takes ~0.03 seconds
46
NumPy Overview
1. Arrays
2. Shaping and transposition
3. Mathematical Operations
4. Indexing and slicing
47
Arrays
Structured lists of numbers.
⚫ Vectors
⚫ Matrices
⚫ Images
⚫ Tensors
⚫ ConvNets
48
Arrays
Structured lists of numbers.
⚫
⚫ Vectors
⚫ Matrices
⚫ Images
⚫ Tensors
⚫ ConvNets
49
Arrays
Structured lists of numbers.
⚫ Vectors
⚫ Matrices
⚫ Images
⚫ Tensors
⚫ ConvNets
50
Arrays
Structured lists of numbers.
⚫ Vectors
⚫ Matrices
⚫ Images
⚫ Tensors
⚫ ConvNets
51
Arrays
Structured lists of numbers.
⚫ Vectors
⚫ Matrices
⚫ Images
⚫ Tensors
⚫ ConvNets
52
Arrays, Basic Properties
import numpy as np
a = np.array([[1,2,3],[4,5,6]],dtype=np.float32)
print a.ndim, a.shape, a.dtype
53
Arrays, creation
⚫ np.ones, np.zeros
⚫ np.arange
⚫ np.concatenate
⚫ np.astype
⚫ np.zeros_like,
np.ones_like
⚫ np.random.random
54
Arrays, creation
⚫ np.ones, np.zeros
⚫ np.arange
⚫ np.concatenate
⚫ np.astype
⚫ np.zeros_like,
np.ones_like
⚫ np.random.random
55
Arrays, creation
⚫ np.ones, np.zeros
⚫ np.arrange
arange([start,]stop[,step],[,dtype=None]
⚫ np.concatenate
⚫ np.astype
⚫ np.zeros_like, np.ones_like
⚫ np.random.random
56
Arrays, creation
⚫ np.ones, np.zeros
⚫ np.arange
⚫ np.concatenate
⚫ np.astype
⚫ np.zeros_like,
np.ones_like
⚫ np.random.random
57
Axis in Numpy
59
Arrays, creation
⚫ np.ones, np.zeros
⚫ np.arange
⚫ np.concatenate
⚫ np.astype
⚫ np.zeros_like,
np.ones_like
⚫ np.random.random
60
Arrays, creation
⚫ np.ones, np.zeros
⚫ np.arange
⚫ np.concatenate
⚫ np.astype
⚫ np.zeros_like,
np.ones_like
⚫ np.random.random
61
Arrays, creation
⚫ np.ones, np.zeros
⚫ np.arange
⚫ np.concatenate
⚫ np.astype
⚫ np.zeros_like,
np.ones_like
⚫ np.random.rando
m
62
Arrays, danger zone
⚫ Must be dense, no holes.
⚫ Must be one type
⚫ Cannot combine arrays of different shape
63
Shaping
a =
np.array([1,2,3,4,5,6])
a = a.reshape(3,2)
a = a.reshape(2,-1)
a = a.ravel()
1. Total number of elements cannot
change.
2. Use -1 to infer axis shape
3. Row-major by default (MATLAB is
column-major) 64
Transposition
a = np.arange(10).reshape(5,2)
a = a.T
a = a.transpose((1,0))
65
Mathematical operators
⚫ Arithmetic operations are element-wise
⚫ Logical operator return a bool array
⚫ In place operations modify the array
66
Mathematical operators
⚫ Arithmetic operations are element-wise
⚫ Logical operator return a bool array
⚫ In place operations modify the array
67
Mathematical operators
⚫ Arithmetic operations are element-wise
⚫ Logical operator return a bool array
⚫ In place operations modify the array
68
Mathematical operators
⚫ Arithmetic operations are element-wise
⚫ Logical operator return a bool array
⚫ In place operations modify the array
69
Math, universal functions
Also called ufuncs
Element-wise
Examples:
– np.exp
– np.sqrt
– np.sin
– np.cos
– np.isnan
70
Quick Game
⚫ Create a game, where you guess a number
between 1 to 10 and check it with random
integer number generated by the code. If
number matches, you win else loose.
Notes:
– Zero-indexing
– Multi-dimensional indices are comma-separated
(i.e., a tuple)
72
Scipy
⚫ Scipy(https://www.scipy.org/):
• Scientific Python
⚫
• based on the data structures of Numpy and furthermore its basic creation and
manipulation functions
⚫
• Both numpy and scipy has to be installed. Numpy has to be installed before
scipy
78
Scipy
⚫ Statistics(Scipy.Stats) :
print(stats.norm.cdf(0))
print(stats.norm.cdf(0.5))
print(stats.norm.cdf(np.array([-3.33, 0, 3.33])))
print(stats.norm.ppf(0.5))
print(stats.norm.ppf(3))
83
Modules
⚫ Modules are additional pieces of code that
further extend Python’s functionality
⚫ A module typically has a specific function
– additional math functions, databases, network…
⚫ Python comes with many useful modules
⚫ arcgisscripting is the module we will use to
load ArcGIS toolbox functions into Python
84
Modules
⚫ Modules are accessed using import
– import sys, os # imports two modules
⚫ Modules can have subsets of functions
– os.path is a subset within os
⚫ Modules are then addressed by
modulename.function()
– sys.argv # list of arguments
– filename = os.path.splitext("points.txt")
– filename[1] # equals ".txt"
85