JAY NIGAM
RA1711003030394
CSE - G
1
Introduction to Python
Python is a high-level programming language
Open source and community driven
“Batteries Included”
a standard distribution includes many modules
Dynamic typed
Source can be compiled or run just-in-time
Similar to perl, tcl, ruby
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
Example Python
Hello World
print “hello world”
Prints hello world to
standard out
Open IDLE and try it out
yourself
Follow along using IDLE
4
String Methods
Assign a string to a
variable
In this case “hw”
hw.title()
hw.upper()
hw.isdigit()
hw.islower()
5
Lists
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
6
Conditional Branching
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
7
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
8
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
9
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
10
What Is a Program?
Usually, one or more algorithms written in a
programming language that can be translated to run
on a real machine
We sometimes call programs software
What Is a Programming Language?
A programming language is somewhat like a
natural language, but with a very limited set of
statements and strict syntax rules.
Has statements to implement sequential,
conditional and iterative processing - algorithms
Examples: FORTRAN, COBOL, Lisp, Basic, Pascal,
C, C++, Java, C#, Python, …
Compiler
A compiler is a program that converts a program
written in a programming language into a program
in the native language, called machine language, of
the machine that is to execute the program.
Three kinds of errors
Syntax error : Some statement in the program is not a
legal statement in the language.
Runtime error : An error occurs while the program is
executing, causing the program to terminate (divide by
zero, etc.)
Logic error : The program executes to completion, but
gives incorrect results.
Language terminology
SYNTAX: The formal rules for legal statements in the
language.
SEMANTICS: The meaning of the statements - what
happens when the statement is executed.
The Basic Pattern
Most of our programs will use the basic pattern of
Get some user input
Perform some algorithm on the input
Provide results as output
Keywords
Keywords are reserved words that have special
meaning in the Python language. Because they are
reserved, they can not be used as identifiers.
Examples of keywords are if, while, class, import.
Variables in Python
A variable has
A name – identifier
A data type - int, float, str, etc.
Storage space sufficient for the type.
Numeric Data Types
Int:
This type is for whole numbers, positive or
negative. Examples: 23, -1756
Float:
This type is for numbers with possible fraction
parts. Examples: 23.0, -14.561
Integer operators
The operations for integers are:
+ for addition
- for subtraction
* for multiplication
/ for integer division
Example - Fahrenheit to Centigrade
We want to convert a Fahrenheit temperature to
Centigrade.
The formula is C = (F -32) x 5/9
We use type float for the temperatures.
Python Session
Certificate
23