Slides 2: Variables and Expressions

Any logistics questions?

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).

Gather-Compute-Use Pattern

  1. Gather input
  • total_days = int(input("days: "))
  1. Compute new information
  • weeks = total_days // 7, days = total_days % 7
  1. Use the computed information
  • print(weeks, "weeks and", days, "days")

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?

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.

Job 11:7-9 (NIV)

Discuss with neighbors:
What do we lose when we reduce the world to ints, floats, and strings?