Python Programming Fundamentals Python Programming Environment We will use the PythonWin version 27 system This programming environment includes a code editor and a code interpreter A code interpreter executes statements linebyline as you type them very useful for testing ID: 591041
Download Presentation The PPT/PDF document "GIT461 GIS" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
GIT461 GIS
Python Programming FundamentalsSlide2
Python Programming Environment
We will use the “
PythonWin
” version 2.7 systemThis programming environment includes a code editor and a code interpreterA code interpreter executes statements line-by-line as you type them – very useful for testingA Code editor is used to type a complete program that is later tested and “de-bugged”Slide3
PythonWin Environment
Interpreter window is displayed first.
You can type and execute simple statements in this window.Slide4
PythonWin Environment
To type a Python script program select “File > New > Script”.
Type in code and save with a .
py extention.A good idea to have a folder such as “\PythonProgs
\” just for source code scripts.Slide5
PythonWin Environment
To load an existing python script select “File > Open” to load script into an edit window.Slide6
PythonWin Environment
To run a script you should load it, and then select “File > Run”
Note that you may enter “command line arguments” at this point if needed
Select “OK” button to run the scriptSlide7
Python Programming Fundamentals
Google “Python Language Reference” to load this help fileSlide8
Python Data Types
String: a sequence of alphanumeric characters
Integer: a whole number that has no fractional component
Float: a number that contains a fractional componentString example: “105 Glendale Avenue” (note that strings are enclosed in quotes)Integer examples: 100, -19, 0, 9999999Float examples: 1.0, -123.678, 1.6745E3Slide9
Python Assignment Statement
The “=“ sign is the assignment operator as it is in most programming languages
X = 1
Print X # the number “1” will appear on the screenX = X + 5Print X # the number “6” will appear on the screenSlide10
Python Comments
A python comment begins with a “#”.
Anything after the “#” is ignored by Python
The 1st line in the below script is a comment line- it is ignored by PythonThe characters to the right of the “#” on lines 2-5 are ignored
# get x1, y1, x2, y2 from the command line
x1param = float(
StripComma
(
sys.argv
[1])) # x1
y1param = float(
StripComma
(
sys.argv
[2])) # y1
x2param = float(
StripComma
(
sys.argv
[3])) # x2
y2param = float(
StripComma
(
sys.argv
[4])) # y2Slide11
Python Operators (in order of precedence)
Multiplication: *
Division: /
Modulus: %Addition: +Subtraction: -Slide12
Expressions
Expressions are combinations of data and operators:Slide13
Built-in Python Functions
A function takes an “argument” or “arguments” and returns a value that can be used in an assignment statement
In the below
satements abs(x) and pow(x,y) are built-in functions in every implementation of the Python language
x = abs(-8)
print x # the number “8” would appear in the interactive window
y = pow(2,3)
print y # the number “8” would appear in the interactive windowSlide14
Python Built-In Functions
abs(x) # returns the absolute value of x
float(x) # returns the string x converted to a floating point number
int(x) # returns the string x converted to a integer numberpow(x,y) # returns the number x rasied to the y power
round(
x,n
) # rounds the number x to n decimal places
str
(x) # returns the string equivalent of the object xSlide15
More Complex Expressions using Functions and Exponentiation
Note that trig functions use radian angular values.
You must convert degrees to radians before using the trig functions ( radians = degrees * 3.1416/180.0).
Note that before using the trig functions the math “module” had to be imported.Slide16
Controlling Program Flow: Conditional Statements
A statement uses “reserved” python language words to initiate an action
A conditional statement makes a decision at run-time
X = 10
If x == 10 :
print “X=10
”
Note: everything indented
Below the “If” statement is
Part of the statement.Slide17
Conditional Statements: More Complex IF/ELIF/ELSE construct
The “
elif
” and “else” keywords can be used to construct more complex decision structuresx =
random.randint
(1,10)
If x == 1 :
print “you are in first place”
Elif
x == 2 :
print “you are in second place”
Elif
x == 3 :
print “you are in third place”
Else :
print “ sorry, you didn’t place this time” Slide18
Controlling program flow with a loop: While statement
While statements can be used to repeat a section of code until some condition is reached.
i
= 0While
i
<= 10 :
print I
i
=
i
+ 1
# you could also use
i
+= 1Slide19
For Loops
A “For” loop uses a “list”. First a list must be built before it can be used in the For loop
Mylist
= [“A”, “B”, “C”, “D”]For letter in Mylist
:
print letter # the letters “A” through “D” would print to the screenSlide20
Getting User Input: command line
Command line parameters are entered at run time in the “Run Script” windowSlide21
Getting User Input: “Input” function
The “input” function prompts the user for input during the running of the scriptSlide22
User Input: Input function
Note that if you use the “input” function and you enter a string value it must be enclosed in
quotes (single or double).Slide23
Functions
A function is a stand-alone section of code that is designed to accomplish a specific task based on one or more parameters passed to the function
The function returns a calculated result so a function normally appears in the main code to the right of an assignment (=) statement so the returned value is stored in the variable on the left side of the assignment statementSlide24
Function Placement
Functions are normally placed at the top of the main program file because they need to be defined before they are referenced in the main program
Below is an example function that converts a longitude string value (“-0883015”) to its decimal degree number equivalent