Class 06 – Monday, September 6
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
- Advance your chrestomathics abilities
- Casting to a decimal
- String method
split()
- Help you better understand how a computer works
- Frame memory built-in functions
id()
andtype()
- Consider assignment subtleties
- Swapping variable values
- Assignment commentary
- ???
Examples
- 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 who_what_and_where.py
- Shows what Python keeps maintains about your variables
- Program the_old_switcheroo.py
- Considers the subtlety of swapping variable values
- Program all_consuming.py
- Considers the subtlety of swapping variable values
Inquiring minds want to know
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.4142135623730951
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, 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
round( number ): 3
Enter number: 2.7182818
number: 2.7182818
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
round( number ): 3
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 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 all_consuming.py
-
Problem
- Develop a program that estimates your annual consumption of a commodity based on how much you consume on a typical weekday and on a typical weekend day.
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 first prompts the user for the commodity consumed.
- The program then individually prompts the user for two integer inputs: first, the quantity consumed on a typical weekday, then the quantity consumed on a typical weekend day. These are to be requested using two separate prompts.
- 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.
-
Strongly suggested agorithm
- Get input commodity of interest
- Get weekday consumption
- Get weekend day consumption
- Compute weekly consumption
- Compute yearly consumption
- Report yearly consumption
-
Three possible 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
Homework 5 commentary
- Five people did not submit the assignment.
- Code snippets from submissions
age1 = input( "Tell me your age:" )
age_orig = input( "You look awesome would you mind tell me how old you are?:" )
age = input("Hey there youngin'! How old are ya?")
print( " Really! You don't look a day over", Faux_age )
print("you dont look a day over",good_age)
print( "Your complimentary age is", complimentary_age )
print("You don't look a day over: ", jeff)
print( 'You do not look a day over', x - 5)
print( " Wow! I would've guessed ", n-5, " ! " )
print ("You do not even look " + str(age2))
print("You do not even look " + str(respond))
print( 'Whatttttt? You do not look a day over', ( str_age + '!' ) )
print( "I would never imagine you to be", str(age) + ", you look at most ", str(compliment_age) + "!" )
print("wow, you don't even look", total)
reply2 = print ( "If I hadn't asked, I would've thought you were ", computed_age )
print( "You don't even look", y )
reply2 = print( "Wow, you look beautiful enough to be", faux_age )
print( "Complimentary Age: ", complimentary_age )
print('Darling, your youth has been good to you. You do not look a day over', whatever_1,'! :)')
print ("Really?? That is quite the surprise you don't look a day over",new_age)
print ("You don't look a day over", n)
print("You don't look a day over", x)
print( "Woah, you look", x , "years young!" )
print ( 'wow you look like you are',Supposed_age,'not',reply1 )
print("You don't even look", b)
print ( "You Look Much younger, you look like you are:" , faux_age )
All you need is love — All you need is love — All you need is love, love — Love is all you need. The Beatles