-
Integers, Floats and Strings 3
-
Lecture1.1
-
Lecture1.2
-
Lecture1.3
-
-
If Statements 3
-
Lecture2.1
-
Lecture2.2
-
Lecture2.3
-
-
Lists, Sets and Tuples 5
-
Lecture3.1
-
Lecture3.2
-
Lecture3.3
-
Lecture3.4
-
Lecture3.5
-
-
Loops 3
-
Lecture4.1
-
Lecture4.2
-
Lecture4.3
-
-
Functions 3
-
Lecture5.1
-
Lecture5.2
-
Lecture5.3
-
-
Dictionaries 3
-
Lecture6.1
-
Lecture6.2
-
Lecture6.3
-
-
Assertions 2
-
Lecture7.1
-
Lecture7.2
-
-
Classes 3
-
Lecture8.1
-
Lecture8.2
-
Lecture8.3
-
-
Matplotlib 2
-
Lecture9.1
-
Lecture9.2
-
Introduction
Assertions¶
Assertions are a way to stop code and defensively code to make sure that conditions are being met. The format is:
assert bool
This will stop the program if it is false, but nothing happens if it is true.
In [1]:
#If the assertion is true then nothing happens
assert 5==5
print("Good to go")
Good to go
In [2]:
#But if it is false we stop the program
assert 5==6
print("Good to go")
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-2-5603f772deef> in <module>
1 #But if it is false we stop the program
----> 2 assert 5==6
3 print("Good to go")
AssertionError:
We can see above that the code stopped when we had false. If you want an actual message to be printed with the assertion if it happens then you can with the following format:
assert bool, error_message
In [3]:
#You can also give an error message
assert 5==6, "Numbers not equal"
print("Good to go")
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-3-71e6abf2f923> in <module>
1 #You can also give an error message
----> 2 assert 5==6, "Numbers not equal"
3 print("Good to go")
AssertionError: Numbers not equal
A classic thing to check is to make sure that you have the correct type when using a variable. For example, checking to make sure we have an integer.
In [4]:
#You might use this to check the type of a variable, for example
assert type(5)==int, "This is not an integer"
print("Good to go")
Good to go
In [5]:
assert type('5')==int, "This is not an integer"
print("Good to go")
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-5-202d1b7a72c4> in <module>
----> 1 assert type('5')==int, "This is not an integer"
2 print("Good to go")
AssertionError: This is not an integer
This is good in the context of a function to make sure you have the correct arguments.
In [6]:
#We would use this for functions to ensure we have the correct arguments
def add(a, b):
assert type(a)==int and type(b)==int,"Arguments must be integers"
return a+b
c = add(1,4)
print(c)
5
In [7]:
c = add('1',4)
print(c)
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-7-711a38f2f5c6> in <module>
----> 1 c = add('1',4)
2 print(c)
<ipython-input-6-fd05671c13f6> in add(a, b)
1 #We would use this for functions to ensure we have the correct arguments
2 def add(a, b):
----> 3 assert type(a)==int and type(b)==int,"Arguments must be integers"
4 return a+b
5 c = add(1,4)
AssertionError: Arguments must be integers