lecture06

lecture06 - Caltech CS 1: Fall 2010 Lecture 6 : October 13,...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Caltech CS 1: Fall 2010 Lecture 6 : October 13, 2010 Loops and Files Caltech CS 1: Fall 2010 Loops using the for statement Decision-making with if / elif / else Caltech CS 1: Fall 2010 while loops The break statement Files opening and closing the readlines() and readline() Caltech CS 1: Fall 2010 while loops The break statement Files opening and closing the readlines() and readline() functions 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 Caltech CS 1: Fall 2010 Python has a more primitive (simple) loop statement called a while loop Structure: while <boolean expression> : <block of code> Caltech CS 1: Fall 2010 Note similarities with if and for forms while <boolean expression> : <block of code> Caltech CS 1: Fall 2010 Note similarities with if and for forms while <boolean expression>: <block of code> Statement starts with the keyword while 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! 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 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 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 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) 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!" 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) 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 Caltech CS 1: Fall 2010 Use raw_input to read numbers from the user and print them, stopping when a negative number is read...
View Full Document

This note was uploaded on 02/22/2011 for the course CS 1 taught by Professor Pinkston,d during the Fall '08 term at Caltech.

Page1 / 64

lecture06 - Caltech CS 1: Fall 2010 Lecture 6 : October 13,...

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

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