Slides 2: Variables and Expressions
Socrative Reminder
Install the phone app, or Google “Socrative student login”
Use the room CS106
For name , use your Calvin Username (e.g., ka37)
POGIL 2
Process-Oriented Guided Inquiry Learning
Activity 2: Arithmetic Expressions
Roles:
a programmer (runs the exercises in Thonny )
a recorder (writes the team’s answers to the exercises)
a manager/presenter (keeps the team on track; interacts with class)
Days and weeks
Write an algorithm (on paper, in English not Python code) that
prompts the user for a number of days, and
prints the equivalent number of weeks and (remaining) days.
Now, write the Python code.
When finished or stuck, share with your neighbor(s).
Homework Prep
How do you draw a filled shape?
Looking up documentation.
Circle Math
Write an algorithm (on paper, in English not Python code) that
prompts the user for the diameter of a circle in inches , then
prints the circumference of the circle in centimeters , then
prints the area of the circle in square centimeters
Now, write the Python code.
When finished or stuck, share with your neighbor(s).
Escape Sequences
Escape sequences are used to represent special characters in strings.
Escape Sequence Exercise
Write a single print call that gives the following output:
Don't say "I can't"
until you've tried 😀
print (
"Don't say " +
" \" I can't \"\n " +
" until you've tried " +
" \U0001F600 " )
We needed to escape the double quotes with a backslash.
We needed to include a newline using \n.
We split the string across multiple lines for readability.
We used the + operator to concatenate the strings.
Exercise
a. What is the output of the following code?
x = 37
y = x + 2
x = 20
x = x + 1
print (x)
print (y)
b. What is the output of the following code?
y = 3
x = 7
y = 1 + 2 * 3
print (x)
print (y)
Check your thinking on PythonTutor .
Python Basic Data Types
int - integer (whole numbers)
in Python: practically unlimited size
float - floating point numbers
has fractional part
limited size (e.g., 64 bits)
used for measurements, scientific calculations
floating point: some of those bits say where the decimal point goes
represented as a whole number, scaled by a power of 2
str - strings (sequence of characters)
practically unlimited size
Limits of Computers’ Numeric Representations
Overflow : result of a calculation is too large to be represented
Loss of precision
Bounds
What do these two videos have in common?
Both demonstrate anomalies caused by bounded representation.
Solutions
Refactor to using 64-bit integers?
Will \(2^{63}-1\) (> 9 quintillion) be big enough?
Python extends integers infinitely, within the limit of system memory.
Character representations are similarly limited.
ASCII only really works in North America.
UNICODE covers most of the world’s scripts.
But not all - Wycliffe has UNICODE people on staff.
“We have to recognise that in software we are always approximating reality,” - B. Scherlis (see BBC article)
Reference:
Limitations
Can you fathom the mysteries of God?
Can you probe the limits of the Almighty?
They are higher than the heavens above—what can you do?
They are deeper than the depths below—what can you know?
Their measure is longer than the earth
and wider than the sea.
Discuss with neighbors : What do we lose when we reduce the world to ints, floats, and strings?
We’re limited (by design and by sin); God is unlimited
Cf. God’s response in Job 38.