Show Menu
Cheatography

Intro to Python Programming for ArcGIS Cheat Sheet (DRAFT) by

Midterm cheat sheet

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Python Data Types - Storage Types

Integer
-100, 100
Float
-100.123, 100e-10
String
"­Hello, World"
Boolean
True, False
List
[1,2,3, you, and, me]
Tuple
(peter, 1, paul, 2, and, mary, 3)1
Dictionary
{key, value, ...}
1 Parent­hesis optional in Python 2, required in 3

Data Types Questions

How do they function?
Do they operate in sequence?
Lists, Tuples, and Strings are sequences. Can be combined, queried, indexed, and sliced.
Are they mutable?
Strings, numbers, and tuples are immutable. Lists and dictio­naries are mutable.

Operators

Modulus
Remainder after division. Uses %.
Expone­nti­ation
Exponents. ** (5^3)
no parent­hesis around asterisks

Rules

Variable
Must start with a letter or an underscore (but can be empty 0, "­"). Remainder can consist of letters, numbers, and unders­cores. They are CASE SENSITIVE.
String

Condit­ionals

How do they work?
True/false statements
What is the syntax?
IF/ELIF statement

Iterators

How do they work?
Loops over a list
What is the syntax?
FOR

Comput­ational Thinking

Psuedo code
brain in slides

Concepts and Benefits of

flow charting
modelb­uilder
 

Nature of Python

High Level
Removed from actual code ran on computer. Use syntax to make it easier to read/w­rite.
Object Oriented
Each object has a value, an identity, and a type.
General Purpose
Does not have a specific applic­ation, can be used for a range of applic­ations like scripting or automating
Interp­reted
Processed through an interp­reter into a low level machine language that can be executed
Dynamic
Data can be manipu­lated into strings and numbers. An integer can be a float or vice versa.

Python Version Differ­ences

Print: In Python 2, “print” is treated as a statement rather than a function. There is no need to wrap the text you want to print in parent­heses, although you can if you want. In contrast, Python 3 explicitly treats “print” as a function, which means you have to pass the items you need to print to the function in parent­heses in the standard way, or you will get a syntax error.
Integer Division: Python 2 treats numbers that you type without any digits after the decimal point as integers. For example, if you type the expression 3 / 2 in Python 2 code, the result of the evaluation will be 1, not 1.5 as you might expect. Python 3 evaluates 3 / 2 as 1.5 by default, which is more intuitive for new progra­mmers.
Impact on ArcPY

Python Libraries

What are they?
A Python Library is a collection of functions and methods that allow you to perform actions without writing code.
ArcPy
Arcpy is a way to perform geographic data analysis, data conver­sion, data manage­ment, and map automation with Python.

Python Range Function

>>> # One parameter
>>> for i in range(5):
...     print(i)
... 
0
1
2
3
4
>>> # Two parameters
>>> for i in range(3, 6):
...     print(i)
... 
3
4
5
>>> # Three parameters
>>> for i in range(4, 10, 2):
...     print(i)
... 
4
6
8
>>> # Going backwards
>>> for i in range(0, -10, -2):
...     print(i)
... 
0
-2
-4
-6
-8
Generates a list of numbers.
 

Structure of a FOR loop

for x in [1,2,3]:
	print(x)
-
tacos = ["Fish Taco", "Brisket Taco", "Carnitas Taco"]
for tacos in tacos:
	print(tacos)

Structure of a WHILE loop

i=1
while i < 4:
	print i
	i+=1
-
ball = 10
while ball > 0:
	print(ball)
	print("The ball is dropping!")
	break

i = 10
while i >= 6:
	print(i)
	i-=1
	print("Keep going")
while i == 5:
	print(i)
	i-=1
	print("Halfway there")
while i >= 0:
	print(i)
	i -= 10
	print("almost there")
else:
	print(i)
	i-=1
	print("finished")

Break/­Con­tin­ue/Pass

Break
Terminates the current loop and resumes at the next statement
Continue
Returns the control to the beginning of the while loop. Rejects all the remaining statements in the current iteration of hte loop and moves control back to the top of the loop.
Pass
Pass for comman­d/codes not wanted to be executed.

Structure of a IF/ELI­F/ELSE code blocks

import random
p = random.randint (1, 6)
print p

if p == 6:
	print "shante you stay!"
elif p == 5:
	print "Shante you both stay"
else:
	print "sashe away"