Upcoming SlideShare
×

# Python & Perl: Lecture 10

971
-1

Published on

Published in: Technology, Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
971
On Slideshare
0
From Embeds
0
Number of Embeds
20
Actions
Shares
0
0
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Python & Perl: Lecture 10

1. 1. Python & Perl Lecture 10 Vladimir Kulyukin Department of Computer Science Utah State Universitywww.youtube.com/vkedco www.vkedco.blogspot.com
2. 2. Outline ● List Comprehension with Nested For-Loops ● List Comprehension with Matrices ● del, pass, exec()www.youtube.com/vkedco www.vkedco.blogspot.com
3. 3. List Comprehension with For-Loopswww.youtube.com/vkedco www.vkedco.blogspot.com
4. 4. For-Loop >>> rslt = [] >>> for x in xrange(6): if x % 2 == 0: for y in xrange(6): if y % 2 != 0: rslt.append((x, y)) >>> rslt [(0, 1), (0, 3), (0, 5), (2, 1), (2, 3), (2, 5), (4, 1), (4, 3), (4, 5)]www.youtube.com/vkedco www.vkedco.blogspot.com
5. 5. List Comprehension Equivalent >>> [(x, y) for x in xrange(6) if x % 2 == 0 for y in xrange(6) if y % 2 != 0] [(0, 1), (0, 3), (0, 5), (2, 1), (2, 3), (2, 5), (4, 1), (4, 3), (4, 5)]www.youtube.com/vkedco www.vkedco.blogspot.com
6. 6. List Comprehension with Matriceswww.youtube.com/vkedco www.vkedco.blogspot.com
7. 7. List Comprehension with Matrices ● List comprehension can be used to scan rows and columns in matrices >>> matrix = [ [10, 20, 30], [40, 50, 60], [70, 80, 90] ] ### extract all rows >>> [r for r in matrix] [[10, 20, 30], [40, 50, 60], [70, 80, 90]]www.youtube.com/vkedco www.vkedco.blogspot.com
8. 8. List Comprehension with Matrices >>> matrix = [ [10, 20, 30], [40, 50, 60], [70, 80, 90] ] ### extract column 0 >>> [r[0] for r in matrix] [10, 40, 70]www.youtube.com/vkedco www.vkedco.blogspot.com
9. 9. List Comprehension with Matrices >>> matrix = [ [10, 20, 30], [40, 50, 60], [70, 80, 90] ] ### extract column 1 >>> [r[1] for r in matrix] [20, 50, 80]www.youtube.com/vkedco www.vkedco.blogspot.com
10. 10. List Comprehension with Matrices >>> matrix = [ [10, 20, 30], [40, 50, 60], [70, 80, 90] ] ### extract column 2 >>> [r[2] for r in matrix] [30, 60, 90]www.youtube.com/vkedco www.vkedco.blogspot.com
11. 11. List Comprehension with Matrices ### turn matrix columns into rows >>> rslt = [] >>> for c in xrange(len(matrix)): rslt.append([matrix[r][c] for r in xrange(len(matrix))]) >>> rslt [[10, 40, 70], [20, 50, 80], [30, 60, 90]]www.youtube.com/vkedco www.vkedco.blogspot.com
12. 12. List Comprehension with Matrices ● List comprehension can work with iterables (e.g., dictio- naries) >>> dict = {a : A, bb : BB, ccc : CCC} >>> [(item[0], item[1], len(item[0]+item[1])) for item in dict.items()] [(a, A, 2), (ccc, CCC, 6), (bb, BB, 4)]www.youtube.com/vkedco www.vkedco.blogspot.com
13. 13. List Comprehension ● If the expression inside [ ] is a tuple, parentheses are a must >>> cubes = [(x, x**3) for x in xrange(5)] >>> cubes [(0, 0), (1, 1), (2, 8), (3, 27), (4, 64)] ● Sequences can be unpacked in list comprehension >>> sums = [x + y for x, y in cubes] >>> sums [0, 2, 10, 30, 68]www.youtube.com/vkedco www.vkedco.blogspot.com
14. 14. List Comprehension ● for-clauses in list comprehensions can iterate over any sequences: >>> rslt = [ c * n for c in math for n in (1, 2, 3)] >>> rslt [m, mm, mmm, a, aa, aaa, t, tt,ttt, h, hh, hhh]www.youtube.com/vkedco www.vkedco.blogspot.com
15. 15. List Comprehension & Loop Variables ● The loop variables used in the list comprehension for-loops (and in regular for-loops) stay after the execution. >>> for i in [1, 2, 3]: print i 1 2 3 >>> i + 4 7 >>> [j for j in xrange(10) if j % 2 == 0] [0, 2, 4, 6, 8] >>> j * 2 18www.youtube.com/vkedco www.vkedco.blogspot.com
16. 16. When To Use List Comprehension ● For-loops are easier to understand and debug ● List comprehensions may be harder to understand ● List comprehensions are faster than for-loops in the in- terpreter ● List comprehensions are worth using to speed up sim- pler tasks ● For-loops are worth using when logic gets complexwww.youtube.com/vkedco www.vkedco.blogspot.com
17. 17. pass, del, exec()www.youtube.com/vkedco www.vkedco.blogspot.com
18. 18. pass ● This is a statement that does nothing ● Typical use: when a statement is syntactically required but the program requires no action >>> for i in xrange(200): pass >>> x = 0 >>> while x < 10: pass; x += 1www.youtube.com/vkedco www.vkedco.blogspot.com
19. 19. Deleting Elements in Sequences ● del can be used to delete elements from mutable sequences (we have seen several examples of this) >>> x = [1, 2, 3, 4, 5] >>> del x[1] >>> x [1, 3, 4, 5] >>> del x[0:2] >>> x [4, 5]www.youtube.com/vkedco www.vkedco.blogspot.com
20. 20. Deleting Variables ● del can be used to delete variables as if they never existed >>> dir() ## lists variables and their ## assignments >>> zzz = zzz >>> dir() ## zzz is now displayed >>> del zzz >>> dir() ## zzz is no longer displayedwww.youtube.com/vkedco www.vkedco.blogspot.com
21. 21. exec() ● exec() is used to execute strings, file objects, and code objects >>> exec("def add2(x, y): return x + y") >>> add2(1, 2) 3 >>> exec("z = add2(1, 2); z *= 2; print z") 6 >>> z 6www.youtube.com/vkedco www.vkedco.blogspot.com
22. 22. exec() >>> code_str = """ ### beginning of multi-line code string def fact(n): ### def begins if n == 0: return 1 else: return n * fact(n-1) ### def ends x = fact(5) print x """ ### end of multi-line code string >>> exec code_str ### code string is executed 120www.youtube.com/vkedco www.vkedco.blogspot.com
23. 23. Reading & References ● www.python.org ● http://en.wikipedia.org/wiki/List_comprehension ● Ch 05 M. L. Hetland. Beginning Python From Novice to Pro- fessional, 2nd Ed., APRESSwww.youtube.com/vkedco www.vkedco.blogspot.com