{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture06 - Lecture 6 Loops and Files Caltech CS 1 Fall...

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

View Full Document Right Arrow Icon
Caltech CS 1: Fall 2010 Lecture 6 : October 13, 2010 Loops and Files
Background image of page 1

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

View Full Document Right Arrow Icon
Caltech CS 1: Fall 2010 Loops using the for statement Decision-making with if / elif / else
Background image of page 2
Caltech CS 1: Fall 2010 while loops The break statement Files opening and closing the readlines() and readline() functions
Background image of page 3

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

View Full Document Right Arrow Icon
Caltech CS 1: Fall 2010 Last time, we saw the for loop for is natural when working with lists does something with each element of the list Sometimes, we're not working with lists Sometimes, we don't have a fixed number of things to loop over Sometimes, we don't know in advance how many times we will have to go through the loop
Background image of page 4
Caltech CS 1: Fall 2010 Python has a more primitive (simple) loop statement called a while loop Structure: while <boolean expression> : <block of code>
Background image of page 5

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

View Full Document Right Arrow Icon
Caltech CS 1: Fall 2010 Note similarities with if and for forms while <boolean expression> : <block of code>
Background image of page 6
Caltech CS 1: Fall 2010 Note similarities with if and for forms while <boolean expression>: <block of code> Statement starts with the keyword while
Background image of page 7

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

View Full Document Right Arrow Icon
Caltech CS 1: Fall 2010 Note similarities with if and for forms while <boolean expression> : <block of code> There is a colon ( : ) at the end of the first line It must be there, or else a syntax error!
Background image of page 8
Caltech CS 1: Fall 2010 Note similarities with if and for forms while <boolean expression>: <block of code> There is an indented block of code which can be one or multiple lines
Background image of page 9

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

View Full Document Right Arrow Icon
Caltech CS 1: Fall 2010 Evaluation of while loop: while <boolean expression>: <block of code> 1. Evaluate the <boolean expression> 2. If it evaluates to True , evaluate the <block of code> and repeat from the beginning 3. Otherwise, continue with the next line after the while loop
Background image of page 10
Caltech CS 1: Fall 2010 Starting at the number 10, print all the numbers from 10 down to 1 >>> num = 10 >>> while num > 0: ... print num ... num -= 1 10 9 ... until reach 1
Background image of page 11

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

View Full Document Right Arrow Icon
Caltech CS 1: Fall 2010 This is equivalent to writing num = 10 if num > 0: print num num -= 1 if num > 0: print num num -= 1 if num > 0: ... (etc. forever)
Background image of page 12
Caltech CS 1: Fall 2010 When num is no longer > 0, the loop ends and execution continues on the line following the while loop num = 10 while num > 0: print num num -= 1 print "done with the while loop!"
Background image of page 13

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

View Full Document Right Arrow Icon
Caltech CS 1: Fall 2010 This example is unrealistic Could easily write this with a for loop: for num in [10,9,8,7,6,5,4,3,2,1]: print num We know how many times through the loop in advance (10)
Background image of page 14
Caltech CS 1: Fall 2010 Can rewrite using the range function to make it shorter: for num in range(10, 0, -1) : print num range(10, 0, -1) is equal to [10,9,8,7,6,5,4,3,2,1] We'll see more of range next lecture
Background image of page 15

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

View Full Document Right Arrow Icon
Caltech CS 1: Fall 2010 Use raw_input to read numbers from the user and print them, stopping when a negative number is read In this case, we cannot know how many times we will have to go through the loop because we can't control what the user does!
Background image of page 16
Image of page 17
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}