Python
Python
Python
CLASS – XI-A
“Batteries Included”
◦ a standard distribution includes many modules
Dynamic typed
Internet Scripting
Embedded Scripting
Database Programming
Artificial Intelligence
Image Processing
Visual Basic is still the method of configuring
and customizing ArcMap
If you have a button on the toolbar, it’s VB
Python scripts can be placed in ArcToolbox
Python can be run from the command line
without ArcMap or ArcCatalog being open
Using just the GIS Engine, lower overhead
Rapid prototyping, ease of authoring, etc.
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
Java
Typically 3-5 times shorter than equivalent
Java programs
Run-time works harder than Java’s
Components can be developed in Java and
combined to form applications in Python
Python can be used to prototype components
into Java implementation
IDLE helps you
program in Python
by:
◦ color-coding your
program code
◦ debugging
◦ auto-indent
◦ interactive shell
Hello World
print “hello world”
Prints hello world to
standard out
Open IDLE and try it
out yourself
Follow along using
IDLE
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
Assign a string to a
variable
In this case “hw”
hw.title()
hw.upper()
hw.isdigit()
hw.islower()
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”
Lists (mutable sets of strings)
◦ var = [] # create list
◦ var = [‘one’, 2, ‘three’, ‘banana’]
Tuples (immutable sets)
◦ var = (‘one’, 2, ‘three’, ‘banana’)
Dictionaries (associative arrays or ‘hashes’)
◦ var = {} # create dictionary
◦ var = {‘lat’: 40.20547, ‘lon’: -74.76322}
◦ var[‘lat’] = 40.2054
Each has its own set of methods
Think of a list as a stack of cards, on which
your information is written
The information stays in the order you place
it in until you modify that order
Methods return a string or subset of the list
or modify the list to add or remove
components
Written as var[index], index refers to order
within set (think card number, starting at 0)
You can step through lists as part of a loop
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
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
THANK YOU…