CS 111 f21 — Comments, math module, and assignment practice

Table of Contents

1 Assignment

  • Assignment is a one-time operation, and does not establish an equality relationship
  • So

    q = 7
    t = q
    q = q * 2
    

    results in q being 14 and t being 7. The third line changes the value in the memory slot labeled q, but has no effect on the slot labeled t

  • Python Tutor is a great resource for visualizaing what's going on

2 Review

  • What will be printed?1

    a = 10
    b = a + 5
    c = b + a
    a = 2
    print(a, b, c)
    
  • What will be printed?2

    x = 8 - 10 / 2
    y = -x * (x + 1)
    x = x + 1
    print(x, y)
    

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

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)
  • Compare to one-line version:
print("difference between hot and current temp in Celsius:", (80 - 32) / 9 * 5 - get_cur_temp())

4 Comments

  • any line that starts with a # is a comment
  • ignored by Python, useful for explaining and documenting your code
  • good things to document:
    • Your name, date, purpose of the file (at the top)
    • Separate steps of the program
    • Any particularly tricky line or section
    • Code you found and/or adapted from somewhere else (including the URL, if applicable)
  • don't document basic Python features

5 Trouble getting set up for Lab 0?

  • Post on the Moodle fourm!
  • Come to my office hours!
  • Ask a lab assistant for help in Olin 310!
  • Ask Mike Tie or one of his assistant system administrators for help (Olin 335, right next to my office)!

6 Python Standard Library

  • https://docs.python.org/3/library/index.html
  • Made up of modules which are made available by importing
    • Example

      import math
      print(math.sqrt(100)) # prints 10
      print(math.sqrt(-100)) # causes a ValueError since sqrt can't handle negative numbers
      
    • sqrt is something we call a function in Python
    • We use or call a function with its name and parentheses
      • Inside the parentheses, we provide the inputs to the function
    • Note we access sqrt by writing MODULE_NAME.sqrt (where in this case MODULE_NAME is math)
      • This is how modules work in general
    • Variation on import
      • from math import sqrt lets us just write sqrt instead of math.sqrt
  • Some things are built-in
    • round function
    • round(10.123, 1) produces 10.1

7 Practice

  • you have a variable change that contains the amount of change need in cents
    • For example change = 82
  • write Python code to compute how many quarters (25), dimes (10), nickels (5), and pennies (1) are required3
    • You can use // to divide and
  • what if change is a decimal number for the amount of change in dollars4
    • For example, change = 1.47

Footnotes:

1

2 15 25

2

4 -12

3
change = 82
quarters = change // 25
remaining = change % 25
dimes = remaining // 10
remaining = remaining % 10
nickles = remaining // 5
remaining = remaining % 5
pennies = remaining
print("The change will be", quarters, "quarters,", dimes, "dimes,", nickles, "nickles, and", pennies, "pennies")
4
change = 1.47
cents = change * 100 # one strategy would be to convert change into cents
dollars = cents // 100
remaining = cents % 100
quarters = remaining // 25
remaining = remaining % 25
dimes = remaining // 10
remaining = remaining % 10
nickles = remaining // 5
remaining = remaining % 5
pennies = remaining
print("The change will be", dollars, "dollarrs,", quarters, "quarters,", dimes, "dimes,", nickles, "nickles, and", pennies, "pennies")