In [19]:
assert True, 'This is an assert statement'

# the code will only run if the criteria is true

In [3]:
num = 2.003

In [4]:
assert num> 2, 'value must be > 2'

In [6]:
assert num < 2.001, 'value can only be a bt bigger than 2'


---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-6-664b7ef78302> in <module>()
----> 1 assert num < 2.001, 'value can only be a bt bigger than 2'

AssertionError: value can only be a bt bigger than 2

In [9]:
# error message that the answer doesn't fit the criteria it should
# assert statements are good when you need to create a function, i.e a funcion only works with positive numbers
# another common place is at the end of a function, i.e check that the answer is right eg. answer in kelvin is negative

In [16]:
def C_to_K(temp_in_C):
    """Convert temperature from C to K
    """
    temp_in_K = temp_in_C + 273.15
    assert temp_in_K > 0, 'Kelvin must be positive'
    return temp_in_K

In [17]:
print(C_to_K(10))


283.15

In [18]:
print(C_to_K(-280))


---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-18-368456984e3d> in <module>()
----> 1 print(C_to_K(-280))

<ipython-input-16-83ed5681b53b> in C_to_K(temp_in_C)
      3     """
      4     temp_in_K = temp_in_C + 273.15
----> 5     assert temp_in_K > 0, 'Kelvin must be positive'
      6     return temp_in_K

AssertionError: Kelvin must be positive

In [1]:
#another note

In [ ]: