(more) Python
Where we have been
Where we have been
And where we are going
Clojure Python Only values have types Only values have types Most values are immutable Some object- orientation, but mostly functional Mark-and-sweep garbage collection Only values have types Some values are mutable Object-oriented, but with functions as well Reference-counted garbage collection
C++ Python Variables and values have types Identifiers are values Most values are mutable Object-oriented, but with functions as well Scope-based destruction Only values have types Identifiers are references Some values are mutable Object-oriented, but with functions as well Reference-counted garbage collection
Python Types Integer 42 32-bit (at least) Long integer 42L Unlimited precision
Python Types Real numbers 64-bit double-precision floating point Generally you can check with sys.float_info
Python Types Complex float values for real and imaginary 1+2j
Python Types String Either type of quote is usable (as long as you're consistent) "A string" = 'A string' 'C' is not of type char Immutable
Python Types List Similar to Clojure vector L=[1, 2, 3, 4, 5] Mutable Slicing L[0:3] == L[:3] = [1, 2, 3] L[1:3] == [2, 3] L[-2:] = [4, 5] L[-1] == 5
Python Types Tuples Added to the language to provide an immutable version of the list T = (1, 2, 3, 4, 5) Special case for initializing a tuple of length one T1 = (1,) Because (1) = 1
Python Types Dictionary A hash table Keys must be hashable and immutable Mutable D = {'key1' : 'val1', 2 : [1, 2]} D['key1'] == 'val1' D[2] == [1, 2] Adding elements to the dictionary D['new-key'] = 'new-val'
Significant whitespace Python (like a few other languages) uses significant whitespace Bodies of code inside functions, conditional expressions, etc. must have the same indentation The line before a block ends in a colon
Conditionals x = 10 y = 20 if x < y: print("first condition was true") else: print("second condition was true")
Functions def function_name(arg1, arg2): print("arg1",arg1)
Classes class C: def __init__(self,val): self.val = val def get_val(self): return self.val
For loops Python doesn't provide a C-style for loop Instead, it provides something more like the 'range-based' for loop of C++11 for x in ['a', 'b', 'c']: print(x) for i in range(10): for x in {1:'one', 2:'two'}: print(x)
Collection Queries Collections can be treated as Booleans for the purpose of if and while statements when doing so, any non-empty collection evaluates to true L = ['a', 'b', 'c'] while L: print(L[0]) del L[0]
Collection Queries Containment is checked with in L = ['a', 'b', 'c'] if 'z' in L: print "I found a 'z'!" D= {1: 'one', 2: 'two'} if 'two' in D: print "D had a 'two' in it!"
Our old friends: map and filter Python has a built-in version of the map and filter that you wrote before the midterm
Map L = [1, 2, 3, 4, 5] L2 = map(lambda x: x*x, L) -> [2, 4, 6, 8, 10]
Filter L = [1, 2, 3, 4, 5] L2 = filter(lambda x: x%2==0 , L) -> [2, 4]
But don't do that! List comprehensions The scariest thing about them is the name
List Comprehensions [ <expression> for <var> in <collection>]
List Comprehensions: Map L2 = [x*x for x in L]
List Comprehensions: Filter L2 = [x for x in L if x%2==0]
List Comprehensions L = [1, 2, 3, 4, 5] L2 = [x*x for x in L if x%2==0]
List Comprehensions You can even use multiple variables [(x,y) for x in range(5) for y in range(5) if x<y]