Class Notes: Variables and Functions


  1. Variables
  2. Functions
  3. Statements and Expressions
  4. Built-in Functions
  5. Variable Scope
  6. Return Statements
  7. Print versus Return
  8. Function Composition
  9. Helper Functions
  10. Recommended Functions
  11. Test Functions


  1. Variables
  2. Functions
  3. Statements and Expressions
  4. Builtin Functions
  5. # Some functions are already provided by Python print("Type conversion functions:") print(bool(0)) # convert to boolean (True or False) print(float(42)) # convert to a floating point number print(int(2.8)) # convert to an integer (int) print("And some basic math functions:") print(abs(-5)) # absolute value print(max(2,3)) # return the max value print(min(2,3)) # return the min value print(pow(2,3)) # raise to the given power (pow(x,y) == x**y) print(round(2.354, 1)) # round with the given number of digits

  6. Variable Scope
  7. Return Statements
  8. Print versus Return
  9. Function Composition

  10. For nested function calls, we have to evaluate the innermost functions first
    def f(w): return 10*w def g(x, y): return f(3*x) + y # f(3*x) must be evaluated before we can return def h(z): return f(g(z, f(z+1))) # The innermost f(z+1) must be evaluated first print(h(1)) # hint: try the "visualize" feature

  11. Helper Functions
  12. # We commonly write functions to solve problems. # We can also write functions to store an action that is used multiple times! # These are called helper functions. def onesDigit(n): return n%10 def largerOnesDigit(x, y): return max(onesDigit(x), onesDigit(y)) print(largerOnesDigit(134, 672)) # 4 print(largerOnesDigit(132, 674)) # Still 4

  13. Recommended Functions
  14. # There are a few functions from modules you'll definitely want to use in the assignments # First: the built-in round function has confusing behavior when rounding 0.5. # Use our function roundHalfUp to fix this. def roundHalfUp(d): # Round to nearest with ties going away from zero. # You do not need to understand how this function works. import decimal rounding = decimal.ROUND_HALF_UP return int(decimal.Decimal(d).to_integral_value(rounding=rounding)) print(round(0.5)) # This evaluates to 0 - what! print(round(1.5)) # And this will be 2 - so confusing! print(roundHalfUp(0.5)) # Now this will always round 0.5 up (to 1) print(roundHalfUp(1.5)) # This still rounds up too! # Second: when comparing floats, == doesn't work quite right. # Use almostEqual to compare floats instead print(0.1 + 0.1 == 0.2) # True, but... d1 = 0.1 + 0.1 + 0.1 d2 = 0.3 print(d1 == d2) # False! print(d1) # prints 0.30000000000000004 (uh oh) print(d1 - d2) # prints 5.55111512313e-17 (tiny, but non-zero!) # Moral: never use == with floats! # Python includes a builtin function math.isclose(), but that function # has some confusing behavior when comparing values close to 0. # Instead, let's just make our own version of isclose: def almostEqual(x, y): return abs(x - y) < 10**-9 # This will now work properly! print(almostEqual(0, 0.0000000000001)) print(almostEqual(d1, d2))

  15. Test Functions