what are booleans?


Imagine we divide the universe in two and the options are things that are the True group and the False group. These are the booleans: True and False.

I used assertIs and assertIsNot in test_assertion_error_w_false and test_assertion_error_w_true in the assertion_error project, where I saw that

I want to test what groups Python places the objects seen so far (None, integers, floats, strings, tuples, lists, sets and dictionaries). Which do you think will be in the False group or the True group?

preview

I have these tests by the end of the chapter

booleans/tests/test_booleans.py
 1import unittest
 2
 3
 4class TestBooleans(unittest.TestCase):
 5
 6    def test_what_is_false(self):
 7        self.assertIsInstance(False, (bool, int))
 8        self.assertNotIsInstance(
 9            False,
10            (
11                float, tuple, str,
12                list, set, dict,
13            )
14        )
15        self.assertIs(False, False)
16        self.assertIs(bool(False), False)
17        self.assertFalse(bool(False))
18        self.assertFalse(False)
booleans/tests/test_booleans.py
20    def test_what_is_true(self):
21        self.assertIsInstance(True, (bool, int))
22        self.assertNotIsInstance(
23            True,
24            (
25                float, tuple, str,
26                list, set, dict,
27            )
28        )
29        self.assertIs(True, True)
30        self.assertIs(bool(True), True)
31        self.assertTrue(bool(True))
32        self.assertTrue(True)
booleans/tests/test_booleans.py
34    def assert_is_not_the_same_as_false_or_true(self, an_object):
35        self.assertNotEqual(an_object, False)
36        self.assertIsNot(an_object, False)
37        self.assertNotEqual(an_object, True)
38        self.assertIsNot(an_object, True)
39
40    def assert_is_falsy(self, an_object):
41        self.assert_is_not_the_same_as_false_or_true(an_object)
42        self.assertFalse(bool(an_object))
43        self.assertFalse(an_object)
booleans/tests/test_booleans.py
45    def assert_is_truthy(self, an_object):
46        self.assert_is_not_the_same_as_false_or_true(an_object)
47        self.assertTrue(bool(an_object))
48        self.assertTrue(an_object)
49
50    def test_is_none_falsy_or_truthy(self):
51        self.assert_is_falsy(None)
booleans/tests/test_booleans.py
53    def test_is_an_integer_falsy_or_truthy(self):
54        self.assert_is_truthy(-1)
55
56        self.assertEqual(0, False)
57        self.assertIsNot(0, False)
58        self.assertNotEqual(0, True)
59        self.assertIsNot(0, True)
60        self.assertFalse(bool(0))
61        self.assertFalse(0)
62
63        a_positive_integer = 1
64        self.assertNotEqual(a_positive_integer, False)
65        self.assertIsNot(a_positive_integer, False)
66        self.assertEqual(1, True)
67        self.assertIsNot(a_positive_integer, True)
68        self.assertTrue(bool(a_positive_integer))
69        self.assertTrue(a_positive_integer)
booleans/tests/test_booleans.py
71    def test_is_a_float_falsy_or_truthy(self):
72        self.assert_is_truthy(-0.1)
73
74        self.assertEqual(0.0, False)
75        self.assertIsNot(0.0, False)
76        self.assertNotEqual(0.0, True)
77        self.assertIsNot(0.0, True)
78        self.assertFalse(bool(0.0))
79        self.assertFalse(0.0)
80
81        self.assert_is_truthy(0.1)
booleans/tests/test_booleans.py
83    def test_is_a_string_falsy_or_truthy(self):
84        self.assert_is_falsy(str())
85        self.assert_is_truthy('string with things')
86
87    def test_is_a_tuple_falsy_or_truthy(self):
88        self.assert_is_falsy(tuple())
89        self.assert_is_truthy((0, 1, 2, 'n'))
booleans/tests/test_booleans.py
 91    def test_is_a_list_falsy_or_truthy(self):
 92        self.assert_is_falsy(list())
 93        self.assert_is_truthy([0, 1, 2, 'n'])
 94
 95    def test_is_a_set_falsy_or_truthy(self):
 96        self.assert_is_falsy(set())
 97        self.assert_is_truthy({0, 1, 2, 'n'})
 98
 99    def test_is_a_dictionary_falsy_or_truthy(self):
100        self.assert_is_falsy(dict())
101        self.assert_is_truthy({'key': 'value'})
booleans/tests/test_booleans.py
104# NOTES
105# the value of True is 1
106# bool(a dictionary with things) is True
107# bool(a set with things) is True
108# bool(a list with things) is True
109# bool(a tuple with things) is True
110# bool(a string with things) is True
111# bool(a positive number) is True
112# bool(a negative number) is True
113# True is True
114# True is an integer
115# True is a boolean
116# True is NOT False
booleans/tests/test_booleans.py
117# the value of False is 0
118# bool(the empty dictionary) is False
119# bool(the empty set) is False
120# bool(the empty list) is False
121# bool(the empty tuple) is False
122# bool(the empty string) is False
123# bool(zero) is False
124# bool(None) is False
125# False is False
126# False is an integer
127# False is a boolean
128# False is NOT True
129
130
131# Exceptions seen
132# AssertionError

questions about Booleans

Questions to think about as we divide Python’s basic objects into False and True


start the project

  • I name this project booleans

  • I open a terminal

  • I use uv to make a directory for the project and initialize it

    terminal
    uv init booleans
    
  • I change directory to the project

    terminal
    cd booleans
    
  • I make a directory for the tests

    mkdir tests
    
  • I make the tests directory a Python package

    Danger

    use 2 underscores (__) before and after init for __init__.py not _init_.py

    touch tests/__init__.py
    
    New-Item tests/__init__.py
    
  • I make an empty file for the tests in the tests folder

    touch tests/test_booleans.py
    
    New-Item tests/test_booleans.py
    
  • I open test_booleans.py

  • I add the first failing test to test_booleans.py

    1import unittest
    2
    3
    4class TestBooleans(unittest.TestCase):
    5
    6    def test_failure(self):
    7        self.assertEqual(False, True)
    
  • I go back to the terminal

  • I use echo to add pytest to requirements.txt

    echo "pytest" > requirements.txt
    
  • I add pytest-watcher to the requirements file

    echo "pytest-watcher" >> requirements.txt
    

    the terminal goes back to the command line.

  • I use Out-File to add pytest to requirements.txt

    "pytest" | Out-File requirements.txt -Encoding UTF8
    
  • I add pytest-watcher to the requirements file

    "pytest-watcher" >> requirements.txt
    
  • I use uv to install pytest-watcher with the requirements file

    terminal
    uv add --requirement requirements.txt
    
  • I add the new files and folder to git for tracking

    terminal
    git add .
    
  • I add a git commit message

    terminal
    git commit --all --message \
    'setup project'
    
  • I use pytest-watcher to run the tests automatically

    terminal
    uv run pytest-watcher . --now --patterns "**/*.py"
    

    the terminal is my friend, and shows AssertionError

    terminal
    ======================== FAILURES ========================
    _______________ TestBooleans.test_failure ________________
    
    self = <tests.test_booleans.TestBooleans testMethod=test_failure>
    
        def test_failure(self):
    >       self.assertEqual(False, True)
    E       AssertionError: False != True
    
    tests/test_booleans.py:7: AssertionError
    ================ short test summary info =================
    FAILED tests/test_booleans.py::TestBooleans::test_failure - AssertionError: False != True
    =================== 1 failed in X.YZs ====================
    

    if the terminal does not show the same error, then check if

    • your tests/__init__.py has two underscores (__) before and after init for __init__.py not _init_.py

    • you ran echo "pytest-watcher" >> requirements.txt, to add pytest-watcher to the requirements file

    and try uv run pytest-watcher . --now --patterns "**/*.py" again

  • I add AssertionError to the list of Exceptions seen, in test_booleans.py

  • I change True to False in the assertion

    the test passes.


