On Github tayden / python-tutorial
# String person = "Douglas Adams" #Integer a = 3 #Float b = 6.0
# LIST a = [1, 2, 3, "dog"] # these are mutable a[0] = 9 #[9, 2, 3, "dog"] # TUPLE b = (1, "fish", 9.1) # these are immutable b[1] = "pony" # Error!
person = {name: "Rob", profession: "Delivery guy"}
Accessed by key
print person.name print person['name'] output>> "Rob" output>> "Rob"
Types can be combined to help represent more complex data structures
Common for representing matrices
matrix_keanu = [ [0,0,0], [0,0,1], [0,1,0], [0,1,1] ]
... can also be written
matrix_keanu = [[0,0,0], [0,0,1], [0,1,0], [0,1,1]]
Common for representing tabular data
powerrangers = [ {color: "Red", zord_rating: 7 }, {color: "Black", zord_rating: 8 }, {color: "Pink", zord_rating: 9 }, {color: "Blue", zord_rating: 2 } ]
for n in [1, 2, 3, 4, 5, 6]: print n output>> 1 output>> 2 ... output>> 6
syntax:
for [var-name] in [iterable]: [Do something with var-name]
iterables are typically lists or tuples.
You can do anthing you want in the [Do something] part of the loop, including call more loops
quads = [ [1,2,3,4], [5,6,7,8] ] for item in quads: # item is [1,2,4,8], then [1,3,9,27]. for n in item: print n*2, # trailing comma makes print not add a newline print ", ", print # print a newline output>> 2, 4, 6, 8, output>> 10, 12, 14, 16,
i = 3 while(i > 0) print i i -= 2 output>> 3 output>> 1
A compact for loop for working with lists/tuples
numbers = [1,2,3,4,5,6,7,8,9,10] odds = [n for n in numbers if n%2 == 1] print odds output>> [1,3,5,7,9]
Used to execute code if a condition is met
a = 2 if a > 2: print "a is greater than 2" elif a == 4: print "a is 4" # can add more elif statements here if desired else: # All tests failed print "there is no 'truth' in the world" output>> "there is no 'truth' in the world"
powerrangers = [ {color: "Red", zord_rating: 7 }, {color: "Black", zord_rating: 8 }, {color: "Pink", zord_rating: 9 }, {color: "Blue", zord_rating: 2 } ] for ranger in powerrangers: if ranger.zord_rating > 7: print ranger.color outputs>> "Black" outputs>> "Pint"
Functions abstract away complex operations and prevent rewriting code you've already written.
# add two numbers and print the result def add(a, b): print a+b # call function add(1, 2) output>> 3
Reusable functions typically return a value so it can then be used as the input to another function
# add two numbers and print the result def add(a, b): return a+b # call function result = add(1, 1) result = add(1, result) print result output>> 3
A function may have a default value like This
# add two numbers and print the result def add(a, b=10): return a+b # call function print add(1) print add(1, 2) output>> 11 output>> 3
A definition of an object - something that contains attributes and functions to abstract away complexity
class Person: def __init__(self, name) # A special function that creates new objects self.name = name def nameStartsWithT(self): return upper(self.name[0]) == "T" # Create object from class definition me = Person("Taylor") # Call Class function on object me.nameStartsWithT() # Returns True
class Restaurant: def __init__(self, name, capacity): self.name = name self.capacity = capacity self.currentDiners = 0 def addTable(self, patrons): self.currentDiners += patrons def atCapacity(self): self.currentDiners >= self.capacity
In Python, everything is an object
This means that pretty much everything has some inherent functions attached
# String functions print "fish, dog, pony".split(",") output>> ["fish", "dog", "pony"] # Dict functions some_dict = {person:"Guido", profession:"Programmer"} print some_dict.keys() output>> ["person", "profession"] print some_dict.values() output>> ["Guido", "programmer"]
and many more!
"Application programming interface"
class Polygon(exterior[, interiors=None])
from shapely.geometry import Polygon polygon = Polygon([(0, 0), (1, 1), (1, 0)]) polygon.area output>> 0.5
From Carson Farmer's website: