Python 101 - Session 2

  • Built-in Data Structures
  • Intro to PythonWin
  • Control Flow Statements

* Prep

  • Ensure that PythonWin is installed on your virtual machine; See ArcGIS Desktop Post-Install
  • Update your WhirlwindTourOfPython repository or copy the Scripts files to your local workspace

Built-In Data Structures

Source

  • Lists
  • Tuples
  • Dictionaries
  • Sets

Lists

  • Ordered and mutable collection
    • Resembles a vector
    • Items in a list can different data types
  • Created using square brackets
    myList = [1,2,"Apple"]
  • List arithmetic
  • Indexing a slicing to get items - zero based
  • Functions to manipulate: use tab-complete or help

Tuples

  • Like a list, but immutable
  • Created using parentheses, or not:
    myTuple = (1, 2, "Apple") or myTuple = 1,2,"Apple"
  • Cannot add, remove, or rearrange items.
  • "Modify" by creating a new tuple:
    myTuple += (4, 5, True)

Dictionaries

  • A collection of unordered objects, like a list, but items referred to by a 'key', not an index
  • Created using curly braces with key/value pairs: playerCount'= {'Volleyball': 6, 'Baseball': 9}
  • Items retrieved by its key:
    x = playerCount['Volleyball']
  • Items can be updated:
    playerCount['Volleyball'] = 2
  • New items can be added"
    playerCount['Soccer'] = 11
  • Dictionaries have functions...

Sets

  • Collection of unordered, unique objects
  • Created using curly braces
    primes = {2, 3, 5, 7}
  • Can perform set functions: union, intersection, difference, symmetric difference...

Introduction to PythonWin

See Getting-Started-with-PythonWin.html

  • What is an Integrated Development Environment IDE?
  • What's what in PythonWin
  • Customizing PythonWin
  • Editing, debugging, and saving scripts

Control Flow Statements

Source

  • Conditional Statements
  • for loops
  • while loops
  • break and continue

for loops

  • for x in myList:
  • Repeats indented code block for each item in a collection (list, tuple, set)
  • The variable after for is available in the code block and changes with each iteration of the loop
  • Loop ends when the last item is processed, and code resumes to next dedented line
  • Use the range() function to create a simple list to loop a set number of times

In [ ]:
#ForLoopExample.py

# This example uses a for loop to iterate through each item in 
# the "fruit" list, updating the value of the "fruit" variable and 
# executing whatever lines are indented under the for statement

#Create a list of fruit
fruitList = ("apples","oranges","kiwi","grapes","blueberries") 

# Loop through each item in the tuple and execute
# each line that is indented under the for loop
for fruit in fruitList:
    print "I like to eat " + fruit

# Dedented lines are run after the loop completes
print "\nI like ice cream too..."

the range() function


In [ ]:
print range(10)

In [ ]:
print range(10,100)

In [ ]:
print range(10,100,20)

for loop with the range() function


In [ ]:
#RangeFunctionExample.py

# This example demonstrates the range function for generating 
# a sequence of values that can be used in a for loop.

pi = 3.1415

for r in range(0,100,20):
    area = (r ** 2) * pi
    print "The area of a circle with radius ", r, "is ", area

while loops

  • while i < 10:
  • Repeats indented code until the expression is no longer true
    • Value in the expression must change; possibility for an infinite loop

In [ ]:
#WhileLoopExample.py

# This example demonstrates how a while loop is used. Here, we
# calculate the area of several circle with a radius 'r'. We loop
# through gradually larger values of r until the area of the circle
# exceeds 1000.

pi = 3.1415
r = 1
area = (r ** 2) * pi

while area < 1000:
    print r, area          # Indentation indicates what's run in the loop
    r = r + 1              # The variable that gets evaluated must change in the
    area = (r ** 2) * pi   # loop otherwise you'll create an infinite loop!

print "The while loop is done"  # Dedented lines run after the loop completes

break and continue: Fine-Tuning Your Loops

  • The break statement breaks-out of the loop entirely
  • The continue statement skips the remainder of the current loop, and goes to the next iteration