CSC148H5 Lecture Notes - Lecture 9: University Of Toronto Mississauga, Newline
Document Summary
Csc148h5s - introduction to computer science (winter 2017) # part e try : except nameerror: division(x, 1 ) print( "name not defined outside" ) The steps through the (like a for-loop) For each value of , is evaluated and added to a running list. >>> [x + 5 for x in lst] >>> lst = [x + 5 for x in lst] Suppose we have a list of lines that each end with a newline. Create a new list of the same elements but without the new lines. >>> lst = [ " abc " , "def\n" , "ghi\n\n" ] Combine the first elements of each iterable, second elements of each iterable etc. >>> for pair in zip([ 1 , 2 ], [ 3 , 4 ]): Here zip creates a list of tuples, and the comprehension multiplies corresponding elements. >>> [a * b for a, b in zip(x, y)]