Docsity

Python Programming for GIS: Looping, Conditional Statements, and Modules (GIS 5222), Lecture notes of Information Systems

Partial preview of the text

Download Python Programming for GIS: Looping, Conditional Statements, and Modules (GIS 5222) and more Lecture notes Information Systems in PDF only on Docsity!

Python Intro Continued

GIS 5222

Jake K. Carr

Week 2

Looping: While loops

A way to automate a process until a particular condition is reached:

x = 0 while x < 4: print x x += 1 print " You ran the loop " + str ( x ) + " times. "

Here we have assigned the numeric value of 0 to the object x.

Then we sequentially print the value of x (= 0 initially), modify &

assign 1 to x, and then finally print the message:

You ran the loop str ( x ) times.

Looping: For loops

Another way to automate a process, only here the process is

repeated for each element of a sequence:

for name in [ " Carter " , " Reagan " , " Bush " ]: print name + " was a U. S. president. "

Here we have a list of string items. For each item in the list we

sequentially print the value of the item concatenated with an

additional string. The process repeats until there are no more

items remaining in the list:

Carter was a U. S. president. Reagan was a U. S. president. Bush was a U. S. president.

Looping: For loops

For loops also work for numeric items.

x = 2 multipliers = [1 ,2 ,3 ,4] for num in multipliers : print x * num

How could we make this code more efficient? Hint: we don’t need

to type out a list of sequential integers - there’s a function for that.

Nested Loops

Sometimes we will want to pair items from one list with items from

another list. We can nest for and/or while loops.

suits = [ ’ Spades ’ , ’ Clubs ’ , ’ Diamonds ’ , ’ Hearts ’] values = [ ’ Ace ’ , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , ’ Jack ’ , ’ Queen ’ , ’ King ’] for suit in suits : for value in values : print str ( value ) + " of " + str ( suit )

Notice that subsequent loops are additionally indented.

If Statement

Yet another way to execute commands conditional on a required

statement:

x = 2 if x == 1: print ’x is 1 ’

Notice that there is no output, because x is not 1.

Methods

Methods are object oriented functions. The functionality of an

object’s methods is dependent on what type of object we are

dealing with (i.e. a number, string, or list).

We access an object’s methods with the syntax:

object.method(arguments)

Note that a method is a function, so it takes arguments, but it is

associated with a particular object - hence the object.method

syntax.

Methods: Example

String objects have some useful methods available. The .count

method scans a string and counts the number of occurrences of

the argument passed to it.

topic = ’ Geographic Information Systems ’ topic. count ( " i " )

2

Note the object.method(argument) syntax. Here we passed the

argument ‘i’ to the count method and it returned the number of

times ‘i’ appeared in the string object called topic. Recall that

Python is case sensitive, so we don’t count the ‘I’ in Information.

Path Specification

Often we will need to specify the path/directory location of

shapefiles and other data sources - like C:\Python27\Lecture 2.

However, in standard Python backslash is reserved as an escape

character.

There are 3 standard recommendations for specifying a path:

folder = " C :/ Python27 / Lecture 2 " folder ’C :/ Python27 / Lecture 2 ’ folder = " C :\ Python27 \ Lecture 2 " folder ’C :\ Python27 \ Lecture 2 ’ folder = r " C :\ Python27 \ Lecture 2 " folder ’C :\ Python27 \ Lecture 2 ’

Modules!

So far, we have used the basic functions available to Python (from

the builtin module), but there are many more functions

available. (Recall that Python is ∞ expandable!)

We can access these other functions by using the import command

to bring them into the Python session and make them ‘active’.

The import statement uses the following syntax:

import module

Modules greatly expand the capabilities of Python processes.

Math Module

To utilize these functions we use the following syntax

module.function(arguments)

For example, consider the power function (math.pow), which takes

two arguments and raises the first argument to the power of the

second. In other words:

math.pow (2, 4) = 2^4 = 16

math. pow (2 ,4)

Math Module

Another way to access the functions of a module is to use the

sytax:

from module import *

from math import * pow (2 ,4)

This alternate syntax tells Python to make all of the functions

found in the math module available by name - no need to specify

math.functionname.

Random Examples

Some of the functions of the random module:

random. random () # Random float x , 0.0 <= x < 1.

random. uniform (1 , 10) # Random float x , 1.0 <= x < 10.

random. randint (1 , 10) # Random Integer from 1 to 10 2

random. choice ( ’ abcdefghij ’) # Choose a random element ’g ’

Random Shuffle and Sample

There are ways to randomly choose or arrange the items in a list.

A technique which is often used in a powerful statistical approach

called permutation sampling:

items = [1 , 2 , 3 , 4 , 5 , 6 , 7] random. shuffle ( items ) print items [2 , 3 , 5 , 6 , 1 , 7 , 4]

random. sample ( items ,3) # Choose 3 random elements from items [7 , 2 , 6]