Class 4 – Wednesday, September 5
Variation on a theme
A variable — Is like a box or a name — We must all know this
Look both ways
Agenda
- Further expand our communication and abstraction skills
- Expand our understanding of variables
- Introduce assignment
- Introduce input acquisition
- Introduce the Python string type
str
- Get adept at casting
Important questions
- What is a variable?
- Why is assignment so important
- Does the old value of a variable hang around for us to use?
Examples of the day
What the solution offers
- Formally introduces the assignment operator
=
. The assignment operator causes the variable to its left to get the value of the expression to its right.
- The ability to update is crucial when problem solving.
What the solution offers
- Examines some nuances of the assignment operator.
- The final version of the program updates the value of variables in terms of themselves; ie., number of rabbits doubles each generation and the generation number is an increment of the previous generation value.
Problem
- Prompt and get what is on the user’s mind
- Echo (print) the reply
- Prompt and get why the first reply is on their mind.
- Prints ooh
What the solution offers
- Introduces the
str
type operator+
. The+
operator when evaluated with two strings as its operands, produces as its value, their concatenation.
- Also 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.
- The final version of the program reflects echoing of the first input before printing the second prompt
What the solution offers
- Uses the built-in cast functions
int()
andfloat()
to convert numeric strings into the numbers they represent.
- The final version of the program shows the two casts using built-in functions
int()
andfloat()
to convert the supplied string-based inputs into numerics.
What the solution offers
- Introduces the string member function
split()
. Its called a member because every string can be called upon to produce a split up version of itself. The value of asplit()
invocation is the list of substrings (words) that make up the string being told tosplit()
. The function is handy for breaking up user input into its individual parts.
- The final version of the program shows that every string has a member function for inspecting itself. The
split()
function hands back the list of words that make up the string.
- Shows the proper way of converting the splitting of a string made up of numberic characters into individual numbers.
To do list
- Review class artifacts
- Ensure familarity with Python variables, assignment, and input
- Read, but do not do, the next assignment
© 2019 Jim Cohoon | Resources from previous semesters are available. |