Embed presentation
Download as PDF, PPTX










![On Linux or Mac● Python is built-in on Linux or Mac.● All you have to do is check the version. Type "python" in any terminal. Python 2.7.3 (default, Sep 26 2012, 21:51:14) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 11](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-11-2048.jpg&f=jpg&w=240)






![hello.py (cont.)if __name__ == '__main__': ● __name__, the name of module. import sys if len(sys.argv) >= 2: ● import is important. print The usage:hello(sys.argv[1]) else: ● import sys print hello() ● from sys import argv ● … as alias 18](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-18-2048.jpg&f=jpg&w=240)




![Common Types● Numeric ● Sequence – Integer 100 – String "" – Float 10.0 – Unicode u"" – Long 100L – List [,] – Complex 1+1j – Tuple (,) – Boolean True, False 23](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-23-2048.jpg&f=jpg&w=240)













![List and TupleList (mutable seq.) Tuple (seq.)– [] – tuple()– ['item'] – ('item', )– ['s', 100, u'unicode'] – ('s', 100, u'unicode')– list('abc') – tuple('abc')– 'a b c'.split(' ')– 'n'.join(['spam', – 'n'.join(('spam', 'eggs']) 'eggs'))– x, y = [1, 2] – x, y = (1, 2)– x, y = [y, x] – x, y = (y, x) 37](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-37-2048.jpg&f=jpg&w=240)
![SequenceSequence Mutable Seq. – s[i] = x– x in s # performance? – s[i:j] = t– x not in s – del s[i:j]– s + t – s[i:j:k] = t– s * n, n * s – s.append(x) – s.insert(i, x)– s[i] – s.pop([i])– s[i:j] – s.remove(x) # performance?– s[i:j:k] – s.extend(t) in-place– len(s) – s.sort([cmp[, key[, reverse]]])– s.index(x) – s.sort([key[, reverse]]) # Py 3– s.count(x) – s.reverse() 38](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-38-2048.jpg&f=jpg&w=240)
![Sequence Comparison● (0, 0, 0) < (0, 0, 1)● [0, 0, 0] < [0, 0, 1]● (0, ) < (0, 0)● 'ABC' < 'C' < 'Pascal' < 'Python'● (1, 2, 3) == (1.0, 2.0, 3.0)● 'A' == 'A'● 'A' > 65● 'A' > 66● ('A', ) > (66, ) 39](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-39-2048.jpg&f=jpg&w=240)
![Sequence Comparison in Python 3● (0, 0, 0) < (0, 0, 1)● [0, 0, 0] < [0, 0, 1]● (0, ) < (0, 0)● 'ABC' < 'C' < 'Pascal' < 'Python'● (1, 2, 3) == (1.0, 2.0, 3.0)● 'A' == 'A'● 'A' > 65 → TypeError● 'A' > 66 → TypeError● ('A', ) > (66, ) → TypeError 40](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-40-2048.jpg&f=jpg&w=240)
![Sequence (cont.)Slicing and Slice object:– s = range(10) – s = 'I am a str.'– t = s – s[:-3]– t[0] = 'A'– print s – s.reverse()– t is s → TypeError – s[::-1]– t = s[:] – ''.join(reversed(s))– t is s – slice(None, None, -1) 41](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-41-2048.jpg&f=jpg&w=240)
![MappingDict. (mutable map.) – len(d)– {} – d[k]– {'A ': 1, 'B': 2, 'C': 3} – d[k] = v– dict({...}) – del d[k]– dict(A=1, B=2, C=3) – k in d, k not in d – d.copy() – d.get(key[, default])– k = 'ABC' – d.setdefault(key[, default])– v = [1, 2, 3] – d.items(), d.keys(), d.values()– pairs = zip(k, v) – d.pop(key[, default)– dict(pairs) – d.update([other]) ... 42](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-42-2048.jpg&f=jpg&w=240)
![SetSet (mutable set) – len(s)– set() – x in s, x not in s– {'A', 'B', 'C'} # Py3 – s.copy() – s.add(elem)– set('ABC') – s.discard(elem)– set(['A','B','C']) – s.pop() – s |= other – s &= other – s | other | ... – s & other & ... – s < | <= | == | > = | > other ... 43](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-43-2048.jpg&f=jpg&w=240)

![The if Statementif [condition 1]: …elif [condition 2]: …elif [condition 3]: …else: …[exp. if true] if [condition] else [exp. if false] 45](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-45-2048.jpg&f=jpg&w=240)
![Truth Value TestingThey are same as False in a boolean context:– None– False– Zeros (ex. 0, 0.0, 0L, 0j)– Empty containers (ex. '', [], {})– __nonzero__() or __len__() returns 0 or False 46](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-46-2048.jpg&f=jpg&w=240)
![Truth Value Testing (cont.)● if not None: ...● if not []: ...● if [0]: ...● if [[]]: ...● if "": ...● if {}: ...● if not {0: False}: … … 47](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-47-2048.jpg&f=jpg&w=240)
![The for Statementfor [item] in [iterable]: for i in [0, 1, 2]: … print ifor i in range(3): for i in xrange(3): print i print i 48](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-48-2048.jpg&f=jpg&w=240)
![The for Statement in Python 3for [item] in [iterable]: for i in [0, 1, 2]: … print ifor i in range(3): for i in xrange(3): print i print i 49](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-49-2048.jpg&f=jpg&w=240)
![The for Statement (cont.)for i in range(1, 3): for i in range(3, -1, -1): print i print is = [1, 2, 3] s = [...]t = 'xyz' for i, item in enumerate(s): print i, itemfor i, j in zip(s, t): print i, j 50](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-50-2048.jpg&f=jpg&w=240)



![Challenge 2-2: Collect the Chars● Use for loop to collect "Here are UPPERCASE the chars. and lowercase chars." – limit: use setdefault {'c': ['C', 'c', 'c'], ...} 54](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-54-2048.jpg&f=jpg&w=240)
![The while Statementtasks = [...]while tasks: while 1: … …● It leaves the loop once ● A infinite loop. the tasks is empty. ● It is better to use block mechanism in a loop. – ex. I/O block 55](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-55-2048.jpg&f=jpg&w=240)




![Challenge 3-1: The Primes● Try to filter the primes [2, 3, 5, 7, 11, 13, from [2, 100). 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, – without limit. 59, 61, 67, 71, 73, – limit: use loop's else 79, 83, 89, 97] 60](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-60-2048.jpg&f=jpg&w=240)








![The def Statement (cont.) def f(): pass def g(): pass d = {'x': f, 'y': g} d['x']()● Python functions are first-class functions. – It means you can pass functions as arguments, and assign functions to variables. – It is like the function pointers in C. 69](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-69-2048.jpg&f=jpg&w=240)

![A Trap of the Default Value# file: ex_defval_trap.py ● Because the list is created when thedef f(items=[]): function is defined. items.append(1) return items ● Avoid to use the mutable types as theif __name__ == '__main__': default value. print f() # -> [1] print f() # -> [1, 1] print f() # -> [1, 1, 1] 71](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-71-2048.jpg&f=jpg&w=240)





![The csv Moudle#!/usr/bin/env python 1, apple# -*- coding: utf-8 -*- 2, orange# file: ex_csv.py 3, watermelonimport csv ['1', ' apple']with open('ex_csv.csv') as f: ['2', ' orange'] for row in csv.reader(f): print row ['3', ' watermelon'] 77](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-77-2048.jpg&f=jpg&w=240)
![The os.path Moudle# file: ex_os_path.py $ python ex_os_path.pyfrom os import walkfrom os.path import join It requires a path asdef list_files(path): paths = [] argument. for root, dir_names, file_names in walk(path): for file_name in file_names: paths.append(join(root, file_name)) $ python ex_os_path.py . return paths …/1if __name__ == '__main__': import sys …/b/4 from os.path import abspath, dirname if len(sys.argv) == 2: …/a/2 path = abspath(dirname(sys.argv[1])) for path in list_files(path): …/a/3 print path else: print 'It requires a path as argument.' 78](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-78-2048.jpg&f=jpg&w=240)





![The LEGB Rule# file: ex_LEGB.py ● return …global_var = 100 – Local (in function)def f(): enclosed_var = 10 – Enclosed def g(): – Global local_var = 1 return sum([local_var, enclosed_var, – Built-inglobal_var]) return g()if __name__ == '__main__': print f() # -> 111 84](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-84-2048.jpg&f=jpg&w=240)
![Challenge 3-2: The Primes (cont.)– limit 1: Sieve of [2, 3, 5, 7, 11, 13, Eratosthenes. 17, 19, 23, 29, 31,– limit 2: use set. 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] 85](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-85-2048.jpg&f=jpg&w=240)



s[:] makes a copy.– s[:]– slice(0, 3)– s[slice(0, 3)]– s[0:3]– s[:3]– s[3:]– s[:-3]So slice is more flexible.– s[start:stop:step]– s[::-1] # reverse– help(slice)– s = 'Hello World'– s[5:11:2]– 'oWl'– s[::2]– 'Hlo rd'– s[-










![On Linux or Mac● Python is built-in on Linux or Mac.● All you have to do is check the version. Type "python" in any terminal. Python 2.7.3 (default, Sep 26 2012, 21:51:14) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 11](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-11-2048.jpg&f=jpg&w=240)






![hello.py (cont.)if __name__ == '__main__': ● __name__, the name of module. import sys if len(sys.argv) >= 2: ● import is important. print The usage:hello(sys.argv[1]) else: ● import sys print hello() ● from sys import argv ● … as alias 18](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-18-2048.jpg&f=jpg&w=240)




![Common Types● Numeric ● Sequence – Integer 100 – String "" – Float 10.0 – Unicode u"" – Long 100L – List [,] – Complex 1+1j – Tuple (,) – Boolean True, False 23](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-23-2048.jpg&f=jpg&w=240)













![List and TupleList (mutable seq.) Tuple (seq.)– [] – tuple()– ['item'] – ('item', )– ['s', 100, u'unicode'] – ('s', 100, u'unicode')– list('abc') – tuple('abc')– 'a b c'.split(' ')– 'n'.join(['spam', – 'n'.join(('spam', 'eggs']) 'eggs'))– x, y = [1, 2] – x, y = (1, 2)– x, y = [y, x] – x, y = (y, x) 37](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-37-2048.jpg&f=jpg&w=240)
![SequenceSequence Mutable Seq. – s[i] = x– x in s # performance? – s[i:j] = t– x not in s – del s[i:j]– s + t – s[i:j:k] = t– s * n, n * s – s.append(x) – s.insert(i, x)– s[i] – s.pop([i])– s[i:j] – s.remove(x) # performance?– s[i:j:k] – s.extend(t) in-place– len(s) – s.sort([cmp[, key[, reverse]]])– s.index(x) – s.sort([key[, reverse]]) # Py 3– s.count(x) – s.reverse() 38](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-38-2048.jpg&f=jpg&w=240)
![Sequence Comparison● (0, 0, 0) < (0, 0, 1)● [0, 0, 0] < [0, 0, 1]● (0, ) < (0, 0)● 'ABC' < 'C' < 'Pascal' < 'Python'● (1, 2, 3) == (1.0, 2.0, 3.0)● 'A' == 'A'● 'A' > 65● 'A' > 66● ('A', ) > (66, ) 39](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-39-2048.jpg&f=jpg&w=240)
![Sequence Comparison in Python 3● (0, 0, 0) < (0, 0, 1)● [0, 0, 0] < [0, 0, 1]● (0, ) < (0, 0)● 'ABC' < 'C' < 'Pascal' < 'Python'● (1, 2, 3) == (1.0, 2.0, 3.0)● 'A' == 'A'● 'A' > 65 → TypeError● 'A' > 66 → TypeError● ('A', ) > (66, ) → TypeError 40](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-40-2048.jpg&f=jpg&w=240)
![Sequence (cont.)Slicing and Slice object:– s = range(10) – s = 'I am a str.'– t = s – s[:-3]– t[0] = 'A'– print s – s.reverse()– t is s → TypeError – s[::-1]– t = s[:] – ''.join(reversed(s))– t is s – slice(None, None, -1) 41](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-41-2048.jpg&f=jpg&w=240)
![MappingDict. (mutable map.) – len(d)– {} – d[k]– {'A ': 1, 'B': 2, 'C': 3} – d[k] = v– dict({...}) – del d[k]– dict(A=1, B=2, C=3) – k in d, k not in d – d.copy() – d.get(key[, default])– k = 'ABC' – d.setdefault(key[, default])– v = [1, 2, 3] – d.items(), d.keys(), d.values()– pairs = zip(k, v) – d.pop(key[, default)– dict(pairs) – d.update([other]) ... 42](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-42-2048.jpg&f=jpg&w=240)
![SetSet (mutable set) – len(s)– set() – x in s, x not in s– {'A', 'B', 'C'} # Py3 – s.copy() – s.add(elem)– set('ABC') – s.discard(elem)– set(['A','B','C']) – s.pop() – s |= other – s &= other – s | other | ... – s & other & ... – s < | <= | == | > = | > other ... 43](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-43-2048.jpg&f=jpg&w=240)

![The if Statementif [condition 1]: …elif [condition 2]: …elif [condition 3]: …else: …[exp. if true] if [condition] else [exp. if false] 45](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-45-2048.jpg&f=jpg&w=240)
![Truth Value TestingThey are same as False in a boolean context:– None– False– Zeros (ex. 0, 0.0, 0L, 0j)– Empty containers (ex. '', [], {})– __nonzero__() or __len__() returns 0 or False 46](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-46-2048.jpg&f=jpg&w=240)
![Truth Value Testing (cont.)● if not None: ...● if not []: ...● if [0]: ...● if [[]]: ...● if "": ...● if {}: ...● if not {0: False}: … … 47](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-47-2048.jpg&f=jpg&w=240)
![The for Statementfor [item] in [iterable]: for i in [0, 1, 2]: … print ifor i in range(3): for i in xrange(3): print i print i 48](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-48-2048.jpg&f=jpg&w=240)
![The for Statement in Python 3for [item] in [iterable]: for i in [0, 1, 2]: … print ifor i in range(3): for i in xrange(3): print i print i 49](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-49-2048.jpg&f=jpg&w=240)
![The for Statement (cont.)for i in range(1, 3): for i in range(3, -1, -1): print i print is = [1, 2, 3] s = [...]t = 'xyz' for i, item in enumerate(s): print i, itemfor i, j in zip(s, t): print i, j 50](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-50-2048.jpg&f=jpg&w=240)



![Challenge 2-2: Collect the Chars● Use for loop to collect "Here are UPPERCASE the chars. and lowercase chars." – limit: use setdefault {'c': ['C', 'c', 'c'], ...} 54](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-54-2048.jpg&f=jpg&w=240)
![The while Statementtasks = [...]while tasks: while 1: … …● It leaves the loop once ● A infinite loop. the tasks is empty. ● It is better to use block mechanism in a loop. – ex. I/O block 55](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-55-2048.jpg&f=jpg&w=240)




![Challenge 3-1: The Primes● Try to filter the primes [2, 3, 5, 7, 11, 13, from [2, 100). 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, – without limit. 59, 61, 67, 71, 73, – limit: use loop's else 79, 83, 89, 97] 60](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-60-2048.jpg&f=jpg&w=240)








![The def Statement (cont.) def f(): pass def g(): pass d = {'x': f, 'y': g} d['x']()● Python functions are first-class functions. – It means you can pass functions as arguments, and assign functions to variables. – It is like the function pointers in C. 69](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-69-2048.jpg&f=jpg&w=240)

![A Trap of the Default Value# file: ex_defval_trap.py ● Because the list is created when thedef f(items=[]): function is defined. items.append(1) return items ● Avoid to use the mutable types as theif __name__ == '__main__': default value. print f() # -> [1] print f() # -> [1, 1] print f() # -> [1, 1, 1] 71](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-71-2048.jpg&f=jpg&w=240)





![The csv Moudle#!/usr/bin/env python 1, apple# -*- coding: utf-8 -*- 2, orange# file: ex_csv.py 3, watermelonimport csv ['1', ' apple']with open('ex_csv.csv') as f: ['2', ' orange'] for row in csv.reader(f): print row ['3', ' watermelon'] 77](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-77-2048.jpg&f=jpg&w=240)
![The os.path Moudle# file: ex_os_path.py $ python ex_os_path.pyfrom os import walkfrom os.path import join It requires a path asdef list_files(path): paths = [] argument. for root, dir_names, file_names in walk(path): for file_name in file_names: paths.append(join(root, file_name)) $ python ex_os_path.py . return paths …/1if __name__ == '__main__': import sys …/b/4 from os.path import abspath, dirname if len(sys.argv) == 2: …/a/2 path = abspath(dirname(sys.argv[1])) for path in list_files(path): …/a/3 print path else: print 'It requires a path as argument.' 78](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-78-2048.jpg&f=jpg&w=240)





![The LEGB Rule# file: ex_LEGB.py ● return …global_var = 100 – Local (in function)def f(): enclosed_var = 10 – Enclosed def g(): – Global local_var = 1 return sum([local_var, enclosed_var, – Built-inglobal_var]) return g()if __name__ == '__main__': print f() # -> 111 84](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-84-2048.jpg&f=jpg&w=240)
![Challenge 3-2: The Primes (cont.)– limit 1: Sieve of [2, 3, 5, 7, 11, 13, Eratosthenes. 17, 19, 23, 29, 31,– limit 2: use set. 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] 85](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fprogramming-with-python-basic-130312114054-phpapp01%2f75%2fProgramming-with-Python-Basic-85-2048.jpg&f=jpg&w=240)

