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 Parenthesis 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 dictionaries are mutable. |
Operators
Modulus |
Remainder after division. Uses %. |
Exponentiation |
Exponents. ** (5^3) |
no parenthesis around asterisks
Rules
Variable |
Must start with a letter or an underscore (but can be empty 0, ""). Remainder can consist of letters, numbers, and underscores. They are CASE SENSITIVE. |
String |
Conditionals
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 |
Concepts and Benefits of
flow charting |
modelbuilder |
|
|
Nature of Python
High Level |
Removed from actual code ran on computer. Use syntax to make it easier to read/write. |
Object Oriented |
Each object has a value, an identity, and a type. |
General Purpose |
Does not have a specific application, can be used for a range of applications like scripting or automating |
Interpreted |
Processed through an interpreter into a low level machine language that can be executed |
Dynamic |
Data can be manipulated into strings and numbers. An integer can be a float or vice versa. |
Python Version Differences
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 parentheses, 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 parentheses 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 programmers. |
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 conversion, data management, 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/Continue/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 command/codes not wanted to be executed. |
Structure of a IF/ELIF/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"
|
|