This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| a_list = [1 for i in range(10000000) ] | |
| a_generator = (1 for i in range(10000000) ) | |
| sys.getsizeof(a_list) # 81528064 Bytes | |
| sys.getsizeof(a_generator) # 80 Bytes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| gen = (1 for _ in range(1000) ) | |
| # iterating manually | |
| while True: | |
| try: | |
| my_value = gen.next() | |
| do_stuff_with(my_value) | |
| except: | |
| break |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| with open('big.csv') as f: | |
| for line in f: | |
| process(line) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # regular, imperative declaration. | |
| def twice(x): | |
| return 2*x | |
| twice(5) # 10 | |
| # declaration assigning a lambda | |
| #(bad practice, don't try this at home!) | |
| twice = lambda x: 2*x | |
| twice(x) #10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def triple(a): | |
| return 3*a | |
| thrice = lambda x: 3*x | |
| these = [triple(i) for i in range(5) ] | |
| are = [(lambda x: 3*x)(i) for i in range(5) ] | |
| all = [thrice(i) for i in range(5) ] | |
| the = map(thrice, range(5)) | |
| same = map(triple, range(5)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| some_list = list(range(5)) | |
| only_evens = filter(lambda x: x%2 == 0, some_list) | |
| only_evens_list = [x for x in some_list if x%2==0] | |
| # list(only_evens) == only_evens_list |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import time | |
| BIG = 20000000 | |
| def f(k): | |
| return 2*k | |
| def benchmark(function, function_name): | |
| start = time.time() | |
| function() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def list_a(): | |
| list_a = [] | |
| for i in range(BIG): | |
| list_a.append(f(i)) | |
| def list_b(): | |
| list_b = [f(i) for i in range(BIG)] | |
| def list_c(): | |
| list_c = map(f, range(BIG)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def f(x): | |
| return 2*x | |
| a = list( map(f,[1,2,3]) ) | |
| b = [] | |
| for i in [1,2,3]: | |
| b.append(f(i)) | |
| a == b #True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def imperative_linear_search(some_list,some_element): | |
| for e in some_list: | |
| if e == some_element: | |
| return True | |
| return False | |
| def recursive_linear_search(some_list,some_element): | |
| if some_list == []: | |
| return False | |
| elif some_list[0]==some_element: #first element always exists at this point |