Download presentation
Presentation is loading. Please wait.
Published byAlban Elliott Modified over 9 years ago
1
Python, Part 2
2
Python Object Equality x == y x is y In Java: (x.equals(y)) (x == y)
3
Python Exceptions try: do_stuff() if something_bad_has_happened(): raise MyException, "something happened" except MyException, e: print "My exception occurred, value: ", e.value
4
Python Exceptions while True: try: x = int(raw_input("Please enter a number: ")) except ValueError: print "That was no valid number. Try again."
5
Functional programming features: map lst = ["1", "2", "3", "4", "5"] nums = map(string.atoi, lst) # nums is now [1, 2, 3, 4, 5]
6
map def add(x, y): return x + y lst1 = [1, 2, 3, 4, 5] lst2 = [6, 7, 8, 9, 10] lst_sum = map(add, lst1, lst2) # lst_sum == [7, 9, 11, 13, 15]
7
reduce def add(x, y): return x + y lst1 = [1, 2, 3, 4, 5] sum = reduce(add, lst1) # sum == 15
8
filter nums = range(0,101) # [0, 1,... 100] def is_odd(x): return x % 2 == 1 odd_nums = filter(is_odd, nums) # odd_nums == [1, 3, 5,... 99]
9
Lambda: Anonymous functions t = lambda x,y: x*y def t: return x*y answer = t(2,3)
10
Lambda Useful for map, reduce, filter: nums = range(0,101) # [0, 1,... 100] odd_nums = filter(lambda x: x%2==1, nums) # odd_nums == [1, 3, 5,... 99]
11
Pass Comparators to Sort: #return data which has groups of a name followed by 2 scores. def get_data(): return [('fred',10,1),('bill',3,0),('betty',5,8)] a=get_data() a.sort(lambda x, y: cmp(x[2],y[2])) print a >>>[('bill', 3, 0), ('fred', 10, 1), ('betty', 5, 8)]
12
Each “file” is a module. Consider module called “hello.py”: def printer(message): print message In module main.py write: import hello hello.printer(“hello”) -OR- from hello import printer printer(“hello”) from hello import *
13
Main Module What about interactive session? Is it a module?? Yes: __main__ Modules executed on import. In many modules you will see: if __name__ == "__main__": main()
14
Python Classes: Attributes can be added/deleted dynamically (no declaring) Keyword self (like this) passed into all class methods Special methods __init__ (Constructor) __del__ (Destructor)
15
class Student: def __init__(self, name, hours, qpts): self.name = name self.hours = hours self.qpts = qpts def getName(self): return self.name def gpa(self): return self.qpts/self.hours
16
from student import * aStudent = Student(“Mary Adams”, 127, 228) gpa = aStudent.gpa() print gpa s = Student(…) calls the constructor. To call the destructor: s = 0
17
Creating a list the old way… >>> squares = [] >>> for x in range(10): squares.append(x**2) >>> squares [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
18
List Comprehensions >>> squares = [] >>> for x in range(10): squares.append(x**2) >>> squares [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] >>>squares = [x**2 for x in range(10)]
19
List Comprehensions >>> vec = [-4, -2, 0, 2, 4] >>> [x*2 for x in vec]
20
List Comprehensions >>> vec = [-4, -2, 0, 2, 4] >>> [x*2 for x in vec] [-8, -4, 0, 4, 8]
21
>>> vec = [-4, -2, 0, 2, 4] >>> [x for x in vec if x >= 0]
22
>>> vec = [-4, -2, 0, 2, 4] >>> [x for x in vec if x >= 0] [0, 2, 4]
23
Can do embedded loops too >>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
24
Can do embedded loops too >>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.