test_what_is_false

RED: make it fail


 4class TestBooleans(unittest.TestCase):
 5
 6    def test_what_is_false(self):
 7        self.assertNotIsInstance(False, bool)
 8
 9
10# Exceptions seen
11# AssertionError

the terminal is my friend, and shows AssertionError

AssertionError: False is an instance
                of <class 'bool'>

this was also in test_is_none_a_boolean.


GREEN: make it pass


I change assertNotIsInstance to the assertIsInstance method

 6    def test_what_is_false(self):
 7        # self.assertNotIsInstance(False, bool)
 8        self.assertIsInstance(False, bool)
 9
10
11# Exceptions seen

the test passes.


REFACTOR: make it better



how to test if something is an instance of more than one object

The instance methods can take a tuple of objects.


how to test if something is grouped as False

I can test if Python groups an object as False with the bool built-in function from The Python Standard Library. It returns False or True for the object given in parentheses.


RED: make it fail


  • I add an assertion with a call to bool

    the terminal is my friend, and shows AssertionError

    AssertionError: unexpectedly identical: False
    

    because this happens when assertIsNot(bool(False), False) runs

    assertIsNot(bool(False), False)
    assertIsNot(False      , False)
    
  • I add a comment


GREEN: make it pass


I change the assertIsNot to assertIs to make the statement True

33        # self.assertIsNot(False, False)
34        self.assertIs(False, False)
35        # self.assertIsNot(bool(False), False)
36        self.assertIs(bool(False), False)
37
38
39# NOTES

the test passes because bool(False) is equal to False.


another way to test if something is grouped as False


The unittest.TestCase class has a method I can use to test if the result of calling the bool built-in function with an object is False - assertFalse.

assertFalse(an_object)

which is like

assertIs(bool(an_object), False)

it raises AssertionError if the object in parentheses is grouped as True.


RED: make it fail


  • I add assertFalse to the test

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    because this happens when assertFalse(bool(True)) runs

    assertFalse(bool(True))
    assertFalse(True)
    

    which raises AssertionError since the result of bool(True) is True, not False.

  • I add a comment


GREEN: make it pass


I change True to False in the parentheses

35        # self.assertIsNot(bool(False), False)
36        self.assertIs(bool(False), False)
37        # self.assertFalse(bool(True))
38        self.assertFalse(bool(False))
39
40
41# NOTES

the test passes because bool(False) is False.


REFACTOR: make it better



test_what_is_true

RED: make it fail


I add test_what_is_true with assertNotIsInstance

17        self.assertFalse(bool(False))
18        self.assertFalse(False)
19
20    def test_what_is_true(self):
21        self.assertNotIsInstance(True, (bool, int))
22
23
24# NOTES

the terminal is my friend, and shows AssertionError

AssertionError: True is an instance
                of <class 'bool'>

this was also in test_is_none_a_boolean, without the tuple of objects.


GREEN: make it pass


I change assertNotIsInstance to the assertIsInstance method

18          self.assertFalse(False)
19
20      def test_what_is_true(self):
21          # self.assertNotIsInstance(True, (bool, int))
22          self.assertIsInstance(True, (bool, int))
23
24
25  # NOTES

the test passes.


REFACTOR: make it better



how to test if something is grouped as True

I can test if Python groups an object as True with the bool built-in function since it returns False or True for the object given in parentheses.


RED: make it fail


  • I add an assertion with a call to bool

    the terminal is my friend, and shows AssertionError

    AssertionError: unexpectedly identical: True
    

    because this happens when assertIsNot(bool(True), True) runs

    assertIsNot(bool(True), True)
    assertIsNot(True      , True)
    
  • I add a comment


GREEN: make it pass


I change assertIsNot to assertIs

