print(z)
z = 0
<cell>1: error: Name "z" is used before definition [used-before-def]
0
ct = 0
print(ct)
ct = ct + 1
<cell>1: error: Cannot determine type of "ct" [has-type] <cell>1: error: Name "ct" is used before definition [used-before-def] <cell>2: error: Cannot determine type of "ct" [has-type]
8
from math import sqrt
def hypotenuse(a, b):
# print("a was", a)
return sqrt(a**2 + b**2)
hypotenuse(3, 4)
5.0
def pythagoras(a, b):
"""
Return the length of the hypotenuse of a
right triangle with side lengths a and b.
"""
return sqrt(a**2 + b**2)
help(pythagoras)
Help on function pythagoras in module __main__:
pythagoras(a, b)
Return the length of the hypotenuse of a
right triangle with side lengths a and b.
def sillystring():
return """
Hello world!
""" # This is not a docstring
"""
This
is
a multi-line
comment
"""
print(sillystring())
help(sillystring)
Hello world!
Help on function sillystring in module __main__:
sillystring()
def pythagoras(a, b):
sqrt(a**2 + b**2)
def hypotenuse(a, b):
return sqrt(a**2 + b**2)
print(hypotenuse(3, 4))
print(5)
5.0 5
print(float(42))
print(int(41.999))
print(round(41.999))
print(round(41.912, 2))
42.0 41 42 41.91
def hypotenuse(a: float, b: float) -> str:
return sqrt(a**2 + b**2)
print(hypotenuse(3, 4))
<cell>2: error: Incompatible return value type (got "float", expected "str") [return-value]
5.0
x = print("Hello")
print(x)
print(x / 10)
<cell>3: error: Unsupported operand types for / ("None" and "int") [operator]
Hello None
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [44], in <cell line: 3>() 1 x = print("Hello") 2 print(x) ----> 3 print(x / 10) TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'
def divide(a: float, b: float) -> float:
return a / b
print(divide(3, 4))
print(divide(3, 0))
0.75
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) Input In [46], in <cell line: 5>() 2 return a / b 4 print(divide(3, 4)) ----> 5 print(divide(3, 0)) Input In [46], in divide(a, b) 1 def divide(a: float, b: float) -> float: ----> 2 return a / b ZeroDivisionError: division by zero
def hypotenuse(a: float, b: float) -> float:
return sqrt(a**2 + b**2)
assert hypotenuse(3, 4) == 5, "3-4-5 triangle"
# WARNING: Don't use == for floats!
def zeroDotThreeMinusZeroDotTwo():
return 0.3-0.2
print(zeroDotThreeMinusZeroDotTwo())
assert zeroDotThreeMinusZeroDotTwo() == 0.1, "Math too simple to fail"
0.09999999999999998
--------------------------------------------------------------------------- AssertionError Traceback (most recent call last) Input In [52], in <cell line: 5>() 2 return 0.3-0.2 4 print(zeroDotThreeMinusZeroDotTwo()) ----> 5 assert zeroDotThreeMinusZeroDotTwo() == 0.1, "Math too simple to fail" AssertionError: Math too simple to fail
assert abs(zeroDotThreeMinusZeroDotTwo() - 0.1) < 0.001, "Precision problems!"
print(42/7
#def hello():
# print("World!")
Input In [55] # print("World!") ^ SyntaxError: incomplete input
print(42/7
def hello():
print(
Input In [56] def hello(): ^ SyntaxError: invalid syntax