What will you do in Compsci 6 Today?

advertisement
What will you do in Compsci 6 Today?

Learn about selection and if/else statements




Learn about looping over sequences


In context of solving problems
Understanding how to alter flow-of-control in Python
Vastly increasing the kinds of problems we can solve
See above! Sequences will be strings, lists, images
Learn about lists

Humongously powerful structure for storing data
Compsci 06/101, Fall 2010
4.1
Language and Problems in Context

Convert Romeo and Juliet to Piglatin




How do we make an image larger, more red, …


What else could we do with Shakespeare's plays?
How do we convert HTML to text?
Remove all $$ from salary data in a file
What is an image? How do read it? Convert it? Access it?
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?
Compsci 06/101, Fall 2010
4.2
How do you solve a problem like …?

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)

There is more than one way to skin a cat, but we
need at least one way
Compsci 06/101, Fall 2010
4.3
Python if statements and Booleans

In python we have if: else: elif:



What type of expression used in if/elif tests?




Used to guard or select block of code
If guard is True then, else other
==, <=, <, >, >=, !=, and, or, not, in
Value of expression must be either True or False
Type == bool, George Boole, Boolean,
Examples with if


String starts with vowel
Rock, paper, scissors (!aka Rochambeau) winner
Compsci 06/101, Fall 2010
4.4
Pragmatics of using if, else, elif

Indentation in Python determines how blocks of
code execute




Nested blocks are hard to develop and reason
about, but powerful


What's in a function, what's not
What's guarded by if/else statement
Preview: what's in control of for loop
Sometimes factor nested blocks into other functions, for
example see Uppity.py
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
Compsci 06/101, Fall 2010
4.5
Grace Murray Hopper (1906-1992)

“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”
ACM Hopper award given for
contributions before 35
2004: Jennifer Rexford
2009: Tim Roughgarden
Compsci 06/101, Fall 2010
4.6
READ THIS NOW!

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
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
Compsci 06/101, Fall 2010
4.7
Sequences: Strings, Files, Lists

String is a sequence of characters which are
essentially strings of length 1



File is a sequence of lines, each line is a string


Python does not have a character type
In Python type("rocket"[0]) is ?
Can process files in other ways, e.g., read entire file or
process a fixed number of bytes/chars at a time
List is a sequence of elements


For example, returned by line.split() in Uppity.py
Lists are powerful abstractions and they are mutable!
Compsci 06/101, Fall 2010
4.8
Anatomy of a Python List

Lists are sequences and indexable/subscriptable


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(..)


What does this mean in Python?
We'll see range later, not a list in Python 3
See list idiom in Uppity.py, create [], then append

Grow the list when appropriate
Compsci 06/101, Fall 2010
4.9
Python loops and strings

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
Compsci 06/101, Fall 2010
4.10
Looping and other idioms

Initialize a variable before a loop, update in loop

See vowel indexing code on previous slide: min
• What about maximal element in a list?


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?
Can we add up the digits of a number?


Create list of "long" words (from list, file, other source)
How can we index elements of a number?
Can we add up divisors of a number?

How do we loop over possibilities?
Compsci 06/101, Fall 2010
4.11
Donald Knuth (b. 1938, Hopper '71)
Scholar, practitioner, artisan
 Art of Computer
Programming
 Began effort in 1962 to
survey entire field, still
going
 Writes beautiful code
 Developed TeX to help
typeset his books, widely
used scientific document
processing program
 Many, many publications
 First was in Mad Magazine
 On the Complexity of Songs
Compsci 06/101, Fall 2010
 Surreal Numbers

4.12
It’s all relative and it depends
I make
the best
bread in
the city
I make
the best
bread in
the world
Compsci 06/101, Fall 2010
I make the best
bread in the
universe
I make the
best bread on
the block
4.13
Richard Stallman (b.1953, Hopper '90)

"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
Compsci 06/101, Fall 2010
4.14
Download