Professional Documents
Culture Documents
Daniel P. Ames
Hydroinformatics
Fall 2012
>>> 3*(7+2)
27
The IDLE GUI Environment
(Windows)
Shell for interactive evaluation.
Text editor with color-coding and smart indenting for
creating Python files.
Menu commands for changing system settings and
running files.
def get_final_answer(filename):
“Documentation String”
line1
line2 Colon.
return total_counter
So, for example, you can’t just append an integer to a string. You
must first convert the integer to a string itself.
x = “the answer is ” # Decides x is bound to a string.
y = 23 # Decides y is bound to an integer.
print x + y # Python will complain about this.
Calling a Function
>>>
For Loops
For Loops 1
For-each is Python’s only for construction
A for loop steps through each of the items in a collection
type, or any other type of object which is “iterable”
>>> “hello”.upper()
‘HELLO’
• Python: a free,
cross-platform, and
easy to learn
language
• Used to execute
single tools
or strings of tools
• Develop, execute,
and share
geoprocessing
workflows
• Automation
ArcPy
# Import ArcPy
import arcpy
• Tool documentation
• arcpy.Usage(“Buffer_analysis”)
Setting environments in Python
• Common environments:
– Workspace, coordinate system, extent
• Examples:
arcpy.env.workspace
arcpy.env.outputCoordinateSystem
arcpy.env.extent
Live Demo
# If an error occurs
except:
# Print that Buffer failed and why
print "Buffer failed“
print arcpy.GetMessages(2)
Live Demo