Class 07 – Wedneday, September 8
Make Bill Murray proud
Take another step — Chrestomathics awaits you — Feel the momentum
All Hoos got your back — A single community — In it together
Look both ways
Important survey
- Please take.
Not random thoughts
- Complete introduction of the basics of Python programming.
- To solve fun and realistic problems we need to be able to decompose inputs into manipulable data and then be able to process the data no matter it size.
- To do so requires deeper awareness of string manipulation and the ability to iterate; that is, be able to repeat actions so that all the data can be processed. The slang word for iteration is looping.
Agenda
- Continue exploration of the basics of problem solving — getting data, performing computation, and displaying information
- Continue introduction to the string type
str
My favorite complimenter.py interaction of all time
How old are you? 24
If aliens descended from space, they would classify you as 19
Downloads
- Program temperature.py
- Considers operator precedence
- Program break_the_bank.py
- Continues chrestomathics
- Program who_what_and_where.py
- Introduces
type()
andid()
functions.
- Program the_old_switcheroo.py
- Introduces
type()
andid()
functions.
- Program combine_and_replicate.py
- Shows string
+
and*
usage
- Program wiped.py
- Cleans up user-supplied text
- Program finding.py
- Weird world of indexing in Python
- Program thats_amore.py
- Demonstrates how to achieve capitalization possibilities
- Program finding.py
- Introduces string searching
- Program slice_of_pie.py
- Introduces string searching
- Program cat.py
- Introduces subscripting
- Program replacement.py
- Introduces creating new strings through replacement
To do list
- Review class artifacts
- Finish homework
- Check out other Python built-in functions
- Reread about Python string capabilities.
- Read Chrestomathics: part 05.
Program temperature.py
-
Problem
- Prompt user for a integer Celsius temperature and separately compute its integer and decimal Fahrenheit equivalent using the formula
-
Two possible runs
Enter Celsius temperature: 24
24 C = 75.2 F
24 C = 75 F
Enter Celsius temperature: 8
8 C = 46.4 F
8 C = 46 F
Program break_the_bank.py
- With a single prompt, it gets from the user four values in the following order: number of quarters, dimes, nickels, and pennies. Computes and displays how much the coinage is worth.
- To avoid runs like the following, you are required to make me use of built-in function round() to convert the calculated worth of the piggy bank to two decimal places.
- Two possible testing runs
Number of quarters, dimes, nickels, and pennies: 27 14 28 18
Coinage
27 quarters
14 dimes
28 nickels
18 pennies
are worth 9.73 dollars
Number of quarters, dimes, nickels, and pennies: 1 2 3 4
Coinage
1 quarters
2 dimes
3 nickels
4 pennies
are worth 0.64 dollars
Program who_what_and_where.py
-
Purpose
- Show memory frame information
- Introduce built-in functions
id()
andtype()
.
id( n )
identifies where in memory to findn
type( n )
supplies what type of value isn
-
Two possible program runs
Enter integer and decimal: 3 4.159
reply: 3 4.159 <class 'str'> 140400369925360
n: 3 <class 'int'> 9720640
x: 4.159 <class 'float'> 140400370194512
Enter integer and decimal: 8 8.88
reply: 8 8.88 <class 'str'> 139778356653296
n: 8 <class 'int'> 9720800
x: 8.88 <class 'float'> 139778356922448
Program the_old_switcheroo.py
-
Purpose
- Swap the values of two variables
- Worksheet: suggest you print if off and work through the code by hand
-
As miswritten
reply = input( 'Enter two words: ' )
w1, w2 = reply.split()
print( )
print( 'w1 =', w1 )
print( 'w2 =', w2 )
print()
# swap the values of w1 and w2
w1 = w2
w2 = w1
# print results
print( 'After swapping' )
print()
print( 'w1 =', w1 )
print( 'w2 =', w2 )
-
A program run — what is wrong?
Enter two words: wahoo wah
w1 = wahoo
w2 = wah
After swapping
w1 = wah
w2 = wah
Program wiped.py
-
Purpose
- Introduces string method
strip()
-
A possible program run
Enter text: scraunched is just one syllable
string of interest: scraunched is just one syllable :
stripped version: scraunched is just one syllable :
Program thats_a_moray.py
- Purpose
- Introduce strings methods for letter casing
-
Program run
s: When an Eel Climbs a Ramp to Eat Squid From a Clamp, That’s a Moray
s.lower(): when an eel climbs a ramp to eat squid from a clamp, that’s a moray
s.upper(): WHEN AN EEL CLIMBS A RAMP TO EAT SQUID FROM A CLAMP, THAT’S A MORAY
s.capitalize(): When an eel climbs a ramp to eat squid from a clamp, that’s a moray
Program finding.py
-
Purpose
- Introduce string method
find()
. The method returns where the index (location) of its substring argument.
-
Program run
s: can anyone
----------
0123456789 <== indices into s
t: an
s.find( t ): 1
s.find( t, 1 ): 4
s.find( t, 4 ): -1
Program cat.py
-
Purpose
- Introduce the string brackets operator
[]
to access individual characters in a string.
-
Three program runs
Enter three indices: 0 8 9
cat
Enter three indices: 13 1 3
she
Enter three indices: 2 8 7
ram
Program slice_of_pie.py
-
Purpose
- Introduce string slicing
Three program runs
Enter favorite pie: chocolate
Enter two indices i and j: 3 8
s[ i : j ]: colat
s[ i : ]: colate
s[ : j ]: chocolat
s[ : ]: chocolate
Enter favorite pie: pizza
Enter two indices i and j: 2 3
s[ i : j ]: z
s[ i : ]: zza
s[ : j ]: piz
s[ : ]: pizza
Enter favorite pie: garlic tart
Enter two indices i and j: 1 8
s[ i : j ]: arlic t
s[ i : ]: arlic tart
s[ : j ]: garlic t
s[ : ]: garlic tart
Program
-
Purpose
- Introduce string method
replace()
that returns a variant of string based on replacing its substrings.
-
Two program runs
Enter text: hello mellow pillow
Enter substring (s): ll
Enter substring (r): L L
text.replace( s, r ): heL Lo meL Low piL Low # text’s s’s replaced with r’s
Enter text: jeepers creepers look at those peepers
Enter substring (s): eepers
Enter substring (r):
text.replace( s, r ): j cr look at those p # text’s s’s replaced with r’s