lecture05

lecture05 - Caltech CS 1: Fall 2010 Lecture 5 : October 11,...

Info iconThis preview shows pages 1–18. 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

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 5 : October 11, 2010 Loops and decisions... Caltech CS 1: Fall 2010 Docstrings Lists Caltech CS 1: Fall 2010 Loops the for statement Making decisions the if statement the else and elif statements Caltech CS 1: Fall 2010 So far, have seen multiple kinds of data int s , float s , strings, lists Have seen how to write functions with def and return Now we introduce another fundamental concept: a loop Caltech CS 1: Fall 2010 A loop is a chunk of code that executes repeatedly though something must change each time the chunk is repeated (or else the program will never terminate) Python has two kinds of loop statements: for loops (this lecture) while loops (next lecture) Caltech CS 1: Fall 2010 Loops often associated with lists Basic idea: for each element of this list do the following ... [chunk of code] ... Example: for each element of a list print the element Caltech CS 1: Fall 2010 title_words = ['Monty', 'Python', 'and', 'the', 'Holy', 'Grail'] for word in title_words: print word for loop Caltech CS 1: Fall 2010 Result: Monty Python and the Holy Grail Caltech CS 1: Fall 2010 Structure of a for loop: for <name> in <list> : <chunk of code> Chunk of code is executed once for each element of the list Each time through, <name> is bound to the next element of <list> and <chunk of code> is executed Caltech CS 1: Fall 2010 title_words = ['Monty', 'Python', ...] for word in title_words: print word First time through: word is 'Monty' print prints Monty Second time through: word is 'Python' print prints Python etc. until no more elements in list Caltech CS 1: Fall 2010 Another way to look at this: for word in title_words: print word This is equivalent to: word = 'Monty' print word word = 'Python' print word word = 'and' print word # etc. Caltech CS 1: Fall 2010 Chunk of code in a for loop is called a block Blocks can consist of multiple lines: for word in title_words: print word print '---' This puts a line of '---' between words: Monty --- Python --- (etc.) Caltech CS 1: Fall 2010 Syntax: 1. Must have a colon ( : ) at the end of the for line 2. Every line in block must be indented the same amount (or else it's a syntax error) 3. End of block indicated when indent goes back to value before for loop began Caltech CS 1: Fall 2010 No colon at end of for line: for word in title_word ^ SyntaxError: invalid syntax Caltech CS 1: Fall 2010 Irregular indentation: for word in title_word: print word print '---' ^ IndentationError: unexpected indent for word in title_word: print word print '---' ^ IndentationError: unindent does not match any outer indentation level Caltech CS 1: Fall 2010 Want to sum elements of a list of numbers nums = [-32, 0, 45, -101, 334] sum_nums = 0 for n in nums: sum_nums = sum_nums + n print sum_nums Result: 246 Caltech CS 1: Fall 2010 When you see a line of the form...
View Full Document

Page1 / 69

lecture05 - Caltech CS 1: Fall 2010 Lecture 5 : October 11,...

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

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