Download presentation
Presentation is loading. Please wait.
Published byCrystal Ball Modified over 9 years ago
1
Compsci 06/101, Fall 2010 4.1 What will you do in Compsci 6 Today? l Learn about selection and if/else statements In context of solving problems Understanding how to alter flow-of-control in Python Vastly increasing the kinds of problems we can solve l Learn about looping over sequences See above! Sequences will be strings, lists, images l Learn about lists Humongously powerful structure for storing data
2
Compsci 06/101, Fall 2010 4.2 Language and Problems in Context l Convert Romeo and Juliet to Piglatin What else could we do with Shakespeare's plays? How do we convert HTML to text? Remove all $$ from salary data in a file l How do we make an image larger, more red, … What is an image? How do read it? Convert it? Access it? l How do get the parts of an IP address 152.3.250.1 What are the parts? How do we access them? How would a router use these parts?
3
Compsci 06/101, Fall 2010 4.3 How do you solve a problem like …? l Solution from lab last week always returns -1? def firstVowelIndex(word): ai = word.find('a') ei = word.find('e') ii = word.find('i') oi = word.find('o') ui = word.find('u') return min(ai,ei,ii,oi,ui ) l There is more than one way to skin a cat, but we need at least one way
4
Compsci 06/101, Fall 2010 4.4 Python if statements and Booleans l In python we have if: else: elif: Used to guard or select block of code If guard is True then, else other l What type of expression used in if/elif tests? ==,, >=, !=, and, or, not, in Value of expression must be either True or False Type == bool, George Boole, Boolean, l Examples with if String starts with vowel Rock, paper, scissors (!aka Rochambeau) winner
5
Compsci 06/101, Fall 2010 4.5 Pragmatics of using if, else, elif l Indentation in Python determines how blocks of code execute What's in a function, what's not What's guarded by if/else statement Preview: what's in control of for loop l Nested blocks are hard to develop and reason about, but powerful Sometimes factor nested blocks into other functions, for example see Uppity.py l Complex boolean expressions difficult to deal with When is a year a leap year? When divisible by 4, unless divisible by 100 except when divisible by 400
6
Compsci 06/101, Fall 2010 4.6 Grace Murray Hopper (1906-1992) l “third programmer on world’s first large-scale digital computer” US Navy: Admiral “It’s better to show that something can be done and apologize for not asking permission, than to try to persuade the powers that be at the beginning” l ACM Hopper award given for contributions before 35 2004: Jennifer Rexford 2009: Tim Roughgarden
7
Compsci 06/101, Fall 2010 4.7 READ THIS NOW! l How do we convert Romeo and Juliet to all uppercase: WHEREFORE ART THOU ROMEO? See Uppity.py for details, note how many functions have loops l We start with loops over sequences for element in sequence: #Process element Sequences: Strings, files, lists, (more in future) Sequences queryable with boolean in operator
8
Compsci 06/101, Fall 2010 4.8 Sequences: Strings, Files, Lists l String is a sequence of characters which are essentially strings of length 1 Python does not have a character type In Python type("rocket"[0]) is ? l File is a sequence of lines, each line is a string Can process files in other ways, e.g., read entire file or process a fixed number of bytes/chars at a time l List is a sequence of elements For example, returned by line.split() in Uppity.py Lists are powerful abstractions and they are mutable!
9
Compsci 06/101, Fall 2010 4.9 Anatomy of a Python List l Lists are sequences and indexable/subscriptable What does this mean in Python? Lists created by using list(..) as a function, similar to str(..) and int(..) What is list("apple") and what is list(123)? Lists created and returned by other functions, e.g., str.split() or range(.. ) We'll see range later, not a list in Python 3 l See list idiom in Uppity.py, create [], then append Grow the list when appropriate
10
Compsci 06/101, Fall 2010 4.10 Python loops and strings l Smelly code and how to fix it: firstVowelIndex If you're duplicating code it's smelly If you think there's a better way, it could be smelly Smelly code should NOT always be fixed, sometimes you can simply call it odorific! Running right is most important! def firstVowelIndex(word): mi = len(word)+1 for v in "aeiou": if v in word: mi = min(mi,word.find(v)) return mi
11
Compsci 06/101, Fall 2010 4.11 Looping and other idioms l Initialize a variable before a loop, update in loop See vowel indexing code on previous slide: min What about maximal element in a list? Create list of "long" words (from list, file, other source) l This is filtering: selecting according to some criteria Could also remove elements from list, changing list Create new list v. alter existing list: benefits, tradeoffs? l Can we add up the digits of a number? How can we index elements of a number? l Can we add up divisors of a number? How do we loop over possibilities?
12
Compsci 06/101, Fall 2010 4.12 Donald Knuth (b. 1938, Hopper '71) l Scholar, practitioner, artisan Art of Computer Programming Began effort in 1962 to survey entire field, still going l Writes beautiful code Developed TeX to help typeset his books, widely used scientific document processing program l Many, many publications First was in Mad Magazine On the Complexity of Songs Surreal Numbers
13
Compsci 06/101, Fall 2010 4.13 It’s all relative and it depends I make the best bread in the city I make the best bread in the world I make the best bread in the universe I make the best bread on the block
14
Compsci 06/101, Fall 2010 4.14 Richard Stallman (b.1953, Hopper '90) l "World's Best Programmer" Gnu/Linux: g++ Believes all software should be free, but like “free speech”, not “free beer” Won MacArthur award for his efforts and contributions League for Programming Freedom It's about free, not open
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.