Scripting Basics and Introduction to Python - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Scripting Basics and Introduction to Python

Description:

Statements generally consist of one line in the script ... Variable names can include alphanumeric characters and underscores (no spaces! ... – PowerPoint PPT presentation

Number of Views:288
Avg rating:3.0/5.0
Slides: 26
Provided by: peopleOre
Category:

less

Transcript and Presenter's Notes

Title: Scripting Basics and Introduction to Python


1
Scripting Basics and Introduction to Python
  • Geo599
  • Week 2, Oct. 5, 2009

2
What is a script?
  • An ordered series of statements that instruct the
    computer to complete a task
  • Sound familiar? Definition of geoprocessing from
    last week Organized sequence of steps to
    accomplish a spatial analysis task

3
Statements
  • Statement A discrete instruction that tells the
    computer to do one specific thing
  • Statements generally consist of one line in the
    script
  • In Python, statements are distinguished by line
    breaks (no special characters)

4
Types of Statements
  • Assignment
  • Print
  • Comment
  • Run a method or function
  • Combination of assignment and method or function
  • Start a loop
  • Test a condition
  • Other special purpose statements

5
Variables
  • Variables are named containers for data
  • Provide a way for you to refer to data when you
    want the computer to do something with it
  • Variables can contain many different kinds of
    data
  • The specific data stored in a variable can change
    over the course of a script

6
Data Types
  • Once a variable is assigned a value, its data
    type is set
  • The actions you are allowed to do with data
    depend on its type
  • Basic data types
  • Integers
  • Doubles
  • Strings
  • Python data types
  • Dictionaries
  • Lists
  • ArcGIS data types
  • Feature classes
  • Tables
  • Cursors
  • Row objects

7
Pair and Compare
  • Statements
  • Variables
  • Data Types

8
Assignment
  • Assignment statements assign a value to a
    variable
  • The syntax for an assignment statement is
  • ltVariableNamegt ltValuegt
  • Examples
  • FirstName Tracy ? FirstName is a string
    variable
  • CreditHrs 12.0 ? CreditHrs is a double

9
Assignment, cont.
  • After assigning a value to a variable, you can
    use the variable name to refer to the data stored
    in it
  • Variable names can include alphanumeric
    characters and underscores (no spaces!)
  • (Python is dynamically typed so you do not need
    to declare variables before using them.)

10
More complex assignments
  • Can assign results of an operation
  • Sum 23
  • ? Sum is now an integer variable with value 5
  • Operations can include other variables
  • Diff Big - Small
  • Assignment is also used to store results of
    functions and methods

11
Print Statements
  • Print statements print something to the
    interactive window
  • The syntax for a print statement is
  • print ltvaluegt
  • Examples
  • print Hello, my name is
  • print FirstName
  • Each print statement starts on a new line

12
Simple Concatenation
  • Concatenation is a string operation that allows
    you to put several strings together
  • Pythons basic concatenation operator is
  • Example
  • FullName FirstName LastName

13
A very short script
  • FirstName Tracy
  • LastName Kugler
  • print Hello, my name is FirstName
    LastName
  • What variables are used in this script?
  • What data type are they?
  • What types of statements are used?
  • What does the script do?
  • What kinds of modifications could be made?

14
Comments
  • Comments are special statements that are not
    executed by the computer
  • Comments are used to explain what the script is
    doing
  • The syntax for comments is
  • ltcommentgt OR
  • ltcommentgt
  • (The second version is typically used for blocks
    of comments, such as at the beginning of a script)

15
Use of Comments
  • Comments should be used liberally to allow people
    looking at your script to understand what it does
  • Each script should include a header comment block
    that includes
  • A summary of what the script does
  • The scripts inputs and outputs
  • Who wrote the script and when
  • Comments should also be used throughout the
    script to describe each functional set of
    statements

16
IDLE
  • IDLE is the interactive development environment
    that is packaged with Python in ArcGIS
  • IDLE is used to write and to run scripts
  • Geoprocessing scripts can be run in IDLE without
    ArcGIS running
  • IDLE consists of two types of windows
  • Python Shell (interactive window)
  • Scripts

17
Python Shell
  • Python Shell opens when you start IDLE
  • Serves as an interactive window

18
Python Shell as an Interactive Window
  • Statements entered at the gtgtgt prompt will be
    executed immediately
  • Variable values assigned here persist until you
    close the window
  • The results of print statements appear here
  • Good way to quickly test statements

19
Script Window
  • Scripts (.py) are essentially text files
  • IDLE color-codes
  • Orange Python key words
  • Red Comments
  • Green Strings
  • Purple Function names

20
Script Window Menu Highlights
  • File gt Recent files
  • Also in interactive window
  • Quick access to recently opened scripts
  • Run gt Run module (F5)
  • Run the script
  • Format gt Comment/Uncomment
  • Useful for turning off sections of script
  • Format gt Indent/Dedent
  • Define code blocks

21
Activity
  • Use the interactive window to play with some
    assignment and print statements
  • Try different data types (e.g., strings,
    integers)
  • Try creating variables that store combinations of
    other variables (e.g., concatenation or
    calculation)
  • What happens when you try to concatenate a string
    and an integer?
  • Try combining some of these statements in a
    simple script in the script window and running it

22
Functions
  • Functions are used to do things that are more
    complex than operations
  • Have inputs and outputs
  • Inputs are called arguments or parameters
  • Outputs are returned

23
Parameters
  • Parameters are passed to a function
  • The syntax for calling a function is
  • ltFunctionNamegt(ltparametersgt)
  • Ex Str(Int)
  • ? Converts an integer into a string
  • Functions may have multiple parameters, separated
    by commas
  • With multiple parameters, ORDER MATTERS
  • Parameters are expected to be a specific data type

24
Function example
  • Suppose there was a function to do what the Clip
    tool does (there actually isnt, quite)
  • What are the parameters? What data type are
    they?
  • What do you do with the output in a script?
  • What would a call to this function look like in a
    script?

25
Function Example
  • ClippedLayer Clip(InputLayer, ClipLayer)
Write a Comment
User Comments (0)
About PowerShow.com