Presentation is loading. Please wait.

Presentation is loading. Please wait.

(more) Python.

Similar presentations


Presentation on theme: "(more) Python."— Presentation transcript:

1 (more) Python

2 Where we have been

3 Where we have been

4 And where we are going

5 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

6 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

7 Python Types Integer 42 32-bit (at least) Long integer 42L
Unlimited precision

8 Python Types Real numbers 64-bit double-precision floating point
Generally you can check with sys.float_info

9 Python Types Complex float values for real and imaginary
1+2j

10 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

11 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

12 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

13 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'

14 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

15 Conditionals x = 10 y = 20 if x < y:
print("first condition was true") else: print("second condition was true")

16 Functions def function_name(arg1, arg2): print("arg1",arg1)

17 Classes class C: def __init__(self,val): self.val = val
def get_val(self): return self.val

18 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)

19 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]

20 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!"

21 Our old friends: map and filter
Python has a built-in version of the map and filter that you wrote before the midterm

22 Map L = [1, 2, 3, 4, 5] L2 = map(lambda x: x*x, L)
-> [2, 4, 6, 8, 10]

23 Filter L = [1, 2, 3, 4, 5] L2 = filter(lambda x: x%2==0 , L)
-> [2, 4]

24 But don't do that! List comprehensions
The scariest thing about them is the name

25 List Comprehensions [ <expression> for <var> in <collection>]

26 List Comprehensions: Map
L2 = [x*x for x in L]

27 List Comprehensions: Filter
L2 = [x for x in L if x%2==0]

28 List Comprehensions L = [1, 2, 3, 4, 5]
L2 = [x*x for x in L if x%2==0]

29 List Comprehensions You can even use multiple variables
[(x,y) for x in range(5) for y in range(5) if x<y]


Download ppt "(more) Python."

Similar presentations


Ads by Google