Class 06 – Monday, September 5
Fearlessly we march on — variables and strings
A sign for knowing — what the future asks of us — demands we act now
I say love is love — For aren’t we all just people — Living as we are
Look both ways
Agenda
- Reconsider last class
- Advance your chrestomathics abilities
- Round a decimal using built-in function
round()
- String method
split()
- Chrestomathics
Examples
- Program iiiii.py
- Another try at repeated variable incrementing
- Program being_powerful.py
- Gets decimal input
- Program trio.py
- Introduces string method function
split()
- Program i_get_around.py
- Shows use of function
round()
- Program escaping.py
- Explores the use of Python escape character
\
for representing special characters.
- Program all_consuming.py
- Chrestomathics
Program iiiii.py
- Another try at repeated variable incrementing by really look at what is going on in the following code.
# define an iterator variable named i, and initialize it to 0
i = 0
print( "i:", i )
print()
# increment i by one (for the first time)
i = i + 1 # i's new value is its current value plus 1
print( "i:", i )
print()
# increment i by one (for a second time)
i = i + 1 # i's new value is its current value plus 1
print( "i:", i )
print()
# increment i by one (for a third time)
i = i + 1 # i's new value is its current value plus 1
print( "i:", i )
print()
# increment i by one (for a fourth time)
i = i + 1 # i's new value is its current value plus 1
print( "i:", i )
print()
# increment i by one (for a fifth time)
i = i + 1 # i's new value is its current value plus 1
print( "i:", i )
print()
Program being_powerful.py
-
Problem
- Prompt and get an integer number (the base for the computation)
- Prompt and get a decimal number (the exponent for the computation)
- Compute the value of the integer to power of the decimal number
- Print the result of the computation
-
What the solution offers
- Uses the built-in cast functions int() and float() to convert numeric strings into the numbers they represent.
-
Why else is it important
- The program has four parts that can serve as a template for much of our future problem solving.
- Get inputs
- Massage inputs into needed values
- Compute result
- Print result
-
Three possible program runs
Enter base (integer): 2
Enter exponent (decimal): 0.5
1.4142235623730951
Enter base (integer): 11
Enter exponent (decimal): 3.14
1861.971509290233
Enter base (integer): 4
Enter exponent (decimal): 0.333
1.5866676863822857
Program trio.py
-
Problem
- From a single input request determine the three words the user supplies
-
Why is it important
- Introduces string method function
split()
-
Two possible program runs
Enter three words: peace joy friends
Your words
peace
joy
friends
Enter three words: ready or not
Your words
ready
or
not
Program i_get_around.py
-
What it offers
- Introduces function
round()
-
Two possible program runs
Enter number: 3.1415926
number: 3.1415926
round( number ): 3
round( number, 6 ): 3.141593
round( number, 5 ): 3.14159
round( number, 4 ): 3.1416
round( number, 3 ): 3.142
round( number, 2 ): 3.14
round( number, 1 ): 3.1
round( number, 0 ): 3.0
Enter number: 2.7182818
number: 2.7182818
round( number ): 3
round( number, 6 ): 2.718282
round( number, 5 ): 2.71828
round( number, 4 ): 2.7183
round( number, 3 ): 2.718
round( number, 2 ): 2.72
round( number, 1 ): 2.7
round( number, 0 ): 3.0
Program escaping.py
-
Program importance
- Introduces the use of Python escape character
\
for representing special characters.
Program output
a: a b c d
b: "Air quotes" are annoying
c: Hello
Goodbye
len( a ): x
len( a ): 7
len( b ): 25
Program all_consuming.py
- Develop a program that estimates the annual consumption of a commodity based on how much is consumed on both a typical weekday and on a typical weekend day.
- Due time: Thursday, September 8
Assumptions
- The term week denotes 5 weekdays and 2 weekend days.
- Assume a year is exactly 52 weeks (yes, we know a real year is longer, but we will ignore that in this program).
- User supplied inputs are integers.
Requirements
- The program prompts the user for the commodity to analyze.
- The program then individually (i.e., separarely) prompts the user for two integer values:
- First, the quantity consumed on a typical weekday,
- Then the quantity consumed on a typical weekend day.
- The program uses the inputs to compute and report the estimate of the quantity consumed in a year.
- The quantity output should specify the commodity whose use is being reported.
Suggested agorithm
- Get input commodity of interest
- Get weekday consumption
- Get weekend day consumption
- Compute weekly consumption
- Compute yearly consumption
- Report yearly consumption
Three program runs
What is something you consume daily? paper napkins
How much consumed on a typical weekday: 6
How much consumed on a typical weekend day: 0
You consume 1560 paper napkins per year.
What is something you consume daily? pretzels
How much consumed on a typical weekday: 0
How much consumed on a typical weekend day: 1
You consume 104 pretzels per year.
What is something you consume daily? jelly beans
How much consumed on a typical weekday: 4
How much consumed on a typical weekend day: 12
You consume 2288 jelly beans per year.
To do list
- Review class artifacts
- Read about strings: sections 04 and 05
- Read more about strings
- Check out the epistles on Python and problem solving
All you need is love — All you need is love — All you need is love, love — Love is all you need. The Beatles