• Save
Python & Perl: Lecture 10
Upcoming SlideShare
Loading in...5
×
 

Python & Perl: Lecture 10

on

  • 1,042 views

 

Statistics

Views

Total Views
1,042
Views on SlideShare
686
Embed Views
356

Actions

Likes
0
Downloads
0
Comments
0

22 Embeds 356

http://vkedco.blogspot.com 273
http://www.vkedco.blogspot.com 28
http://vkedco.blogspot.com.au 9
http://vkedco.blogspot.in 8
http://vkedco.blogspot.co.il 6
http://vkedco.blogspot.co.nz 4
http://vkedco.blogspot.de 4
http://www.vkedco.blogspot.in 4
http://vkedco.blogspot.nl 3
http://vkedco.blogspot.ca 2
http://vkedco.blogspot.mx 2
http://vkedco.blogspot.kr 2
http://vkedco.blogspot.hu 2
http://vkedco.blogspot.sg 1
http://vkedco.blogspot.fr 1
http://vkedco.blogspot.ru 1
http://vkedco.blogspot.it 1
http://vkedco.blogspot.pt 1
http://vkedco.blogspot.ro 1
http://vkedco.blogspot.be 1
http://vkedco.blogspot.com.es 1
http://vkedco.blogspot.jp 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Python & Perl: Lecture 10 Python & Perl: Lecture 10 Presentation Transcript

  • Python & Perl Lecture 10 Vladimir Kulyukin Department of Computer Science Utah State Universitywww.youtube.com/vkedco www.vkedco.blogspot.com
  • Outline ● List Comprehension with Nested For-Loops ● List Comprehension with Matrices ● del, pass, exec()www.youtube.com/vkedco www.vkedco.blogspot.com
  • List Comprehension with For-Loopswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 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
  • 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
  • List Comprehension with Matriceswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • pass, del, exec()www.youtube.com/vkedco www.vkedco.blogspot.com
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • Feedback Bugs, comments to vladimir dot kulyukin at usu dot eduwww.youtube.com/vkedco www.vkedco.blogspot.com