In [1]:
import math

def hypotenuse(a: float, b: float) -> float:
    return math.sqrt(a**2 + b**2)

assert abs(hypotenuse(3, 4) - 5.0) < 0.001, "3-4-5 triangle"
In [3]:
def checkWithin(result: float, expected: float, tolerance: float, name: str) -> None:
    """
    Check that result is within tolerance of expected, with an assertion test
    named name.
    """
    assert abs(result - expected) < tolerance, name
    
checkWithin(hypotenuse(3, 4), 5.1, 0.001, "3-4-5 triangle")
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Input In [3], in <cell line: 8>()
      2     """
      3     Check that result is within tolerance of expected, with an assertion test
      4     named name.
      5     """
      6     assert abs(result - expected) < tolerance, name
----> 8 checkWithin(hypotenuse(3, 4), 5.1, 0.001, "3-4-5 triangle")

Input In [3], in checkWithin(result, expected, tolerance, name)
      1 def checkWithin(result: float, expected: float, tolerance: float, name: str) -> None:
      2     """
      3     Check that result is within tolerance of expected, with an assertion test
      4     named name.
      5     """
----> 6     assert abs(result - expected) < tolerance, name

AssertionError: 3-4-5 triangle
In [5]:
print(hypotenuse(4, 5))
print(hypotenuse(4, 5) < 7)
6.4031242374328485
True
In [8]:
def pos(x: float) -> float:
    """
    If x is not zero, return the absolute value of x.
    Otherwise, return 1.
    """
    if x < 0:
        return -x
    else:
        if x == 0:
            return 1
        else:
            return x
        
print(pos(3.14159))
print(pos(0))
print(pos(-42))
3.14159
1
42
In [9]:
def pos(x: float) -> float:
    """
    If x is not zero, return the absolute value of x.
    Otherwise, return 1.
    """
    if x < 0:
        return -x
    elif x == 0:
        return 1
    else:
        return x
        
print(pos(3.14159))
print(pos(0))
print(pos(-42))
3.14159
1
42
In [11]:
def multiOp(x: float, y: float, op: int) -> float:
    """
    Return “x op y”, where the operation is given as a code in op:
        0: addition
        1: subtraction
        2: multiplication
    """
    assert op >= 0, "No negative operation codes"
    assert op <= 2, "Operation codes go up to 2"
    if op == 0:
        return x + y
    if op == 1:
        return x - y
    return x * y

print(multiOp(2, 3, 0))
print(multiOp(2, 3, 2))
5
6
In [13]:
def multiOp(x: float, y: float, op: int) -> float:
    """
    Return “x op y”, where the operation is given as a code in op:
        0: addition
        1: subtraction
        2: multiplication
    """
    print("multiOp: Begining")
    assert op >= 0, "No negative operation codes"
    assert op <= 2, "Operation codes go up to 2"
    print("multiOp: After assert")
    if op == 0:
        return x + y
    print("multiOp: After addition")
    if op == 1:
        return x - y
    print("multiOp: After subtraction")
    return x * y
    print("multiOp: After return???")
    
print(multiOp(2, 3, 2))
multiOp: Begining
multiOp: After assert
multiOp: After addition
multiOp: After subtraction
6
In [16]:
x = hypotenuse(4, 5) < 7
print(x)

if x:
    print("x is True")
True
x is True
In [17]:
op = 0
x = 2
y = 3

if op >= 0:
    if op <= 2:
        print("In range")
    else:
        print("Out of range")
else:
    print("Out of range")
In range
In [19]:
if op >= 0 and op <= 2:
    print("In range")
else: # op < 0 or op > 2
    print("Out of range")
In range
In [20]:
if 0 <= op <= 2:
    print("In range")
In range
In [21]:
if not (op < 0 or op > 2):
    print("In range")
else: # op < 0 or op > 2
    print("Out of range")
In range
In [25]:
def opInRange(op: int) -> bool:
    """
    Returns True if op is in the range 0–2. Otherwise, returns False.
    """
    #if op >= 0 and op <= 2:
    #    return True
    #else:
    #    return False
    return op >= 0 and op <= 2

def multiOp(x: float, y: float, op: int) -> float:
    """
    Return “x op y”, where the operation is given as a code in op:
        0: addition
        1: subtraction
        2: multiplication
    """
    if opInRange(op):
        if op == 0:
            return x + y
        if op == 1:
            return x - y
        return x * y
    else: # not in range
        return -1
    
print(multiOp(2, 3, 0))
print(multiOp(3, 2, -1))
5
-1
In [1]:
def isEven(v: int) -> bool:
    """
    Returns True if v is even, False otherwise.
    """
    #if v%2 == 0:
    #    return True
    #else:
    #    return False
    return v%2 == 0

print(isEven(0))
print(isEven(1))
True
False
In [2]:
def printEvenness(v: int) -> None:
    """
    Prints "v is even" is v is even, "v is odd" otherwise.
    """
    if isEven(v):
        print(v, "is even")
    else:
        print(v, "is odd")
        
printEvenness(0)
printEvenness(42)
printEvenness(31)
0 is even
42 is even
31 is odd
In [3]:
def describeEvenness(v: int) -> str:
    """
    Returns "even" if v is even, "odd" otherwise.
    """
    if isEven(v):
        return "even"
    else:
        return "odd"
    
def printEvenness(v: int) -> None:
    """
    Prints "v is even" is v is even, "v is odd" otherwise.
    """
    print(v, "is", describeEvenness(v))
    
printEvenness(42)
42 is even