Class 4 – Wednesday, August 31
Variation on a theme
A variable — Is like a box or a name — We must all know this
The #metoo movement — Calls out to everyone — Female Lives Matter
Look both ways
Aside
- Madison House plea
- Zoom information (785 886 8536) September 1st at 6 PM
- In person on September 7th in RRH 246 at 6 PM.
- Applications are due September 16th through GivePulse.
What is happening today
- Want you to have a mental model of how to step through code.
- Become reactive
Current and future examples (download them all, some have been updated)
- Program piaget.py
- Program intsy_bitsy.py
- Program floatilla.py
- Program variety.py
- Program psychoanalyst.py
- Program favorite_word.py
- Program hip_hopping.py
A code walk through — getting a mental model of variables and assignment
Agenda
- Further expand our communication skills
- Consider abstraction
- Expand our understanding of variables
- Introduce assignment
- Introduce input acquisition
Important questions that I hoped were answered last time
- What is a variable?
- Why is assignment so important
- Does the old value of a variable hang around for us to use?
Whoops
- Program piaget.py
- Typos galore
- Code listing
Purpose: correct errant code
Author: Jim Cohoon
Id: jpc
print( We learn best, when we must invent. -- Jean Piaget
- Wanted output
We learn best, when we must invent. -- Jean Piaget
Program intsy_bitsy.py
-
Problem
- Introduce the
int
type and integer arithmetic
-
Program run
Values of interest
a: 20
b: 3
Results
a + b: 23
a - b: 17
a * b: 60
a / b: 6.666666666666667
a // b: 6
a ** b: 8000
a % b: 2
Program floatilla.py
-
Problem
- Introduce the
float
type and decimal arithmetic
-
Program run
Values of interest
x: 2.71
y: 3.14
Results
x + y: 5.85
x - y: -0.43000000000000016
x * y: 8.5094
x / y: 0.8630573248407643
x // y: 0.0
x ** y: 22.883559193263366
x % y: 2.71
Program variety.py
-
Problem
- Perform several assignment statements including an update of a variable.
-
What the solution offers
- Basic understanding of variables and assignment.
- The ability to update is crucial when problem solving.
-
Check out after class
- Different walkthrough
Program hip_hopping.py
-
Problem
- Simulate the number of rabbits occurring over five generations.
-
What the solution offers
- Examines some nuances of the assignment operator.
-
Expected output
Generation: 1
Rabbits: 2
Generation: 2
Rabbits: 4
Generation: 3
Rabbits: 8
Generation: 4
Rabbits: 16
-
Note for later
- The artifact version of the program updates the values of the variables in terms of themselves.
Program psychoanalyst.py
-
Problem
- Prompt and get what is on the user’s mind
- Print a reaction
- Print an out of time message.
-
What the solution offers
- Introduces built-in function
input()
. When the function is invoked, its argument is printed to the display, the entry supplied by the user in response to that message is the value of the invocation. Theinput()
function always returns the user reply as a string.
- The
input()
function allows programs to be interactive; i.e., their actions can be influenced by the user. Theinput()
function enables Python to do general problem solving.
-
Three possible program runs
Tell me what is on your mind: Worrying about the future
That tells me a lot about you.
I wish we had more time to chat. So long.
Tell me what is on your mind: Fluttering butterfly
That tells me a lot about you.
I wish we had more time to chat. So long.
Tell me what is on your mind: Is Superman colorblind
That tells me a lot about you.
I wish we had more time to chat. So long.
Program favorite_word.py
-
Problem
- Prompt and get a word of interest from user user
- Compute the length of the word
- Output a message about the length of the word
-
What the solution offers
Another use of the built-in function
input()
.
Introduces the built-in function
len()
.
-
Three possible program runs
Tell me a favorite word: crwth
Did you know that crwth has length 5
Tell me a favorite word: strengths
Did you know that strengths has length 9
Tell me a favorite word: squirreled
Did you know that squirreled has length 10
Program combine_replicate.py
- Introduces the string concatenation operator
+
and replication operator*
.
### operator + performs concatenation
a = fire
b = fighter
a + b = firefighter
### * operator produces repeated concatenation
m = Wahoo-Wah!
n = 3
m * n = Wahoo-Wah!Wahoo-Wah!Wahoo-Wah!
n * m = Wahoo-Wah!Wahoo-Wah!Wahoo-Wah!
To do list
- Review class artifacts
- Ensure familarity with Python variables, assignment, and input
- Look over the Python string module (library)
- Read the epistle on
variety.py
- Re-examine the walkthrough
- Look over another walkthrough
- Do current assignment
- Read, but do not do, the next assignment