31        # self.assertIsNot(True, True)
32        self.assertIs(True, True)
33        # self.assertIsNot(bool(True), True)
34        self.assertIs(bool(True), True)
35
36
37# NOTES

the test passes because bool(True) is True.


another way to test if something is grouped as True


The unittest.TestCase class has a method I can use to test if the result of calling the bool built-in function with an object is True - assertTrue.

assertTrue(an_object)

which is like

assertIs(bool(an_object), True)

it raises AssertionError if the object in parentheses is grouped as False.


RED: make it fail


  • I add assertTrue to the test

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

    because this happens when assertTrue(bool(False)) runs

    assertTrue(bool(False))
    assertTrue(False)
    

    which raises AssertionError since the result of bool(False) is False not True.

  • I add a comment


GREEN: make it pass


I change False to True in the parentheses

33        # self.assertIsNot(bool(True), True)
34        self.assertIs(bool(True), True)
35        # self.assertTrue(bool(False))
36        self.assertTrue(bool(True))
37
38
39# NOTES

the test passes because bool(True) is True.


REFACTOR: make it better


True is a boolean.


test_is_none_falsy_or_truthy

Is None grouped as False or True?

RED: make it fail



GREEN: make it pass


I change assertEqual to assertNotEqual for None, False

34    def test_is_none_falsy_or_truthy(self):
35        # self.assertEqual(None, False)
36        self.assertNotEqual(None, False)
37
38
39# NOTES

the test passes.


REFACTOR: make it better



test_is_an_integer_falsy_or_truthy

Is an integer grouped as False or True?

RED: make it fail



GREEN: make it pass


I change assertEqual to assertNotEqual for -1, False

42    def test_is_an_integer_falsy_or_truthy(self):
43        # self.assertEqual(-1, False)
44        self.assertNotEqual(-1, False)
45
46
47# NOTES

the test passes.


REFACTOR: make it better



test_is_a_float_falsy_or_truthy

Is a float grouped as False or True?

RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test for if a float (a binary floating point decimal number) is grouped as False or True

    the terminal is my friend, and shows AssertionError

    AssertionError: -0.1 != False
    

GREEN: make it pass


I change assertEqual to assertNotEqual for a_negative_float, False

66    def test_is_a_float_falsy_or_truthy(self):
67        a_negative_float = -0.1
68        # self.assertEqual(a_negative_float, False)
69        self.assertNotEqual(a_negative_float, False)
70
71
72# NOTES

the test passes.


REFACTOR: make it better



  • I add a git commit message in the other terminal

    terminal
    git commit --all --message \
    'add test_is_a_float_falsy_or_truthy'
    

‘0.0’ is grouped as False. Positive and Negative floats are grouped as True.


test_is_a_string_falsy_or_truthy

Is a string grouped as False or True?

RED: make it fail



GREEN: make it pass


I change assertEqual to assertNotEqual for str(), False

88        self.assertTrue(a_positive_float)
89
90    def test_is_a_string_falsy_or_truthy(self):
91        # self.assertEqual(str(), False)
92        self.assertNotEqual(str(), False)
93
94
95# NOTES

the test passes.


REFACTOR: make it better


The empty string is grouped as False. A string with things is grouped as True.


test_is_a_tuple_falsy_or_truthy

Is a tuple grouped as False or True?

RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test for if a tuple (anything in parentheses ( ) separated by a comma) is grouped as False or True

    the terminal is my friend, and shows AssertionError

    AssertionError: () != False
    

    the empty tuple is not equal to False


GREEN: make it pass


I change assertEqual to assertNotEqual for tuple(), False

104        self.assertTrue(a_string)
105
106    def test_is_a_tuple_falsy_or_truthy(self):
107        # self.assertEqual(tuple(), False)
108        self.assertNotEqual(tuple(), False)
109
110
111# NOTES

the test passes.


REFACTOR: make it better


The empty tuple is grouped as False. A tuple with things is grouped as True.


test_is_a_list_falsy_or_truthy

Is a list grouped as False or True?

RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test for if a list (anything in square brackets [ ]) is grouped as False or True

    the terminal is my friend, and shows AssertionError

    AssertionError: [] != False
    

    the empty list is not equal to False


GREEN: make it pass


I change assertEqual to assertNotEqual for list(), False

120        self.assertTrue(a_tuple)
121
122    def test_is_a_list_falsy_or_truthy(self):
123        # self.assertEqual(list(), False)
124        self.assertNotEqual(list(), False)
125
126
127# NOTES

the test passes.


REFACTOR: make it better


The empty list is grouped as False. A list with things is grouped as True.


test_is_a_set_falsy_or_truthy

Is a set grouped as False or True?

RED: make it fail



GREEN: make it pass


I change assertEqual to assertNotEqual for set(), False

136        self.assertTrue(a_list)
137
138    def test_is_a_set_falsy_or_truthy(self):
139        # self.assertEqual(set(), False)
140        self.assertNotEqual(set(), False)
141
142
143# NOTES

the test passes.


REFACTOR: make it better


The empty set is grouped as False. A set with things is grouped as True.


test_is_a_dictionary_falsy_or_truthy

Is a dictionary grouped as False or True?

RED: make it fail



GREEN: make it pass


I change assertEqual to assertNotEqual for dict(), False

152        self.assertTrue(a_set)
153
154    def test_is_a_dictionary_falsy_or_truthy(self):
155        # self.assertEqual(dict(), False)
156        self.assertNotEqual(dict(), False)
157
158
159# NOTES

the test passes.


REFACTOR: make it better


The empty dictionary is grouped as False. A dictionary with things is grouped as True.


extract assert_is_falsy method

All the objects had the same assertions when testing if something is grouped as False

self.assertNotEqual(an_object, False)
self.assertIsNot(an_object, False)
self.assertNotEqual(an_object, True)
self.assertIsNot(an_object, True)
self.assertFalse(bool(an_object))
self.assertFalse(an_object)

I can make a function to remove the repetition.


RED: make it fail



GREEN: make it pass



REFACTOR: make it better



extract assert_is_truthy method

All the objects had the same assertions when testing if something is grouped as True

self.assertNotEqual(an_object, False)
self.assertIsNot(an_object, False)
self.assertNotEqual(an_object, True)
self.assertIsNot(an_object, True)
self.assertTrue(bool(an_object))
self.assertTrue(an_object)

I can make a method like I did with :ref:`assert_is_falsy<extract assert_is_falsy method>`to remove the repetition.


RED: make it fail



GREEN: make it pass



REFACTOR: make it better



extract assert_is_not_the_same_as_false_or_true method

The assert_is_falsy and assert_is_truthy methods use the same assertion to make sure that the object they receive is not the same as False or True.

self.assertNotEqual(an_object, False)
self.assertIsNot(an_object, False)
self.assertNotEqual(an_object, True)
self.assertIsNot(an_object, True)

I can add a method to remove this repetition.


REFACTOR: make it better


In Python the value of True is 1.


close the project

  • I close test_booleans.py

  • I click in the terminal where the tests are running

  • I use q on the keyboard to leave the tests. The terminal goes back to the command line.

  • I change directory to the parent of booleans

    terminal
    cd ..
    

    the terminal shows

    .../pumping_python
    

review

I know that bool is an object. It only has two instances - False and True

In Python the following objects are grouped as

This comes in handy when I want programs to make decisions, because they can choose what to do based on if an object is grouped as False (0, empty or None ) or is grouped as True (positive and negative numbers or has something in it).

I can use assertFalse in place of assertIs(bool(x), False) and assertTrue for assertIs(bool(x), True).

How many questions can you answer after going through this chapter?


code from the chapter

Do you want to see all the CODE I typed in this chapter?


what is next?

So far I have done the same steps to make a Python Test Driven Development environment. Would you like to see me turn those steps into a program to automatically make a Test Driven Development project with one command?.


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.