CSC326 Lecture 3 - 4

CSC326 Lecture 3 - 4 - CSC326 Python Sequences i CSC326...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
CSC326 Python Sequences i CSC326 Python Sequences
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSC326 Python Sequences ii REVISION HISTORY NUMBER DATE DESCRIPTION NAME 1.0 2011-09 JZ
Background image of page 2
CSC326 Python Sequences iii Contents 1 Agenda 1 2 while Statement 1 3 Sequence Overview 2 4 String 2 5 Lists 4 6 Dictionary 5 7 Tuples 7 8 What We have Learned for Sequences 9 9 Cross product of two sets 11 10 Accessing Matrics 12 11 Prime Number with List Comprehension 12 12 Quick Sort 12 13 Recap 12
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSC326 Python Sequences 1 / 12 1 Agenda • iteration construct • sequences string list dictionary tuple 2 while Statement • recall recursive construction of countdown • cost of function calls • good old while loop def countdown( n ) : while n > 0 : print n n = n - 1 print ’Blastoff!’ • forever loop while True : line = raw_input( ’> ’ ) if line == ’done’ : break print line print ’Done’ • square root approximation algorithm while True: print x y = (x + a/x) / 2 if y == x: break x = y • Newton’s algorithm • A few caveats epsilon = 0.01 def sra( a ) : x = a / 2 while True: print x y = (x + a/x) / 2 if abs(y-x) < epsilon : break x = y return x
Background image of page 4
CSC326 Python Sequences 2 / 12 • Recap Multiple assignment: vars are initialized and assigned more than once loop often has loop index, incremented by one each iteration conditional evaluation and branch forming cycle 3 Sequence Overview • an ordered collection (set) of values membership test subset eumeration • another angle: a mapping from an index to value lookup reverse lookup (searching, content address memory) • data structures in C • native data types with native operators in Python make python enjoyable to read and write! 4 String • We have already seen one: string is a sequence! • sequence of characters • indexed lookup using [] >>>> fruit = ’banana’ >>>> print fruit[0] >>>> print fruit[1] • index has to be an integer >>>> letter = fruit[1.5] TypeError: string indices must be integers • builtin function len >>>> len(fruit) 6 • index has to be within range >>>> letter = fruit[len(fruit)] IndexError: string index out of range
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSC326 Python Sequences 3 / 12 • enumeration: for statement for char in fruit print char • compare with C for ( i = 0; i < strlen(fruit); i ++ ) printf( "%c" , fruit[i] ); • subsetting: string slices [first:last]: from first to last, excluding last [first:] [:last] [:] ? print fruit[0:3] print fruit[0:] print fruit[:3] print fruit[3:3] • strings are "immutable" element cannot be assigned ( NOT an Lvalue) has to create new string of wants to modify greeting = ’hello, world’ greeting[0] = ’J’ # wrong! new_greeting =
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/20/2012 for the course CSC 326 taught by Professor Jzhu during the Fall '11 term at University of Toronto.

Page1 / 15

CSC326 Lecture 3 - 4 - CSC326 Python Sequences i CSC326...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online