CS 111 f21 — Variables and Arithmetic

Table of Contents

1 Review

What are the two things the CPU does in our notional machine?

2 Problem: what icon to display in a weather app?

  • Computer needs to make a decision at a particular point in time
  • Warmup: sketch what the computer needs to know (input) and what the possible outcomes are (output)
  • Simplified problem: difference between current and hot temperatore (i.e., should the app show the current temperature in red)
    • outline: get input data, calculate difference, output result
    • have the values we want: get_cur_temp() and 80° F
      • don't worry about exactly what's going on with get_cur_temp()
      • we'll get into it next week—for now, abstraction!
      • need to make the values available and give them useful names: assignment

3 Variables

  • = and variables have different meaning than in math
  • assignment statement: <variable name> = <expression>

    cur_temp = get_cur_temp()
    hot_temp = 80
    
  • assignment copies value to memory and gives it a label (the variable name)
  • variable names follow certain rules
    • name must begin with a letter or an underscore, can only contain letters, number, and underscores
    • no spaces
    • case sensitive (e.g., cur_temp and Cur_temp are different names)
  • to make variable names easier to read, convention is to use underscores or capitalization
    • cur_temp or curTemp
    • snake case and camel case
    • I expect you to use one of these conventions and be consistent, but which you choose is up to personal preference
      • I tend to use snake case

4 Arithmetic

  • just got off the phone with Current Temperature Inc., get_cur_temp() is in C
  • Python provides the standard mathematical operators
    • + (addition)
    • - (subtraction)
    • * (multiplication)
    • / (division)
    • % (remainder or modulo)
    • ** (exponent)
  • Follows the order of operations you might have seen in math class
    • PEMDAS
    • parens, exponents, multiplication & division, addition & subtraction
    • minus signs happen after exponents, so -2**2 evaluates to -4
  • POLL: 3 * 5 - 10**2 % 9
  • First step: what's the formula we need to implement in Python code?
    • \(C = \frac{5}{9}(F - 32)\)
  • Python version: cur_temp_F = (cur_temp - 32) * 5 / 9
    • when a variable appears on the right side of an =, it is interpreted as the value it labels in memory
    • when a variable appears on the left side of an =, it is the label where the value computed on the right side will be stored
  • quick check: write Python code to compute a lower bound on hours I spent watching Fellowship of the Ring (2 hours 58 minutes) when it came out on VHS (I watched it once per day for a week), the final result should be assigned to a variable
  • Calculate the diffence: difference = hot_temp - cur_temp_F
from temperature import get_cur_temp

cur_temp = get_cur_temp()
hot_temp_F = 80
hot_temp_C = hot_temp_F - 32 * 5 / 9 # missing parentheses
  • output result
from temperature import get_cur_temp

cur_temp = get_cur_temp()
hot_temp_F = 80
hot_temp_C = (hot_temp_F - 32) * 5 / 9
print("hot_temp_C =", hot_temp_c, "degrees Celsius
print("difference from current temp is", cur_temp - hot_temp_C)
  • Three mistakes
    • Forgetting a closing " around text causes a SyntaxError
    • anatomy of the error message
      • File where the error occurred, line where the error occurred, and the error type and message
    • it may feel like it's Python yelling at you for your mistakes
    • really Python catching on fire and saying I don't understand, aaaaaaaahhhhhhhhhh.
    • Forgetting a closing ) also causes a SyntaxError, but often on a line after the line missing a parenthesis
    • Mispelling a variable name results in a NameError
  • How would you demonstrate to me this is correct (or incorrect)
    • print everything, check for known results
  • Compare to one-line version:

    print("difference between hot and current temp in Celsius:", (80 - 32) / 9 * 5 - get_cur_temp())
    

5 Comments

  • any line that starts with a # is a comment
  • ignored by Python, useful for explaining and documenting your code

6 Quiz on Gradescope

7 Lab 0 Intro