booleans 3: values of True and False
I added a new if statement to the numbers_only function in the calculator program because when I tested it with different data types, True and False passed the condition, and made the test fail.
This means that they are also integers or floats even though they are booleans. I want to find out if booleans are integers or floats
open the project
I change directory to the
booleansfoldercd booleansthe terminal shows I am in the
booleansfolder.../pumping_python/booleansI use
pytest-watcherto run the testsuv run pytest-watcher . --nowthe terminal shows
rootdir: .../pumping_python/booleans configfile: pyproject.toml collected 2 items tests/test_booleans.py .. [100%] ======================== 2 passed in X.YZs =========================I hold ctrl on the keyboard, then click on
tests/test_booleans.pyto open it in the editor
is False an integer or a float?
RED: make it fail
I add a new assertion to test_what_is_false
6 def test_what_is_false(self):
7 self.assertIsInstance(False, bool)
8 self.assertNotIsInstance(False, int)
9 self.assertFalse(False)
the terminal shows AssertionError
AssertionError: False is an instance of <class 'int'>
GREEN: make it pass
I change assertNotIsInstance to assertIsInstance
8 self.assertIsInstance(False, int)
the test passes
REFACTOR: make it better
I add a comment
67# False is False 68# False is not true 69# False is a boolean 70# False is an integer 71 72 73# Exceptions Encountered 74# AssertionErrorI add another assertion to see if False is a float
6 def test_what_is_false(self): 7 self.assertIsInstance(False, bool) 8 self.assertIsInstance(False, int) 9 self.assertIsInstance(False, float) 10 self.assertFalse(False)the terminal shows AssertionError
AssertionError: False is not an instance of <class 'float'>I change assertIsInstance to assertNotIsInstance
8 self.assertIsInstance(False, int) 9 self.assertNotIsInstance(False, float) 10 self.assertFalse(False)the test passes
I add a comment
70# False is a boolean 71# False is an integer 72# False is not a float 73 74 75# Exceptions Encountered 76# AssertionErrorI can use an iterable with the assertIsInstance method, the same way I do with the isinstance function in the
numbers_onlyfunction in the calculator6 def test_what_is_false(self): 7 self.assertIsInstance(False, bool) 8 self.assertIsInstance(False, int) 9 self.assertNotIsInstance(False, (bool, int)) 10 self.assertNotIsInstance(False, float)the terminal shows AssertionError
AssertionError: False is an instance of (<class 'bool'>, <class 'int'>)I change the assertion
9 self.assertIsInstance(False, (bool, int))the test passes
I remove the first two assertions in the test since they are covered by the new one
6 def test_what_is_false(self): 7 self.assertIsInstance(False, (bool, int)) 8 self.assertNotIsInstance(False, float) 9 self.assertFalse(False)I use a for loop for the assertions that test_what_is_false
6 def test_what_is_false(self): 7 self.assertIsInstance(False, (bool, int)) 8 self.assertNotIsInstance(False, float) 9 # self.assertFalse(False) 10 # self.assertFalse(None) 11 # self.assertFalse(bool(None)) 12 # self.assertFalse(0) 13 # self.assertFalse(bool(0)) 14 # self.assertFalse(0.0) 15 # self.assertFalse(bool(0.0)) 16 # self.assertFalse(str()) 17 # self.assertFalse(bool(str())) 18 # self.assertFalse(tuple()) 19 # self.assertFalse(bool(tuple())) 20 # self.assertFalse(list()) 21 # self.assertFalse(bool(list())) 22 # self.assertFalse(set()) 23 # self.assertFalse(bool(set())) 24 # self.assertFalse(dict()) 25 # self.assertFalse(bool(dict())) 26 for false_item in ( 27 False, 28 None, bool(None), 29 0, 0.0, bool(0), bool(0.0), 30 str(), bool(str()), 31 tuple(), bool(tuple()), 32 list(), bool(list()), 33 set(), bool(set()), 34 dict(), bool(dict()), 35 ): 36 with self.subTest(item=false_item): 37 self.assertTrue(false_item) 38 39 def test_what_is_true(self):the terminal shows AssertionError for all of them
SUBFAILED(item=False) ... - AssertionError: False is not true SUBFAILED(item=None) ... - AssertionError: None is not true SUBFAILED(item=False) ... - AssertionError: False is not true SUBFAILED(item=0) ... - AssertionError: 0 is not true SUBFAILED(item=0.0) ... - AssertionError: 0.0 is not true SUBFAILED(item=False) ... - AssertionError: False is not true SUBFAILED(item=False) ... - AssertionError: False is not true SUBFAILED(item='') ... - AssertionError: '' is not true SUBFAILED(item=False)... - AssertionError: False is not true SUBFAILED(item=()) ... - AssertionError: () is not true SUBFAILED(item=False) ... - AssertionError: False is not true SUBFAILED(item=[]) ... - AssertionError: [] is not true SUBFAILED(item=False) ... - AssertionError: False is not true SUBFAILED(item=set()) ... - AssertionError: set() is not true SUBFAILED(item=False) ... - AssertionError: False is not true SUBFAILED(item={}) ... - AssertionError: {} is not true SUBFAILED(item=False) ... - AssertionError: False is not trueI change the assertion
37 self.assertFalse(false_item)the test is green again
I remove the commented lines
6 def test_what_is_false(self): 7 self.assertIsInstance(False, (bool, int)) 8 self.assertNotIsInstance(False, float) 9 for false_item in ( 10 False, 11 None, bool(None), 12 0, 0.0, bool(0), bool(0.0), 13 str(), bool(str()), 14 tuple(), bool(tuple()), 15 list(), bool(list()), 16 set(), bool(set()), 17 dict(), bool(dict()), 18 ): 19 with self.subTest(item=false_item): 20 self.assertFalse(false_item) 21 22 def test_what_is_true(self):
is True an integer or a float?
RED: make it fail
I add an assertion to test_what_is_true to test if True is also an integer
22 def test_what_is_true(self):
23 self.assertIsInstance(True, bool)
24 self.assertNotIsInstance(True, int)
25 self.assertTrue(True)
the terminal shows AssertionError
AssertionError: True is an instance of <class 'int'>
GREEN: make it pass
I change assertNotIsInstance to assertIsInstance
24 self.assertIsInstance(True, int)
the test passes
REFACTOR: make it better
I add a comment
55# True is a boolean 56# True is an integer 57# the empty dictionary is False 58# the empty set is FalseI add another assertion to test if True is a float
22 def test_what_is_true(self): 23 self.assertIsInstance(True, bool) 24 self.assertIsInstance(True, int) 25 self.assertIsInstance(True, float) 26 self.assertTrue(True)the terminal shows AssertionError
AssertionError: True is not an instance of <class 'float'>I change the assert method
25 self.assertNotIsInstance(True, float)the test passes. This is why my test with different data types failed. True and False are integers and the if statement in the
numbers_onlyfunction allows integers and floatsI add a comment
55# True is a boolean 56# True is an integer 57# True is not a float 58# the empty dictionary is FalseI can use an iterable with the assertIsInstance method, the same way I do with the isinstance function in the
numbers_onlyfunction in the calculator22 def test_what_is_true(self): 23 self.assertIsInstance(True, bool) 24 self.assertIsInstance(True, int) 25 self.assertNotIsInstance(True, (bool, int)) 26 self.assertNotIsInstance(True, float)the terminal shows AssertionError
AssertionError: True is an instance of (<class 'bool'>, <class 'int'>)I change the assertion
25 self.assertIsInstance(True, (bool, int))the test passes
I remove the first two assertions in the test since they are covered by the new one
22 def test_what_is_true(self): 23 self.assertIsInstance(True, (bool, int)) 24 self.assertNotIsInstance(True, float) 25 self.assertTrue(True)I use a for loop for the assertions that test_what_is_true
22 def test_what_is_true(self): 23 self.assertIsInstance(True, (bool, int)) 24 self.assertNotIsInstance(True, float) 25 # self.assertTrue(True) 26 # self.assertTrue(-1) 27 # self.assertTrue(bool(-1)) 28 # self.assertTrue(1) 29 # self.assertTrue(bool(1)) 30 # self.assertTrue(-0.1) 31 # self.assertTrue(bool(-0.1)) 32 # self.assertTrue(0.1) 33 # self.assertTrue(bool(0.1)) 34 # self.assertTrue("text") 35 # self.assertTrue(bool("text")) 36 # self.assertTrue((1, 2, 3, 'n')) 37 # self.assertTrue(bool((1, 2, 3, 'n'))) 38 # self.assertTrue([1, 2, 3, 'n']) 39 # self.assertTrue(bool([1, 2, 3, 'n'])) 40 # self.assertTrue({1, 2, 3, 'n'}) 41 # self.assertTrue(bool({1, 2, 3, 'n'})) 42 # self.assertTrue({'key': 'value'}) 43 # self.assertTrue(bool({'key': 'value'})) 44 for true_item in ( 45 True, 46 -1, bool(-1), 1, bool(1), 47 -0.1, bool(-0.1), 0.1, bool(0.1), 48 "text", bool("text"), 49 ((1, 2, 3, 'n')), bool((1, 2, 3, 'n')), 50 [1, 2, 3, 'n'], bool([1, 2, 3, 'n']), 51 {1, 2, 3, 'n'}, bool({1, 2, 3, 'n'}), 52 {'key': 'value'}, bool({'key': 'value'}), 53 ): 54 with self.subTest(item=true_item): 55 self.assertFalse(true_item)the terminal shows AssertionError for all of them
SUBFAILED(item=True) ... - AssertionError: True is not false SUBFAILED(item=-1) ... - AssertionError: -1 is not false SUBFAILED(item=True) ... - AssertionError: True is not false SUBFAILED(item=1) ... - AssertionError: 1 is not false SUBFAILED(item=True) ... - AssertionError: True is not false SUBFAILED(item=-0.1) ... - AssertionError: -0.1 is not false SUBFAILED(item=True) ... - AssertionError: True is not false SUBFAILED(item=0.1) ... - AssertionError: 0.1 is not false SUBFAILED(item=True) ... - AssertionError: True is not false SUBFAILED(item='text') ... - AssertionError: 'text' is not false SUBFAILED(item=True) ... - AssertionError: True is not false SUBFAILED(item=(1, 2, 3, 'n')) ... - AssertionError: (1, 2, 3, 'n') is not false SUBFAILED(item=True) ... - AssertionError: True is not false SUBFAILED(item=[1, 2, 3, 'n']) ... - AssertionError: [1, 2, 3, 'n'] is not false SUBFAILED(item=True) ... - AssertionError: True is not false SUBFAILED(item={1, 2, 3, 'n'}) ... - AssertionError: {1, 2, 3, 'n'} is not false SUBFAILED(item=True) ... - AssertionError: True is not false SUBFAILED(item={'key': 'value'}) ... - AssertionError: {'key': 'value'} is not false SUBFAILED(item=True) ... - AssertionError: True is not falseI change the assertion
55 self.assertTrue(true_item)the test is green again
I remove the commented lines
22 def test_what_is_true(self): 23 self.assertIsInstance(True, (bool, int)) 24 self.assertNotIsInstance(True, float) 25 26 for true_item in ( 27 True, 28 -1, bool(-1), 1, bool(1), 29 -0.1, bool(-0.1), 0.1, bool(0.1), 30 "text", bool("text"), 31 ((1, 2, 3, 'n')), bool((1, 2, 3, 'n')), 32 [1, 2, 3, 'n'], bool([1, 2, 3, 'n']), 33 {1, 2, 3, 'n'}, bool({1, 2, 3, 'n'}), 34 {'key': 'value'}, bool({'key': 'value'}), 35 ): 36 with self.subTest(item=true_item): 37 self.assertTrue(true_item) 38 39 40# NOTES
test_the_value_of_false
The add function returned numbers in the calculation with True and False because they are integers. I want to know what their values are
RED: make it fail
I add a new test to find out the value of False
36 with self.subTest(item=true_item):
37 self.assertTrue(true_item)
38
39 def test_the_value_of_false(self):
40 self.assertEqual(False+1, None)
41
42
43# NOTES
the terminal shows AssertionError
AssertionError: 1 != None
False is 0
GREEN: make it pass
I change the expectation to match
40 self.assertEqual(False+1, 1)
the test passes
REFACTOR: make it better
I add another assertion
39 def test_the_value_of_false(self): 40 self.assertEqual(False+1, 1) 41 self.assertEqual(False-1, 1)the terminal shows AssertionError
AssertionError: -1 != 1False is
0I change the expectation
41 self.assertEqual(False-1, -1)the test passes
I add another assertion
39 def test_the_value_of_false(self): 40 self.assertEqual(False+1, 1) 41 self.assertEqual(False-1, -1) 42 self.assertEqual(False*1, -1)the terminal shows AssertionError
AssertionError: 0 != -1False is
0I change the expectation
38 self.assertEqual(False*1, 0)the test passes
what happens if I divide a number by False?
39 def test_the_value_of_false(self): 40 self.assertEqual(False+1, 1) 41 self.assertEqual(False-1, -1) 42 self.assertEqual(False*1, 0) 43 1 / Falsethe terminal shows ZeroDivisionError because False is
0I add assertRaises
39 def test_the_value_of_false(self): 40 self.assertEqual(False+1, 1) 41 self.assertEqual(False-1, -1) 42 self.assertEqual(False*1, 0) 43 with self.assertRaises(ZeroDivisionError): 44 1 / False 45 46 47# NOTESthe test passes
I add a comment
64# 0 is False 65# None is False 66# False is False 67# False is not true 68# False is a boolean 69# False is an integer 70# False is not a float 71# False is 0
test_the_value_of_true
RED: make it fail
I add a new test to find out the value of True
39 def test_the_value_of_false(self):
40 self.assertEqual(False+1, 1)
41 self.assertEqual(False-1, -1)
42 self.assertEqual(False*1, 0)
43 with self.assertRaises(ZeroDivisionError):
44 1 / False
45
46 def test_the_value_of_true(self):
47 self.assertEqual(True+1, 1)
48
49
50# NOTES
the terminal shows AssertionError
AssertionError: 2 != 1
True is 1
GREEN: make it pass
I change the expectation
47 self.assertEqual(True+1, 2)
the test passes
REFACTOR: make it better
I add another assertion
46 def test_the_value_of_true(self): 47 self.assertEqual(True+1, 2) 48 self.assertEqual(True-1, 2)the terminal shows AssertionError
AssertionError: 0 != 2True is
1I change the expectation
48 self.assertEqual(True-1, 0)the test passes
I add an assertion
46 def test_the_value_of_true(self): 47 self.assertEqual(True+1, 2) 48 self.assertEqual(True-1, 0) 49 self.assertEqual(True*1, 0)the terminal shows AssertionError
AssertionError: 1 != 0True is
1I change the expectation
49 self.assertEqual(True*1, 1)the test passes
I add an assertion for division
46 def test_the_value_of_true(self): 47 self.assertEqual(True+1, 2) 48 self.assertEqual(True-1, 0) 49 self.assertEqual(True*1, 1) 50 self.assertEqual(True/2, 1)the terminal shows AssertionError
AssertionError: 0.5 != 1True is
1I change the assertion
46 def test_the_value_of_true(self): 47 self.assertEqual(True+1, 2) 48 self.assertEqual(True-1, 0) 49 self.assertEqual(True*1, 1) 50 self.assertEqual(True/1, 1) 51 52 53# NOTESthe test passes
I add a comment
53# NOTES 54# a dictionary with things is True 55# a set with things is True 56# a list with things is True 57# a tuple with things is True 58# a string with things is True 59# positive and negative numbers are True 60# True is True 61# True is not false 62# True is a boolean 63# True is an integer 64# True is not a float 65# True is 1 66# the empty dictionary is False 67# the empty set is False 68# the empty list is False 69# the empty tuple is False 70# the empty string is False 71# 0 is False 72# None is False 73# False is False 74# False is not true 75# False is a boolean 76# False is an integer 77# False is not a float 78# False is 0 79 80 81# Exceptions seen 82# AssertionError
close the project
review
I added assertions that show booleans are also integers and NOT floats, then I added these tests
test_the_value_of_false which showed that False is
0test_the_value_of_true which showed that True is
1
code from the chapter
what is next?
you know
rate pumping python
If this has been a 7 star experience for you, please CLICK HERE to leave a 5 star review of pumping python. It helps other people get into the book too