Introduction To Python: Name: Juhi Sawon Course:BCA 6 (B) Roll Number: 1021605 (14) Submitted To: Atul Bhandari Sir
Introduction To Python: Name: Juhi Sawon Course:BCA 6 (B) Roll Number: 1021605 (14) Submitted To: Atul Bhandari Sir
Introduction To Python: Name: Juhi Sawon Course:BCA 6 (B) Roll Number: 1021605 (14) Submitted To: Atul Bhandari Sir
• Hello World
print “hello
world”
• Prints hello
world to standard
out
• Open IDLE and try
it out yourself
• Follow along
using IDLE
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
String Methods
• Assign a string
to a variable
• In this case
“hw”
• hw.title()
• hw.upper()
• hw.isdigit()
• hw.islower()
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”
Other Python Objects
• 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
Lists
• 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
Looping with For
• 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
Looping with For
• 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
• 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
• 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"
Files
• Files are 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
Error Capture
• 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"
• Allows for graceful failure
– important in ArcGIS
THANKYOU