CMU 15-112: Fundamentals of Programming and Computer Science
Class Notes: Data Types and Operations
Learning Goal: recognize and use different data types and operations in Python. In particular:
- Use integers and floats to perform mathematical and relational operations
- Use booleans to perform logical operations
- Use types to recognize the type of a value
- Some Builtin Types
- Some Builtin Constants
- Some Builtin Operators
- Operator Order (Precedence and Associativity)
- Integer Division
- The Modulus or Remainder operator (%)
- Check 1.6
- Short-Circuit Evaluation
- type vs isinstance
- Check 1.8
- Some Builtin Types
print("Some basic types in Python:") print(type(2)) # int (integer) print(type(2.2)) # float (floating-point number) print(type(2 < 2.2)) # bool (boolean) print(type(type(42))) # type print("#####################################################") print("And some other types we will see later in the course...") print(type("2.2")) # str (string or text) print(type([1,2,3])) # list print(type((1,2,3))) # tuple print(type({1,2})) # set print(type({1:42})) # dict (dictionary or map)
- Some Builtin Constants
print("Some builtin constants:") print(True) print(False) print(None) print("And some more constants in the math module:") import math print(math.pi) print(math.e)
- Some Builtin Operators
Category Operators Arithmetic +, -, *, /, //, **, %, - (unary), + (unary) Relational <, <=, >=, >, ==, != Logical and, or, not - Operator Order (Precedence and Associativity)
print("Precedence:") print(2+3*4) # prints 14, not 20 print(5+4%3) # prints 6, not 0 (% has same precedence as *, /, and //) print(2**3*4) # prints 32, not 4096 (** has higher precedence than *, /, //, and %) print() print("Associativity:") print(5-4-3) # prints -2, not 4 (- associates left-to-right) print(4**3**2) # prints 262144, not 4096 (** associates right-to-left)
- Integer Division
print("The / operator does 'normal' float division:") print(" 5/3 =", ( 5/3)) print() print("The // operator does integer division:") print(" 5//3 =", ( 5//3)) print(" 2//3 =", ( 2//3)) print("-1//3 =", (-1//3)) print("-4//3 =", (-4//3))
- The Modulus or Remainder Operator (%)
print(" 6%3 =", ( 6%3)) print(" 5%3 =", ( 5%3)) print(" 2%3 =", ( 2%3)) print(" 0%3 =", ( 0%3)) print("-4%3 =", (-4%3)) print(" 3%0 =", ( 3%0))
Optional: Verify that (a%b) is equivalent to (a - (a//b)*b):def mod(a, b): return a - (a//b)*b print(41%14, mod(41,14)) print(14%41, mod(14,41)) print(-32%9, mod(-32,9)) print(32%-9, mod(32,-9)) - Check 1.6
- Short-Circuit Evaluation
def yes(): return True def no(): return False def crash(): return 1/0 # crashes! print(no() and crash()) # Works! print(crash() and no()) # Crashes! print (yes() and crash()) # Never runs (due to crash), but would also crash (without short-circuiting)
Once again, using the "or" operator:def yes(): return True def no(): return False def crash(): return 1/0 # crashes! print(yes() or crash()) # Works! print(crash() or yes()) # Crashes! print(no() or crash()) # Never runs (due to crash), but would also crash (without short-circuiting)
Yet another example:def isPositive(n): result = (n > 0) print("isPositive(",n,") =", result) return result def isEven(n): result = (n % 2 == 0) print("isEven(",n,") =", result) return result print("Test 1: isEven(-4) and isPositive(-4))") print(isEven(-4) and isPositive(-4)) # Calls both functions print("----------") print("Test 2: isEven(-3) and isPositive(-3)") print(isEven(-3) and isPositive(-3)) # Calls only one function! - type vs isinstance
# Both type and isinstance can be used to type-check # In general, (isinstance(x, T)) will be more robust than (type(x) == T) print(type("abc") == str) print(isinstance("abc", str)) # We'll see better reasons for this when we cover OOP + inheritance later # in the course. For now, here is one reason: say you wanted to check # if a value is any kind of number (int, float, complex, etc). # You could do: def isNumber(x): return ((type(x) == int) or (type(x) == float)) # are we sure this is ALL kinds of numbers? print(isNumber(1), isNumber(1.1), isNumber(1+2j), isNumber("wow")) # But this is cleaner, and works for all kinds of numbers, including # complex numbers for example: import numbers def isNumber(x): return isinstance(x, numbers.Number) # works for any kind of number print(isNumber(1), isNumber(1.1), isNumber(1+2j), isNumber("wow"))
- Check 1.8
Advanced Data & Expressions (review after the Variables and Functions section)