{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture07

# lecture07 - Caltech CS 1 Fall 2010 Lecture 7 Odds and Ends...

This preview shows pages 1–15. Sign up to view the full content.

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document
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 7 : October 18, 2010 Odds and Ends Caltech CS 1: Fall 2010 while loops break Files • opening, closing, reading from files Caltech CS 1: Fall 2010 A smorgasbord of topics: • More on booleans • More on loops • looping over files with for • The range function • Tuples • The enumerate function • Caltech CS 1: Fall 2010 A smorgasbord of topics: • More on booleans • More on loops • looping over files with for • The range function • Tuples • The enumerate function • Sequence slices Caltech CS 1: Fall 2010 Booleans are True / False values However, Python is not strict about this Several things besides the False value are considered to be False by Python • the empty string: '' • the empty list: • the number 0 ... and most other values can be considered to be True Caltech CS 1: Fall 2010 Python also supports three boolean operators • take booleans arguments, return booleans • not , and , and or not – converts True False , False True >>> not True False >>> not False True Caltech CS 1: Fall 2010 >>> not 0 True >>> not 1 False >>> not '' # empty string True >>> not True Caltech CS 1: Fall 2010 and and or are operators that take two boolean arguments and return a boolean value and returns True if both arguments are True , otherwise False or returns True if either or both arguments are True , otherwise False Caltech CS 1: Fall 2010 >>> True and True True >>> True and False False >>> False or True True >>> False or False False Usually don't use and and or this way, though Caltech CS 1: Fall 2010 Generally use and and or with expressions that evaluate to booleans • e.g. relational operators a = 10 b = 30 # ... later in code ... if a > 0 and b < 50 : ... # ... later in code ... if a < 0 or b > 50 : ... Caltech CS 1: Fall 2010 Recall the example problem from last time: temps = open('temps.txt', 'r') sum_nums = 0.0 while True: line = temps.readline() if line == '': break sum_nums += float(line) Caltech CS 1: Fall 2010 We had these lines of code in the example: if line == '' : break Since the empty string '' can be considered to be a False value, can write this in equivalent form: if not line : break not line means "if line is empty" or "if there is no line" Caltech CS 1: Fall 2010 With an infinite loop and not line : temps = open('temps.txt', 'r') sum_nums = 0.0 while True: line = temps.readline() if not line: break sum_nums += float(line) This is OK but still somewhat long-winded Caltech CS 1: Fall 2010 Python allows an amazing shortcut using the for statement: temps = open('temps.txt', 'r') sum_nums = 0.0 for line in temps : sum_nums += float(line) This is the preferred way to write this Caltech CS 1: Fall 2010 Previously, we had: for <name> in <list>: # block of code The thing after the in had to be a list or a string Python actually allows more than just lists or strings after the in • files being one example Caltech CS 1: Fall 2010...
View Full Document

{[ snackBarMessage ]}

### Page1 / 70

lecture07 - Caltech CS 1 Fall 2010 Lecture 7 Odds and Ends...

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

View Full Document
Ask a homework question - tutors are online