Introduction To Python (Concepts and Discussion)
Introduction To Python (Concepts and Discussion)
var.append(object)
adds object to the end of the list
var.remove(n)
removes card at n
var.pop(n)
removes n and returns its value
You will create lists:
Layers as inputs
Attributes to
match
Arrays of objects
You will work with
lists:
List of field names
List of selected
features
Like a list, tuples are iterable arrays of
objects
Tuples are immutable –
once created, unchangeable
To add or remove items, you must redeclare
Example uses of tuples
County Names
Land Use Codes
Ordered set of functions
Dictionariesare sets of key & value pairs
Allows you to identify values by a descriptive
name instead of order in a list
Keys are unordered unless explicitly sorted
Keys are unique:
var[‘item’] = “apple”
var[‘item’] = “banana”
print var[‘item’] prints just banana
Python uses whitespace and indents to
denote blocks of code
Lines of code that begin a block end in a
colon:
Lines within the code block are indented at
the same level
To end a code block, remove the indentation
You'll want blocks of code that run only when
certain conditions are met
if and else
if variable == condition:
#do something based on v == c
else:
#do something based on v != c
elif allows for additional branching
if condition:
elif another condition:
…
else: #none of the above
For allows you to loop over a block of
code a set number of times
For is great for manipulating lists:
a = ['cat', 'window', 'defenestrate']
for x in a:
print x, len(x)
Results:
cat 3
window 6
defenestrate 12
We could use a for loop to perform
geoprocessing tasks on each layer in a list
We could get a list of features in a feature
class and loop over each, checking attributes
Anything in a sequence or list can be used in
a For loop
Just be sure not to modify the list while
looping
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
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"
Filesare manipulated by creating a file
object
f = open("points.txt", "r")
The file object then has new methods
print f.readline() # prints line from file
Files can be accessed to read or write
f = open("output.txt", "w")
f.write("Important Output!")
Files are iterable objects, like lists
Check for type assignment errors, items not
in a list, etc.
Try & Except
try:
a block of code that might have an error
except:
code to execute if an error occurs in "try"
Allowsfor graceful failure
– important in ArcGIS
Python Homepage
http://www.python.org/
Dive Into Python
http://www.diveintopython.org/
Learning Python, 3rd Edition
http://www.oreilly.com/catalog/9780596
513986/
Getting Started Writing Geoprocessing Scripts
Available on ESRI's support page