booleans: only two


I want to test what groups the objects seen so far - None, integers, floats, strings, tuples, lists, sets and dictionaries, fall into Python divides them into two groups - False and True. What do you think will happen?

preview

I have these tests by the end of the chapter

  1import unittest
  2
  3
  4class TestBooleans(unittest.TestCase):
  5
  6    def test_what_is_false(self):
  7        self.assertIsInstance(False, bool)
  8        self.assertIs(False, False)
  9        self.assertEqual(bool(False), False)
 10        self.assertFalse(bool(False))
 11        self.assertFalse(False)
 12
 13    def test_what_is_true(self):
 14        self.assertIsInstance(True, bool)
 15        self.assertIs(True, True)
 16        self.assertEqual(bool(True), True)
 17        self.assertTrue(bool(True))
 18        self.assertTrue(True)
 19
 20    def test_is_none_falsy_or_truthy(self):
 21        self.assertFalse(bool(None))
 22        self.assertFalse(None)
 23
 24    def test_is_an_integer_falsy_or_truthy(self):
 25        a_negative_integer = -1
 26        self.assertTrue(bool(a_negative_integer))
 27        self.assertTrue(a_negative_integer)
 28
 29        self.assertFalse(bool(0))
 30        self.assertFalse(0)
 31
 32        a_positive_integer = 1
 33        self.assertTrue(bool(a_positive_integer))
 34        self.assertTrue(a_positive_integer)
 35
 36    def test_is_a_float_falsy_or_truthy(self):
 37        a_negative_float = -0.1
 38        self.assertTrue(bool(a_negative_float))
 39        self.assertTrue(a_negative_float)
 40
 41        self.assertFalse(bool(0.0))
 42        self.assertFalse(0.0)
 43
 44        a_positive_float = 0.1
 45        self.assertTrue(bool(a_positive_float))
 46        self.assertTrue(0.1)
 47
 48    def test_is_a_string_falsy_or_truthy(self):
 49        self.assertFalse(bool(str()))
 50        self.assertFalse(str())
 51
 52        a_string = "a string with things"
 53        self.assertTrue(bool(a_string))
 54        self.assertTrue(a_string)
 55
 56    def test_is_a_tuple_falsy_or_truthy(self):
 57        self.assertFalse(bool(tuple()))
 58        self.assertFalse(tuple())
 59
 60        a_tuple = (0, 1, 2, 'n')
 61        self.assertTrue(bool(a_tuple))
 62        self.assertTrue(a_tuple)
 63
 64    def test_is_a_list_falsy_or_truthy(self):
 65        self.assertFalse(bool(list()))
 66        self.assertFalse(list())
 67
 68        a_list = [0, 1, 2, 'n']
 69        self.assertTrue(bool(a_list))
 70        self.assertTrue(a_list)
 71
 72    def test_is_a_set_falsy_or_truthy(self):
 73        self.assertFalse(bool(set()))
 74        self.assertFalse(set())
 75
 76        a_set = {0, 1, 2, 'n'}
 77        self.assertTrue(bool(a_set))
 78        self.assertTrue(a_set)
 79
 80    def test_is_a_dictionary_falsy_or_truthy(self):
 81        self.assertFalse(bool(dict()))
 82        self.assertFalse(dict())
 83
 84        a_dictionary = {'key': 'value'}
 85        self.assertTrue(bool(a_dictionary))
 86        self.assertTrue(a_dictionary)
 87
 88
 89# NOTES
 90# bool(a dictionary with things) is True
 91# bool(a set with things) is True
 92# bool(a list with things) is True
 93# bool(a tuple with things) is True
 94# bool(a string with things) is True
 95# bool(a positive number) is True
 96# bool(a negative number) is True
 97# True is NOT False
 98# True is NOT equal to False
 99# True is a boolean
100# bool(the empty dictionary) is False
101# bool(the empty set) is False
102# bool(the empty list) is False
103# bool(the empty tuple) is False
104# bool(the empty string) is False
105# bool(zero) is False
106# bool(None) is False
107# False is NOT True
108# False is NOT equal to True
109# False is a boolean
110
111
112# Exceptions seen
113# AssertionError

questions about Booleans

Questions to think about as we divide Python’s basic data structures 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

    uv init booleans
    

    the terminal shows

    Initialized project `booleans`
    at `.../pumping_python/booleans`
    

    then goes back to the command line.

  • I change directory to the project

    cd booleans
    

    the terminal shows I am in the booleans folder

    .../pumping_python/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
    

    the terminal goes back to the command line.

  • I use the mv program to change the name of main.py to test_booleans.py and move it to the tests folder

    mv main.py tests/test_booleans.py
    
    Move-Item main.py tests/test_booleans.py
    

    the terminal goes back to the command line.

  • 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.assertFalse(True)
    
  • I go back to the terminal to make a requirements file for the Python packages I need

    echo "pytest" > requirements.txt
    

    the terminal goes back to the command line.

  • I add pytest-watcher to the requirements file

    echo "pytest-watcher" >> requirements.txt
    

    the terminal goes back to the command line.

  • I use uv to install pytest-watcher with the requirements file

    uv add --requirement requirements.txt
    

    the terminal shows that it installed pytest-watcher and its dependencies.

  • I add the new files and folder to git for tracking

    git add .
    

    the terminal goes back to the command line.

  • I add a git commit message

    git commit -all --message 'setup project'
    

    the terminal shows a summary of the changes then goes back to the command line.

  • I use pytest-watcher to run the tests automatically

    uv run pytest-watcher . --now
    

    the terminal is my friend, and shows AssertionError

    ======================== FAILURES ========================
    _______________ TestBooleans.test_failure ________________
    
    self = <tests.test_booleans.TestBooleans testMethod=test_failure>
    
        def test_failure(self):
    >       self.assertFalse(True)
    E       AssertionError: True is not false
    
    tests/test_booleans.py:7: AssertionError
    ================ short test summary info =================
    FAILED tests/test_booleans.py::TestBooleans::test_failure - AssertionError: True is not false
    =================== 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

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

    fix those errors and try to run uv run pytest-watcher . --now again

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

     4class TestBooleans(unittest.TestCase):
     5
     6    def test_failure(self):
     7        self.assertFalse(True)
     8
     9
    10# Exceptions seen
    11# AssertionError
    
  • I change True to False in the assertion

    7        self.assertFalse(False)
    

    the test passes.


test_what_is_false


RED: make it fail


I change test_failure to test_what_is_false, then use the assertNotIsInstance method I learned from everything is an object to check if False is an instance (a copy) of the bool class

 1import unittest
 2
 3
 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


  • I add a comment

     6    def test_what_is_false(self):
     7        # self.assertNotIsInstance(False, bool)
     8        self.assertIsInstance(False, bool)
     9
    10
    11# NOTES
    12# False is a boolean
    13
    14
    15# Exceptions seen
    16# AssertionError
    

    I know this from testing None.

  • I use the assertIsNot method like I did in test_assertion_error_w_false

     6    def test_what_is_false(self):
     7        # self.assertNotIsInstance(False, bool)
     8        self.assertIsInstance(False, bool)
     9        self.assertIsNot(False, False)
    10
    11
    12# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: unexpectedly identical: False
    

    because False is False.

  • I change assertIsNot to assertIs

     6    def test_what_is_false(self):
     7        # self.assertNotIsInstance(False, bool)
     8        self.assertIsInstance(False, bool)
     9        # self.assertIsNot(False, False)
    10        self.assertIs(False, False)
    11
    12
    13# NOTES
    

    the test passes. I know this from test_assertion_error_w_false.


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.


RED: make it fail


  • I add an assertion with a call to bool

     6    def test_what_is_false(self):
     7        # self.assertNotIsInstance(False, bool)
     8        self.assertIsInstance(False, bool)
     9        # self.assertIsNot(False, False)
    10        self.assertIs(False, False)
    11        self.assertEqual(bool(False), True)
    12
    13
    14# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False != True
    

    because this happens when self.assertEqual(bool(False), True) runs

    self.assertEqual(bool(False), True)
    self.assertEqual(False      , True)
    
    • which raises AssertionError since False, which is the result of bool(False) is not equal to True

    • bool(anything) returns False or True for the thing in parentheses

  • I add a comment

    14# NOTES
    15# False is NOT equal to True
    16# False is a boolean
    17
    18
    19# Exceptions seen
    20# AssertionError
    

GREEN: make it pass


I change the expectation of the assertion to make it True

 6    def test_what_is_false(self):
 7        # self.assertNotIsInstance(False, bool)
 8        self.assertIsInstance(False, bool)
 9        # self.assertIsNot(False, False)
10        self.assertIs(False, False)
11        # self.assertEqual(bool(False), True)
12        self.assertEqual(bool(False), False)
13
14
15# 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, it was in the first failing test

self.assertFalse(True)

which is like

self.assertEqual(bool(True), False)

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


RED: make it fail


  • I add assertFalse to the test

     6    def test_what_is_false(self):
     7        # self.assertNotIsInstance(False, bool)
     8        self.assertIsInstance(False, bool)
     9        # self.assertIsNot(False, False)
    10        self.assertIs(False, False)
    11        # self.assertEqual(bool(False), True)
    12        self.assertEqual(bool(False), False)
    13        self.assertFalse(bool(True))
    14
    15
    16# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    because this happens when self.assertEqual(bool(False), True) runs

    self.assertFalse(bool(True))
    self.assertFalse(True)
    

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

  • I add a comment

    16# NOTES
    17# True is NOT False
    18# False is NOT equal to True
    19# False is a boolean
    

GREEN: make it pass


I change True to False in parentheses

 6    def test_what_is_false(self):
 7        # self.assertNotIsInstance(False, bool)
 8        self.assertIsInstance(False, bool)
 9        # self.assertIsNot(False, False)
10        self.assertIs(False, False)
11        # self.assertEqual(bool(False), True)
12        self.assertEqual(bool(False), False)
13        # self.assertFalse(bool(True))
14        self.assertFalse(bool(False))
15
16
17# NOTES

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


REFACTOR: make it better


  • It turns out that I can skip bool and get the same result. I add another call to the assertFalse method

     6    def test_what_is_false(self):
     7        # self.assertNotIsInstance(False, bool)
     8        self.assertIsInstance(False, bool)
     9        # self.assertIsNot(False, False)
    10        self.assertIs(False, False)
    11        # self.assertEqual(bool(False), True)
    12        self.assertEqual(bool(False), False)
    13        # self.assertFalse(bool(True))
    14        self.assertFalse(bool(False))
    15        self.assertFalse(True)
    16
    17
    18# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    because bool(True) is True.

  • I change True to False in the parentheses

     6    def test_what_is_false(self):
     7        # self.assertNotIsInstance(False, bool)
     8        self.assertIsInstance(False, bool)
     9        # self.assertIsNot(False, False)
    10        self.assertIs(False, False)
    11        # self.assertEqual(bool(False), True)
    12        self.assertEqual(bool(False), False)
    13        # self.assertFalse(bool(True))
    14        self.assertFalse(bool(False))
    15        # self.assertFalse(True)
    16        self.assertFalse(False)
    17
    18
    19# NOTES
    

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

    The assertFalse method raises AssertionError if the result of a call to the bool built-in function with an object is True.

  • I remove the commented lines

     6class TestBooleans(unittest.TestCase):
     7
     8        def test_what_is_false(self):
     9            self.assertIsInstance(False, bool)
    10            self.assertIs(False, False)
    11            self.assertEqual(bool(False), False)
    12            self.assertFalse(bool(False))
    13            self.assertFalse(False)
    14
    15
    16    # NOTES
    
  • I open a new terminal, then add a git commit message

    git commit --all --message 'add test_what_is_false'
    

False is a boolean.


test_what_is_true


RED: make it fail



GREEN: make it pass


I change assertNotIsInstance to the assertIsInstance method

13      def test_what_is_true(self):
14          # self.assertNotIsInstance(True, bool)
15          self.assertIsInstance(True, bool)
16
17
18  # NOTES

the test passes.


REFACTOR: make it better


  • I add a comment

    18# NOTES
    19# True is NOT False
    20# True is a boolean
    21# False is NOT equal to True
    22# False is a boolean
    

    I know this also from testing None.

  • I use the assertIsNot method like I did in test_assertion_error_w_true

    13    def test_what_is_true(self):
    14        # self.assertNotIsInstance(True, bool)
    15        self.assertIsInstance(True, bool)
    16        self.assertIsNot(True, True)
    17
    18
    19# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: unexpectedly identical: True
    

    because True is True.

  • I change assertIsNot to assertIs

    13    def test_what_is_true(self):
    14        # self.assertNotIsInstance(True, bool)
    15        self.assertIsInstance(True, bool)
    16        # self.assertIsNot(True, True)
    17        self.assertIs(True, True)
    18
    19
    20# NOTES
    

    the test passes. This was also in test_assertion_error_w_true.


how to test if something is grouped as True


I will use the bool built-in function from The Python Standard Library to test if Python groups an object as True.


RED: make it fail


  • I add an assertion with a call to bool

    13    def test_what_is_true(self):
    14        # self.assertNotIsInstance(True, bool)
    15        self.assertIsInstance(True, bool)
    16        # self.assertIsNot(True, True)
    17        self.assertIs(True, True)
    18        self.assertEqual(bool(True), False)
    19
    20
    21# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True != False
    

    because this happens when self.assertEqual(bool(True), False) runs

    self.assertEqual(bool(True), False)
    self.assertEqual(True      , False)
    
    • which raises AssertionError since True, which is the result of bool(True) is not equal to False

    • bool(anything) returns False or True for the thing in parentheses

  • I add a comment

    21# NOTES
    22# True is NOT False
    23# True is NOT equal to False
    24# True is a boolean
    25# False is NOT equal to True
    26# False is a boolean
    27
    28
    29# Exceptions seen
    30# AssertionError
    

GREEN: make it pass


I change the expectation of the assertion to make it True

13    def test_what_is_true(self):
14        # self.assertNotIsInstance(True, bool)
15        self.assertIsInstance(True, bool)
16        # self.assertIsNot(True, True)
17        self.assertIs(True, True)
18        # self.assertEqual(bool(True), False)
19        self.assertEqual(bool(True), True)
20
21
22# NOTES

the test passes because bool(True) is equal to 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. It raises AssertionError if the object in parentheses is grouped as False.


RED: make it fail


  • I add assertTrue to the test

    13    def test_what_is_true(self):
    14        # self.assertNotIsInstance(True, bool)
    15        self.assertIsInstance(True, bool)
    16        # self.assertIsNot(True, True)
    17        self.assertIs(True, True)
    18        # self.assertEqual(bool(True), False)
    19        self.assertEqual(bool(True), True)
    20        self.assertTrue(bool(False))
    21
    22
    23# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

    because this happens when self.assertEqual(bool(True), False) runs

    self.assertTrue(bool(False))
    self.assertTrue(False)
    

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

  • I add a comment

    23# NOTES
    24# True is NOT False
    25# True is NOT equal to False
    26# True is a boolean
    27# False is NOT True
    28# False is NOT equal to True
    29# False is a boolean
    

GREEN: make it pass


I change False to True in parentheses

13    def test_what_is_true(self):
14        # self.assertNotIsInstance(True, bool)
15        self.assertIsInstance(True, bool)
16        # self.assertIsNot(True, True)
17        self.assertIs(True, True)
18        # self.assertEqual(bool(True), False)
19        self.assertEqual(bool(True), True)
20        # self.assertTrue(bool(False))
21        self.assertTrue(bool(True))
22
23
24# NOTES

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


REFACTOR: make it better


  • I can skip bool and get the same result. I add another call to the assertTrue method

    13    def test_what_is_true(self):
    14        # self.assertNotIsInstance(True, bool)
    15        self.assertIsInstance(True, bool)
    16        # self.assertIsNot(True, True)
    17        self.assertIs(True, True)
    18        # self.assertEqual(bool(True), False)
    19        self.assertEqual(bool(True), True)
    20        # self.assertTrue(bool(False))
    21        self.assertTrue(bool(True))
    22        self.assertTrue(False)
    23
    24
    25# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

    because bool(False) is False.

  • I change False to True in the parentheses

    13    def test_what_is_true(self):
    14        # self.assertNotIsInstance(True, bool)
    15        self.assertIsInstance(True, bool)
    16        # self.assertIsNot(True, True)
    17        self.assertIs(True, True)
    18        # self.assertEqual(bool(True), False)
    19        self.assertEqual(bool(True), True)
    20        # self.assertTrue(bool(False))
    21        self.assertTrue(bool(True))
    22        # self.assertTrue(False)
    23        self.assertTrue(True)
    24
    25
    26# NOTES
    

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

    The assertTrue method raises AssertionError if the result of a call to the bool built-in function with an object is False.

  • I remove the commented lines

    13    def test_what_is_true(self):
    14        self.assertIsInstance(True, bool)
    15        self.assertIs(True, True)
    16        self.assertEqual(bool(True), True)
    17        self.assertTrue(bool(True))
    18        self.assertTrue(True)
    19
    20
    21# NOTES
    
  • I open a new terminal, then add a git commit message

    git commit --all --message 'add test_what_is_true'
    

True is a boolean.


test_is_none_falsy_or_truthy


RED: make it fail


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

  • I add a test to see if None is grouped as False or True

    13    def test_what_is_true(self):
    14        self.assertIsInstance(True, bool)
    15        self.assertIs(True, True)
    16        self.assertEqual(bool(True), True)
    17        self.assertTrue(bool(True))
    18        self.assertTrue(True)
    19
    20    def test_is_none_falsy_or_truthy(self):
    21        self.assertTrue(bool(None))
    22
    23
    24# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

    because the result of bool(None) is False.


GREEN: make it pass


I change assertTrue to assertFalse

20    def test_is_none_falsy_or_truthy(self):
21        # self.assertTrue(bool(None))
22        self.assertFalse(bool(None))
23
24
25# NOTES

the test passes.


REFACTOR: make it better


  • I add a comment

    25# NOTES
    26# True is NOT False
    27# True is NOT equal to False
    28# True is a boolean
    29# bool(None) is False
    30# False is NOT True
    31# False is NOT equal to True
    32# False is a boolean
    
  • I add an assertion without bool

    20    def test_is_none_falsy_or_truthy(self):
    21        # self.assertTrue(bool(None))
    22        self.assertFalse(bool(None))
    23        self.assertTrue(None)
    24
    25
    26# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: None is not true
    

    I know this from test_assertion_error_w_true.

  • I change assertTrue to assertFalse

    20    def test_is_none_falsy_or_truthy(self):
    21        # self.assertTrue(bool(None))
    22        self.assertFalse(bool(None))
    23        # self.assertTrue(None)
    24        self.assertFalse(None)
    25
    26
    27# NOTES
    
  • I remove the commented lines

    20    def test_is_none_falsy_or_truthy(self):
    21        self.assertFalse(bool(None))
    22        self.assertFalse(None)
    23
    24
    25# NOTES
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'add test_is_none_falsy_or_truthy'
    

None is grouped as False. The AssertionError chapter showed that None is not False and False is not None.


test_is_an_integer_falsy_or_truthy


RED: make it fail


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

  • I add a test for if an integer (a whole number without decimals) is grouped as False or True

    20    def test_is_none_falsy_or_truthy(self):
    21        self.assertFalse(bool(None))
    22        self.assertFalse(None)
    23
    24    def test_is_an_integer_falsy_or_truthy(self):
    25        self.assertFalse(bool(-1))
    26
    27
    28# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    
    • because the result of bool(-1) is True

    • I use -1 for all the integers (whole numbers without decimals) that are smaller than 0


GREEN: make it pass


I change assertFalse to assertTrue

24    def test_is_an_integer_falsy_or_truthy(self):
25        # self.assertFalse(bool(-1))
26        self.assertTrue(bool(-1))
27
28
29# NOTES

the test passes.


REFACTOR: make it better


  • I add a comment

    29# NOTES
    30# bool(-1) is True
    31# True is NOT False
    32# True is NOT equal to False
    33# True is a boolean
    34# bool(None) is False
    35# False is NOT True
    36# False is NOT equal to True
    37# False is a boolean
    
  • I add an assertion without bool

    24    def test_is_an_integer_falsy_or_truthy(self):
    25        # self.assertFalse(bool(-1))
    26        self.assertTrue(bool(-1))
    27        self.assertFalse(-1)
    28
    29
    30# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: -1 is not false
    

    because the result of bool(-1) is True.

  • I change assertFalse to assertTrue

    24    def test_is_an_integer_falsy_or_truthy(self):
    25        # self.assertFalse(bool(-1))
    26        self.assertTrue(bool(-1))
    27        # self.assertFalse(-1)
    28        self.assertTrue(-1)
    29
    30
    31# NOTES
    
  • I add a variable for -1

    24    def test_is_an_integer_falsy_or_truthy(self):
    25        a_negative_integer = -1
    26        # self.assertFalse(bool(-1))
    27        self.assertTrue(bool(-1))
    28        # self.assertFalse(-1)
    29        self.assertTrue(-1)
    30
    31
    32# NOTES
    
  • I use the variable to remove repetition of -1

    24    def test_is_an_integer_falsy_or_truthy(self):
    25        a_negative_integer = -1
    26        # self.assertFalse(bool(-1))
    27        # self.assertTrue(bool(-1))
    28        self.assertTrue(bool(a_negative_integer))
    29        # self.assertFalse(-1)
    30        # self.assertTrue(-1)
    31        self.assertTrue(a_negative_integer)
    32
    33
    34# NOTES
    

    the test is still green.

  • I add an assertion to test if 0 is grouped as False or True

    24    def test_is_an_integer_falsy_or_truthy(self):
    25        a_negative_integer = -1
    26        # self.assertFalse(bool(-1))
    27        # self.assertTrue(bool(-1))
    28        self.assertTrue(bool(a_negative_integer))
    29        # self.assertFalse(-1)
    30        # self.assertTrue(-1)
    31        self.assertTrue(a_negative_integer)
    32
    33        self.assertTrue(bool(0))
    34
    35
    36# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

    because the result of bool(0) is False.

  • I change assertTrue to assertFalse

    24    def test_is_an_integer_falsy_or_truthy(self):
    25        a_negative_integer = -1
    26        # self.assertFalse(bool(-1))
    27        # self.assertTrue(bool(-1))
    28        self.assertTrue(bool(a_negative_integer))
    29        # self.assertFalse(-1)
    30        # self.assertTrue(-1)
    31        self.assertTrue(a_negative_integer)
    32
    33        # self.assertTrue(bool(0))
    34        self.assertFalse(bool(0))
    35
    36
    37# NOTES
    

    the test passes.

  • I add a comment

    37# NOTES
    38# bool(-1) is True
    39# True is NOT False
    40# True is NOT equal to False
    41# True is a boolean
    42# bool(0) is False
    43# bool(None) is False
    44# False is NOT True
    45# False is NOT equal to True
    46# False is a boolean
    
  • I add an assertion without bool

    33        # self.assertTrue(bool(0))
    34        self.assertFalse(bool(0))
    35        self.assertTrue(0)
    36
    37
    38# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 0 is not true
    

    because the result of bool(0) is False.

  • I change assertTrue to assertFalse

    33        # self.assertTrue(bool(0))
    34        self.assertFalse(bool(0))
    35        # self.assertTrue(0)
    36        self.assertFalse(0)
    37
    38
    39# NOTES
    
  • I add an assertion to test if 1 is grouped as False or True

    33        # self.assertTrue(bool(0))
    34        self.assertFalse(bool(0))
    35        # self.assertTrue(0)
    36        self.assertFalse(0)
    37
    38        self.assertFalse(bool(1))
    39
    40
    41# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    
    • because the result of bool(1) is True

    • I use 1 for all the integers (whole numbers without decimals) that are bigger than 0

  • I change assertFalse to assertTrue

    33        # self.assertTrue(bool(0))
    34        self.assertFalse(bool(0))
    35        # self.assertTrue(0)
    36        self.assertFalse(0)
    37
    38        # self.assertFalse(bool(1))
    39        self.assertTrue(bool(1))
    40
    41
    42# NOTES
    

    the test passes.

  • I add a comment

    42# NOTES
    43# bool(1) is True
    44# bool(-1) is True
    45# True is NOT False
    46# True is NOT equal to False
    47# True is a boolean
    48# bool(0) is False
    49# bool(None) is False
    50# False is NOT True
    51# False is NOT equal to True
    52# False is a boolean
    
  • I add an assertion without bool

    38        # self.assertFalse(bool(1))
    39        self.assertTrue(bool(1))
    40        self.assertFalse(1)
    41
    42
    43# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 1 is not false
    

    because the result of bool(1) is True.

  • I change assertFalse to assertTrue

    38        # self.assertFalse(bool(1))
    39        self.assertTrue(bool(1))
    40        # self.assertFalse(1)
    41        self.assertTrue(1)
    42
    43
    44# NOTES
    
  • I add a variable for 1

    33        # self.assertTrue(bool(0))
    34        self.assertFalse(bool(0))
    35        # self.assertTrue(0)
    36        self.assertFalse(0)
    37
    38        a_positive_integer = 1
    39        # self.assertFalse(bool(1))
    40        self.assertTrue(bool(1))
    41        # self.assertFalse(1)
    42        self.assertTrue(1)
    
  • I use the variable to remove repetition of 1

    24        a_positive_integer = 1
    25        # self.assertFalse(bool(1))
    26        # self.assertTrue(bool(1))
    27        self.assertTrue(bool(a_positive_integer))
    28        # self.assertFalse(1)
    29        # self.assertTrue(1)
    30        self.assertTrue(a_positive_integer)
    31
    32
    33# NOTES
    

    the test is still green.

  • I remove the commented lines

    24    def test_is_an_integer_falsy_or_truthy(self):
    25        a_negative_integer = -1
    26        self.assertTrue(bool(a_negative_integer))
    27        self.assertTrue(a_negative_integer)
    28
    29        self.assertFalse(bool(0))
    30        self.assertFalse(0)
    31
    32        a_positive_integer = 1
    33        self.assertTrue(bool(a_positive_integer))
    34        self.assertTrue(a_positive_integer)
    35
    36
    37# NOTES
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'add test_is_an_integer_falsy_or_truthy'
    
  • ‘0’ is grouped as False, positive and negative integers are grouped as True

  • None is grouped as False


test_is_a_float_falsy_or_truthy


RED: make it fail


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

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

     3        a_positive_integer = 1
     4        self.assertTrue(bool(a_positive_integer))
     5        self.assertTrue(a_positive_integer)
     6
     7    def test_is_a_float_falsy_or_truthy(self):
     8        self.assertFalse(bool(-0.1))
     9
    10
    11# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    
    • because the result of bool(-0.1) is True

    • I use -0.1 for all the floats (binary floating point decimal numbers) that are smaller than 0.0


GREEN: make it pass


I change assertFalse to assertTrue

36    def test_is_a_float_falsy_or_truthy(self):
37        # self.assertFalse(bool(-0.1))
38        self.assertTrue(bool(-0.1))
39
40
41# NOTES

the test passes.


REFACTOR: make it better


  • I add a comment

    41# NOTES
    42# bool(-0.1) is True
    43# bool(1) is True
    44# bool(-1) is True
    45# True is NOT False
    46# True is NOT equal to False
    47# True is a boolean
    48# bool(0) is False
    49# bool(None) is False
    50# False is NOT True
    51# False is NOT equal to True
    52# False is a boolean
    
  • I add an assertion without bool

    36    def test_is_a_float_falsy_or_truthy(self):
    37        # self.assertFalse(bool(-0.1))
    38        self.assertTrue(bool(-0.1))
    39        self.assertFalse(-0.1)
    40
    41
    42# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: -0.1 is not false
    

    because the result of bool(-0.1) is True.

  • I change assertFalse to assertTrue

    36    def test_is_a_float_falsy_or_truthy(self):
    37        # self.assertFalse(bool(-0.1))
    38        self.assertTrue(bool(-0.1))
    39        # self.assertFalse(-0.1)
    40        self.assertTrue(-0.1)
    41
    42
    43# NOTES
    
  • I add a variable for -0.1

    36    def test_is_a_float_falsy_or_truthy(self):
    37        a_negative_float = -0.1
    38        # self.assertFalse(bool(-0.1))
    39        self.assertTrue(bool(-0.1))
    40        # self.assertFalse(-0.1)
    41        self.assertTrue(-0.1)
    42
    43
    44# NOTES
    
  • I use the variable to remove repetition of -0.1

    36    def test_is_a_float_falsy_or_truthy(self):
    37        a_negative_float = -0.1
    38        # self.assertFalse(bool(-0.1))
    39        # self.assertTrue(bool(-0.1))
    40        self.assertTrue(bool(a_negative_float))
    41        # self.assertFalse(-0.1)
    42        # self.assertTrue(-0.1)
    43        self.assertTrue(a_negative_float)
    44
    45
    46# NOTES
    

    the test is still green.

  • I add an assertion to test if 0.0 is grouped as False or True

    36    def test_is_a_float_falsy_or_truthy(self):
    37        a_negative_float = -0.1
    38        # self.assertFalse(bool(-0.1))
    39        # self.assertTrue(bool(-0.1))
    40        self.assertTrue(bool(a_negative_float))
    41        # self.assertFalse(-0.1)
    42        # self.assertTrue(-0.1)
    43        self.assertTrue(a_negative_float)
    44
    45        self.assertTrue(bool(0.0))
    46
    47
    48# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

    because the result of bool(0.0) is False.

  • I change assertTrue to assertFalse

    36    def test_is_a_float_falsy_or_truthy(self):
    37        a_negative_float = -0.1
    38        # self.assertFalse(bool(-0.1))
    39        # self.assertTrue(bool(-0.1))
    40        self.assertTrue(bool(a_negative_float))
    41        # self.assertFalse(-0.1)
    42        # self.assertTrue(-0.1)
    43        self.assertTrue(a_negative_float)
    44
    45        # self.assertTrue(bool(0.0))
    46        self.assertFalse(bool(0.0))
    47
    48
    49# NOTES
    

    the test passes.

  • I add a comment

    49# NOTES
    50# bool(-0.1) is True
    51# bool(1) is True
    52# bool(-1) is True
    53# True is NOT False
    54# True is NOT equal to False
    55# True is a boolean
    56# bool(0.0) is False
    57# bool(0) is False
    58# bool(None) is False
    59# False is NOT True
    60# False is NOT equal to True
    61# False is a boolean
    
  • I add an assertion without bool

    45        # self.assertTrue(bool(0.0))
    46        self.assertFalse(bool(0.0))
    47        self.assertTrue(0.0)
    48
    49
    50# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 0.0 is not true
    

    because the result of bool(0.0) is False.

  • I change assertTrue to assertFalse

    45        # self.assertTrue(bool(0.0))
    46        self.assertFalse(bool(0.0))
    47        # self.assertTrue(0.0)
    48        self.assertFalse(0.0)
    49
    50
    51# NOTES
    
  • I add an assertion to test if 0.1 is grouped as False or True

    45        # self.assertTrue(bool(0.0))
    46        self.assertFalse(bool(0.0))
    47        # self.assertTrue(0.0)
    48        self.assertFalse(0.0)
    49
    50        self.assertFalse(bool(0.1))
    51
    52
    53# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    
    • because the result of bool(0.1) is True

    • I use 0.1 for all the floats (binary floating point decimal numbers) that are bigger than 0.0

  • I change assertFalse to assertTrue

    45        # self.assertTrue(bool(0.0))
    46        self.assertFalse(bool(0.0))
    47        # self.assertTrue(0.0)
    48        self.assertFalse(0.0)
    49
    50        # self.assertFalse(bool(0.1))
    51        self.assertTrue(bool(0.1))
    52
    53
    54# NOTES
    

    the test passes.

  • I add a comment

    54# NOTES
    55# bool(0.1) is True
    56# bool(-0.1) is True
    57# True is NOT False
    58# True is NOT equal to False
    59# True is a boolean
    60# bool(0.0) is False
    61# bool(None) is False
    62# False is NOT True
    63# False is NOT equal to True
    64# False is a boolean
    
  • I add an assertion without bool

    50        # self.assertFalse(bool(0.1))
    51        self.assertTrue(bool(0.1))
    52        self.assertFalse(0.1)
    53
    54
    55# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 0.1 is not false
    

    because the result of bool(0.1) is True.

  • I change assertTrue to assertFalse

    50        # self.assertFalse(bool(0.1))
    51        self.assertTrue(bool(0.1))
    52        # self.assertFalse(0.1)
    53        self.assertTrue(0.1)
    54
    55
    56# NOTES
    
  • I add a variable for 0.1

    45        # self.assertTrue(bool(0.0))
    46        self.assertFalse(bool(0.0))
    47        # self.assertTrue(0.0)
    48        self.assertFalse(0.0)
    49
    50        a_positive_float = 0.1
    51        # self.assertFalse(bool(0.1))
    52        self.assertTrue(bool(0.1))
    53        # self.assertFalse(0.1)
    54        self.assertTrue(0.1)
    55
    56
    57# NOTES
    
  • I use the variable to remove repetition of 0.1

    50        a_positive_float = 0.1
    51        # self.assertFalse(bool(0.1))
    52        # self.assertTrue(bool(0.1))
    53        self.assertTrue(bool(a_positive_float))
    54        # self.assertFalse(0.1)
    55        # self.assertTrue(0.1)
    56        self.assertTrue(0.1)
    57
    58
    59# NOTES
    

    the test is still green.

  • I remove the commented lines

    36    def test_is_a_float_falsy_or_truthy(self):
    37        negative_float = -0.1
    38        self.assertTrue(bool(negative_float))
    39        self.assertTrue(negative_float)
    40
    41        self.assertFalse(bool(0.0))
    42        self.assertFalse(0.0)
    43
    44        a_positive_float = 0.1
    45        self.assertTrue(bool(a_positive_float))
    46        self.assertTrue(0.1)
    47
    48
    49# NOTES
    
  • I make the new comments simpler because floats and integers are numbers and 0.0 is the same as 0 even though they are different types

    49# NOTES
    50# bool(a positive number) is True
    51# bool(a negative number) is True
    52# True is NOT False
    53# True is NOT equal to False
    54# True is a boolean
    55# bool(zero) is False
    56# bool(None) is False
    57# False is NOT True
    58# False is NOT equal to True
    59# False is a boolean
    
  • I add a git commit message in the other 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.

  • ‘0’ is grouped as False, positive and negative integers are grouped as True

  • None is grouped as False


test_is_a_string_falsy_or_truthy


RED: make it fail


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

  • I add a test for if a string (anything in quotes) is grouped as False or True

    44        a_positive_float = 0.1
    45        self.assertTrue(bool(a_positive_float))
    46        self.assertTrue(0.1)
    47
    48    def test_is_a_string_falsy_or_truthy(self):
    49        self.assertTrue(bool(str()))
    50
    51
    52# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

    because the result of bool(str()) is False.


GREEN: make it pass


I change assertTrue to assertFalse

48    def test_is_a_string_falsy_or_truthy(self):
49        # self.assertTrue(bool(str()))
50        self.assertFalse(bool(str()))
51
52
53# NOTES

the test passes.


REFACTOR: make it better


  • I add a comment

    53# NOTES
    54# bool(a positive number) is True
    55# bool(a negative number) is True
    56# True is NOT False
    57# True is NOT equal to False
    58# True is a boolean
    59# bool(str()) is False
    60# bool(zero) is False
    61# bool(None) is False
    62# False is NOT True
    63# False is NOT equal to True
    64# False is a boolean
    
  • I add an assertion without bool

    48    def test_is_a_string_falsy_or_truthy(self):
    49        # self.assertTrue(bool(str()))
    50        self.assertFalse(bool(str()))
    51        self.assertTrue(str())
    52
    53
    54# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: '' is not true
    
    • because the result of bool(str()) which is bool('') is False

    • str() is another way to write '' or "" or '''''' or """""" (the empty string)

  • I change assertTrue to assertFalse

    48    def test_is_a_string_falsy_or_truthy(self):
    49        # self.assertTrue(bool(str()))
    50        self.assertFalse(bool(str()))
    51        # self.assertTrue(str())
    52        self.assertFalse(str())
    53
    54
    55# NOTES
    
  • I add an assertion to test if a string with things is grouped as False or True

    48    def test_is_a_string_falsy_or_truthy(self):
    49        # self.assertTrue(bool(str()))
    50        self.assertFalse(bool(str()))
    51        # self.assertTrue(str())
    52        self.assertFalse(str())
    53
    54        self.assertFalse(bool("a string with things"))
    55
    56
    57# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    because the result of bool("a string with things") is True.

  • I change assertFalse to assertTrue

    48    def test_is_a_string_falsy_or_truthy(self):
    49        # self.assertTrue(bool(str()))
    50        self.assertFalse(bool(str()))
    51        # self.assertTrue(str())
    52        self.assertFalse(str())
    53
    54        # self.assertFalse(bool("a string with things"))
    55        self.assertTrue(bool("a string with things"))
    56
    57
    58# NOTES
    

    the test passes.

  • I add a comment

    58# NOTES
    59# bool("a string with things") is True
    60# bool(positive number) is True
    61# bool(negative number) is True
    62# True is NOT False
    63# True is NOT equal to False
    64# True is a boolean
    65# bool(str()) is False
    66# bool(zero) is False
    67# bool(None) is False
    68# False is NOT True
    69# False is NOT equal to True
    70# False is a boolean
    
  • I add an assertion without bool

    54        # self.assertFalse(bool("a string with things"))
    55        self.assertTrue(bool("a string with things"))
    56        self.assertFalse("a string with things")
    57
    58
    59# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 'a string with things' is not false
    

    because the result of bool("a string with things") is True.

  • I change assertFalse to assertTrue

    54        # self.assertFalse(bool("a string with things"))
    55        self.assertTrue(bool("a string with things"))
    56        # self.assertFalse("a string with things")
    57        self.assertTrue("a string with things")
    58
    59
    60# NOTES
    
  • I add a variable for "a string with things"

    48    def test_is_a_string_falsy_or_truthy(self):
    49        # self.assertTrue(bool(str()))
    50        self.assertFalse(bool(str()))
    51        # self.assertTrue(str())
    52        self.assertFalse(str())
    53
    54        a_string = "a string with things"
    55        # self.assertFalse(bool("a string with things"))
    56        self.assertTrue(bool("a string with things"))
    57        # self.assertFalse("a string with things")
    58        self.assertTrue("a string with things")
    59
    60
    61# NOTES
    
  • I use the variable to remove repetition of "a string with things"

    54        a_string = "a string with things"
    55        # self.assertFalse(bool("a string with things"))
    56        # self.assertTrue(bool("a string with things"))
    57        self.assertTrue(bool(a_string))
    58        # self.assertFalse("a string with things")
    59        # self.assertTrue("a string with things")
    60        self.assertTrue(a_string)
    61
    62
    63# NOTES
    

    the test is still green.

  • I remove the commented lines

    48    def test_is_a_string_falsy_or_truthy(self):
    49        self.assertFalse(bool(str()))
    50        self.assertFalse(str())
    51
    52        a_string = "a string with things"
    53        self.assertTrue(bool(a_string))
    54        self.assertTrue(a_string)
    55
    56
    57# NOTES
    
  • I change the new comments to make them clearer

    57# NOTES
    58# bool(a string with things) is True
    59# bool(positive number) is True
    60# bool(negative number) is True
    61# True is NOT False
    62# True is NOT equal to False
    63# True is a boolean
    64# bool(the empty string) is False
    65# bool(zero) is False
    66# bool(None) is False
    67# False is NOT True
    68# False is NOT equal to True
    69# False is a boolean
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'add test_is_a_string_falsy_or_truthy'
    
  • the empty string is grouped as False, a string with things is grouped as True

  • zero is grouped as False, and positive and negative numbers are grouped as True

  • None is grouped as False


test_is_a_tuple_falsy_or_truthy


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

    52        a_string = "a string with things"
    53        self.assertTrue(bool(a_string))
    54        self.assertTrue(a_string)
    55
    56    def test_is_a_tuple_falsy_or_truthy(self):
    57        self.assertTrue(bool(tuple()))
    58
    59
    60# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

    because the result of bool(tuple()) is False.


GREEN: make it pass


I change assertTrue to assertFalse

56    def test_is_a_tuple_falsy_or_truthy(self):
57        # self.assertTrue(bool(tuple()))
58        self.assertFalse(bool(tuple()))
59
60
61# NOTES

the test passes.


REFACTOR: make it better


  • I add a comment

    61# NOTES
    62# bool(a string with things) is True
    63# bool(a positive number) is True
    64# bool(a negative number) is True
    65# True is NOT False
    66# True is NOT equal to False
    67# True is a boolean
    68# bool(the empty tuple) is False
    69# bool(the empty string) is False
    70# bool(zero) is False
    71# bool(None) is False
    72# False is NOT True
    73# False is NOT equal to True
    74# False is a boolean
    
  • I add an assertion without bool

    56    def test_is_a_tuple_falsy_or_truthy(self):
    57        # self.assertTrue(bool(tuple()))
    58        self.assertFalse(bool(tuple()))
    59        self.assertTrue(tuple())
    60
    61
    62# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: () is not true
    
    • because the result of bool(tuple()) which is bool(()) is False

    • tuple() is another way to write () (the empty tuple)

  • I change assertTrue to assertFalse

    56    def test_is_a_tuple_falsy_or_truthy(self):
    57        # self.assertTrue(bool(tuple()))
    58        self.assertFalse(bool(tuple()))
    59        # self.assertTrue(tuple())
    60        self.assertFalse(tuple())
    61
    62
    63# NOTES
    
  • I add an assertion to test if a tuple with things is grouped as False or True

    56    def test_is_a_tuple_falsy_or_truthy(self):
    57        # self.assertTrue(bool(tuple()))
    58        self.assertFalse(bool(tuple()))
    59        # self.assertTrue(tuple())
    60        self.assertFalse(tuple())
    61
    62        self.assertFalse(bool((0, 1, 2, 'n')))
    63
    64
    65# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    because the result of bool((0, 1, 2, 'n')) is True.

  • I change assertFalse to assertTrue

    62        # self.assertFalse(bool((0, 1, 2, 'n')))
    63        self.assertTrue(bool((0, 1, 2, 'n')))
    64
    65
    66# NOTES
    

    the test passes.

  • I add a comment

    66# NOTES
    67# bool(a tuple with things) is True
    68# bool(a string with things) is True
    69# bool(a positive number) is True
    70# bool(a negative number) is True
    71# True is NOT False
    72# True is NOT equal to False
    73# True is a boolean
    74# bool(the empty tuple) is False
    75# bool(the empty string) is False
    76# bool(zero) is False
    77# bool(None) is False
    78# False is NOT True
    79# False is NOT equal to True
    80# False is a boolean
    
  • I add an assertion without bool

    62        # self.assertFalse(bool((0, 1, 2, 'n')))
    63        self.assertTrue(bool((0, 1, 2, 'n')))
    64        self.assertFalse((0, 1, 2, 'n'))
    65
    66
    67# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (0, 1, 2, 'n') is not false
    

    because the result of bool((0, 1, 2, 'n')) is True.

  • I change assertFalse to assertTrue

    62        # self.assertFalse(bool((0, 1, 2, 'n')))
    63        self.assertTrue(bool((0, 1, 2, 'n')))
    64        # self.assertFalse((0, 1, 2, 'n'))
    65        self.assertTrue((0, 1, 2, 'n'))
    66
    67
    68# NOTES
    
  • I add a variable for (0, 1, 2, 'n')

    56    def test_is_a_tuple_falsy_or_truthy(self):
    57        # self.assertTrue(bool(tuple()))
    58        self.assertFalse(bool(tuple()))
    59        # self.assertTrue(tuple())
    60        self.assertFalse(tuple())
    61
    62        a_tuple = (0, 1, 2, 'n')
    63        # self.assertFalse(bool((0, 1, 2, 'n')))
    64        self.assertTrue(bool((0, 1, 2, 'n')))
    65        # self.assertFalse((0, 1, 2, 'n'))
    66        self.assertTrue((0, 1, 2, 'n'))
    67
    68
    69# NOTES
    
  • I use the variable to remove repetition of (0, 1, 2, 'n')

    62        a_tuple = (0, 1, 2, 'n')
    63        # self.assertFalse(bool((0, 1, 2, 'n')))
    64        # self.assertTrue(bool((0, 1, 2, 'n')))
    65        self.assertTrue(bool(a_tuple))
    66        # self.assertFalse((0, 1, 2, 'n'))
    67        # self.assertTrue((0, 1, 2, 'n'))
    68        self.assertTrue(a_tuple)
    69
    70
    71# NOTES
    

    the test is still green.

  • I remove the commented lines

    56    def test_is_a_tuple_falsy_or_truthy(self):
    57        self.assertFalse(bool(tuple()))
    58        self.assertFalse(tuple())
    59
    60        a_tuple = (0, 1, 2, 'n')
    61        self.assertTrue(bool(a_tuple))
    62        self.assertTrue(a_tuple)
    63
    64
    65# NOTES
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'add test_is_a_tuple_falsy_or_truthy'
    
  • the empty tuple is grouped as False, a tuple with things is grouped as True

  • the empty string is grouped as False, a string with things is grouped as True

  • zero is grouped as False, and positive and negative numbers are grouped as True

  • None is grouped as False


test_is_a_list_falsy_or_truthy


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

    60        a_tuple = (0, 1, 2, 'n')
    61        self.assertTrue(bool(a_tuple))
    62        self.assertTrue(a_tuple)
    63
    64    def test_is_a_list_falsy_or_truthy(self):
    65        self.assertTrue(bool(list()))
    66
    67
    68# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

    because the result of bool(list()) is False.


GREEN: make it pass


I change assertTrue to assertFalse

64    def test_is_a_list_falsy_or_truthy(self):
65        # self.assertTrue(bool(list()))
66        self.assertFalse(bool(list()))
67
68
69# NOTES

the test passes.


REFACTOR: make it better


  • I add a comment

    69# NOTES
    70# bool(a tuple with things) is True
    71# bool(a string with things) is True
    72# bool(a positive number) is True
    73# bool(a negative number) is True
    74# True is NOT False
    75# True is NOT equal to False
    76# True is a boolean
    77# bool(the empty list) is False
    78# bool(the empty tuple) is False
    79# bool(the empty string) is False
    80# bool(zero) is False
    81# bool(None) is False
    82# False is NOT True
    83# False is NOT equal to True
    84# False is a boolean
    
  • I add an assertion without bool

    64    def test_is_a_list_falsy_or_truthy(self):
    65        # self.assertTrue(bool(list()))
    66        self.assertFalse(bool(list()))
    67        self.assertTrue(list())
    68
    69
    70# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: [] is not true
    
    • because the result of bool(list()) which is bool([]) is False

    • list() is another way to write [] (the empty list)

  • I change assertTrue to assertFalse

    64    def test_is_a_list_falsy_or_truthy(self):
    65        # self.assertTrue(bool(list()))
    66        self.assertFalse(bool(list()))
    67        # self.assertTrue(list())
    68        self.assertFalse(list())
    69
    70
    71# NOTES
    
  • I add an assertion to test if a list (anything in square brackets ‘[ ]’) with things is grouped as False or True

    64    def test_is_a_list_falsy_or_truthy(self):
    65        # self.assertTrue(bool(list()))
    66        self.assertFalse(bool(list()))
    67        # self.assertTrue(list())
    68        self.assertFalse(list())
    69
    70        self.assertFalse(bool([0, 1, 2, 'n']))
    71
    72
    73# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    because the result of bool([0, 1, 2, 'n']) is True.

  • I change assertFalse to assertTrue

    70        # self.assertFalse(bool([0, 1, 2, 'n']))
    71        self.assertTrue(bool([0, 1, 2, 'n']))
    72
    73
    74# NOTES
    

    the test passes.

  • I add a comment

    74# NOTES
    75# bool(a list with things) is True
    76# bool(a tuple with things) is True
    77# bool(a string with things) is True
    78# bool(a positive number) is True
    79# bool(a negative number) is True
    80# True is NOT False
    81# True is NOT equal to False
    82# True is a boolean
    83# bool(the empty list) is False
    84# bool(the empty tuple) is False
    85# bool(the empty string) is False
    86# bool(zero) is False
    87# bool(None) is False
    88# False is NOT True
    89# False is NOT equal to True
    90# False is a boolean
    
  • I add an assertion without bool

    70        # self.assertFalse(bool([0, 1, 2, 'n']))
    71        self.assertTrue(bool([0, 1, 2, 'n']))
    72        self.assertFalse([0, 1, 2, 'n'])
    73
    74
    75# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: [0, 1, 2, 'n'] is not false
    

    because the result of bool([0, 1, 2, 'n']) is True.

  • I change assertFalse to assertTrue

    70        # self.assertFalse(bool([0, 1, 2, 'n']))
    71        self.assertTrue(bool([0, 1, 2, 'n']))
    72        # self.assertFalse([0, 1, 2, 'n'])
    73        self.assertTrue([0, 1, 2, 'n'])
    74
    75
    76# NOTES
    
  • I add a variable for [0, 1, 2, 'n']

    64    def test_is_a_list_falsy_or_truthy(self):
    65        # self.assertTrue(bool(list()))
    66        self.assertFalse(bool(list()))
    67        # self.assertTrue(list())
    68        self.assertFalse(list())
    69
    70        a_list = [0, 1, 2, 'n']
    71        # self.assertFalse(bool([0, 1, 2, 'n']))
    72        self.assertTrue(bool([0, 1, 2, 'n']))
    73        # self.assertFalse([0, 1, 2, 'n'])
    74        self.assertTrue([0, 1, 2, 'n'])
    75
    76
    77# NOTES
    
  • I use the variable to remove repetition of [0, 1, 2, 'n']

    70        a_list = [0, 1, 2, 'n']
    71        # self.assertFalse(bool([0, 1, 2, 'n']))
    72        # self.assertTrue(bool([0, 1, 2, 'n']))
    73        self.assertTrue(bool(a_list))
    74        # self.assertFalse([0, 1, 2, 'n'])
    75        # self.assertTrue([0, 1, 2, 'n'])
    76        self.assertTrue(a_list)
    77
    78
    79# NOTES
    

    the test is still green.

  • I remove the commented lines

    64    def test_is_a_list_falsy_or_truthy(self):
    65        self.assertFalse(bool(list()))
    66        self.assertFalse(list())
    67
    68        a_list = [0, 1, 2, 'n']
    69        self.assertTrue(bool(a_list))
    70        self.assertTrue(a_list)
    71
    72
    73# NOTES
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'add test_is_a_list_falsy_or_truthy'
    
  • the empty list is grouped as False, a list with things is grouped as True

  • the empty tuple is grouped as False, a tuple with things is grouped as True

  • the empty string is grouped as False, a string with things is grouped as True

  • zero is grouped as False, and positive and negative numbers are grouped as True

  • None is grouped as False


test_is_a_set_falsy_or_truthy


RED: make it fail


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

  • I add a test for if a set (anything in curly braces { }, not key-value pairs) is grouped as False or True

    68        a_list = [0, 1, 2, 'n']
    69        self.assertTrue(bool(a_list))
    70        self.assertTrue(a_list)
    71
    72    def test_is_a_set_falsy_or_truthy(self):
    73        self.assertTrue(bool(set()))
    74
    75
    76# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

    because the result of bool(set()) is False.


GREEN: make it pass


I change assertTrue to assertFalse

72    def test_is_a_set_falsy_or_truthy(self):
73        # self.assertTrue(bool(set()))
74        self.assertFalse(bool(set()))
75
76
77# NOTES

the test passes.


REFACTOR: make it better


  • I add a comment

    77# NOTES
    78# bool(a list with things) is True
    79# bool(a tuple with things) is True
    80# bool(a string with things) is True
    81# bool(a positive number) is True
    82# bool(a negative number) is True
    83# True is NOT False
    84# True is NOT equal to False
    85# True is a boolean
    86# bool(the empty set) is False
    87# bool(the empty list) is False
    88# bool(the empty tuple) is False
    89# bool(the empty string) is False
    90# bool(zero) is False
    91# bool(None) is False
    92# False is NOT True
    93# False is NOT equal to True
    94# False is a boolean
    
  • I add an assertion without bool

    72    def test_is_a_set_falsy_or_truthy(self):
    73        # self.assertTrue(bool(set()))
    74        self.assertFalse(bool(set()))
    75        self.assertTrue(set())
    76
    77
    78# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: set() is not true
    

    because the result of bool(set()) is False.

  • I change assertTrue to assertFalse

    72    def test_is_a_set_falsy_or_truthy(self):
    73        # self.assertTrue(bool(set()))
    74        self.assertFalse(bool(set()))
    75        # self.assertTrue(set())
    76        self.assertFalse(set())
    77
    78
    79# NOTES
    
  • I add an assertion to test if a set (anything in curly braces { }, not key-value pairs) with things is grouped as False or True

    72    def test_is_a_set_falsy_or_truthy(self):
    73        # self.assertTrue(bool(set()))
    74        self.assertFalse(bool(set()))
    75        # self.assertTrue(set())
    76        self.assertFalse(set())
    77
    78        self.assertFalse(bool({0, 1, 2, 'n'}))
    79
    80
    81# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    because the result of bool({0, 1, 2, 'n'}) is True.

  • I change assertFalse to assertTrue

    78        # self.assertFalse(bool({0, 1, 2, 'n'}))
    79        self.assertTrue(bool({0, 1, 2, 'n'}))
    80
    81
    82# NOTES
    

    the test passes.

  • I add a comment

     82# NOTES
     83# bool(a set with things) is True
     84# bool(a list with things) is True
     85# bool(a tuple with things) is True
     86# bool(a string with things) is True
     87# bool(a positive number) is True
     88# bool(a negative number) is True
     89# True is NOT False
     90# True is NOT equal to False
     91# True is a boolean
     92# bool(the empty set) is False
     93# bool(the empty list) is False
     94# bool(the empty tuple) is False
     95# bool(the empty string) is False
     96# bool(zero) is False
     97# bool(None) is False
     98# False is NOT True
     99# False is NOT equal to True
    100# False is a boolean
    
  • I add an assertion without bool

    78        # self.assertFalse(bool({0, 1, 2, 'n'}))
    79        self.assertTrue(bool({0, 1, 2, 'n'}))
    80        self.assertFalse({0, 1, 2, 'n'})
    81
    82
    83# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: {0, 1, 2, 'n'} is not false
    

    because the result of bool({0, 1, 2, 'n'}) is True.

  • I change assertFalse to assertTrue

    78        # self.assertFalse(bool({0, 1, 2, 'n'}))
    79        self.assertTrue(bool({0, 1, 2, 'n'}))
    80        # self.assertFalse({0, 1, 2, 'n'})
    81        self.assertTrue({0, 1, 2, 'n'})
    82
    83
    84# NOTES
    
  • I add a variable for {0, 1, 2, 'n'}

    72    def test_is_a_set_falsy_or_truthy(self):
    73        # self.assertTrue(bool(set()))
    74        self.assertFalse(bool(set()))
    75        # self.assertTrue(set())
    76        self.assertFalse(set())
    77
    78        a_set = {0, 1, 2, 'n'}
    79        # self.assertFalse(bool({0, 1, 2, 'n'}))
    80        self.assertTrue(bool({0, 1, 2, 'n'}))
    81        # self.assertFalse({0, 1, 2, 'n'})
    82        self.assertTrue({0, 1, 2, 'n'})
    83
    84
    85# NOTES
    
  • I use the variable to remove repetition of {0, 1, 2, 'n'}

    78        a_set = {0, 1, 2, 'n'}
    79        # self.assertFalse(bool({0, 1, 2, 'n'}))
    80        # self.assertTrue(bool({0, 1, 2, 'n'}))
    81        self.assertTrue(bool(a_set))
    82        # self.assertFalse({0, 1, 2, 'n'})
    83        # self.assertTrue({0, 1, 2, 'n'})
    84        self.assertTrue(a_set)
    85
    86
    87# NOTES
    

    the test is still green.

  • I remove the commented lines

    72    def test_is_a_set_falsy_or_truthy(self):
    73        self.assertFalse(bool(set()))
    74        self.assertFalse(set())
    75
    76        a_set = {0, 1, 2, 'n'}
    77        self.assertTrue(bool(a_set))
    78        self.assertTrue(a_set)
    79
    80
    81# NOTES
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'add test_is_a_set_falsy_or_truthy'
    
  • the empty set is grouped as False, a set with things is grouped as True

  • the empty list is grouped as False, a list with things is grouped as True

  • the empty tuple is grouped as False, a tuple with things is grouped as True

  • the empty string is grouped as False, a string with things is grouped as True

  • zero is grouped as False, and positive and negative numbers are grouped as True

  • None is grouped as False


test_is_a_dictionary_falsy_or_truthy


RED: make it fail



GREEN: make it pass


I change assertTrue to assertFalse

80    def test_is_a_dictionary_falsy_or_truthy(self):
81        # self.assertTrue(bool(dict()))
82        self.assertFalse(bool(dict()))
83
84
85# NOTES

the test passes.


REFACTOR: make it better


  • I add a comment

     85# NOTES
     86# bool(a set with things) is True
     87# bool(a list with things) is True
     88# bool(a tuple with things) is True
     89# bool(a string with things) is True
     90# bool(a positive number) is True
     91# bool(a negative number) is True
     92# True is NOT False
     93# True is NOT equal to False
     94# True is a boolean
     95# bool(the empty dictionary) is False
     96# bool(the empty set) is False
     97# bool(the empty list) is False
     98# bool(the empty tuple) is False
     99# bool(the empty string) is False
    100# bool(zero) is False
    101# bool(None) is False
    102# False is NOT True
    103# False is NOT equal to True
    104# False is a boolean
    
  • I add an assertion without bool

    80    def test_is_a_dictionary_falsy_or_truthy(self):
    81        # self.assertTrue(bool(dict()))
    82        self.assertFalse(bool(dict()))
    83        self.assertTrue(dict())
    84
    85
    86# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: {} is not true
    
    • because the result of bool(dict()) is False

    • dict() is another way to write {} (the empty dictionary)

  • I change assertTrue to assertFalse

    80    def test_is_a_dictionary_falsy_or_truthy(self):
    81        # self.assertTrue(bool(dict()))
    82        self.assertFalse(bool(dict()))
    83        # self.assertTrue(dict())
    84        self.assertFalse(dict())
    85
    86
    87# NOTES
    
  • I add an assertion to test if a dictionary with things is grouped as False or True

    80    def test_is_a_dictionary_falsy_or_truthy(self):
    81        # self.assertTrue(bool(dict()))
    82        self.assertFalse(bool(dict()))
    83        # self.assertTrue(dict())
    84        self.assertFalse(dict())
    85
    86        self.assertFalse(bool({'key': 'value'}))
    87
    88
    89# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    because the result of bool({'key': 'value'}) is True.

  • I change assertFalse to assertTrue

    86        # self.assertFalse(bool({'key': 'value'}))
    87        self.assertTrue(bool({'key': 'value'}))
    88
    89
    90# NOTES
    

    the test passes.

  • I add a comment

     90# NOTES
     91# bool(a dictionary with things) is True
     92# bool(a set with things) is True
     93# bool(a list with things) is True
     94# bool(a tuple with things) is True
     95# bool(a string with things) is True
     96# bool(a positive number) is True
     97# bool(a negative number) is True
     98# True is NOT False
     99# True is NOT equal to False
    100# True is a boolean
    101# bool(the empty dictionary) is False
    102# bool(the empty set) is False
    103# bool(the empty list) is False
    104# bool(the empty tuple) is False
    105# bool(the empty string) is False
    106# bool(zero) is False
    107# bool(None) is False
    108# False is NOT True
    109# False is NOT equal to True
    110# False is a boolean
    111
    112
    113# Exceptions seen
    114# AssertionError
    
  • I add an assertion without bool

    86        # self.assertFalse(bool({'key': 'value'}))
    87        self.assertTrue(bool({'key': 'value'}))
    88        self.assertFalse({'key': 'value'})
    89
    90
    91# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: {'key': 'value'} is not false
    

    because the result of bool({'key': 'value'}) is True.

  • I change assertFalse to assertTrue

    86        # self.assertFalse(bool({'key': 'value'}))
    87        self.assertTrue(bool({'key': 'value'}))
    88        # self.assertFalse({'key': 'value'})
    89        self.assertTrue({'key': 'value'})
    90
    91
    92# NOTES
    
  • I add a variable for {'key': 'value'}

    80    def test_is_a_dictionary_falsy_or_truthy(self):
    81        # self.assertTrue(bool(dict()))
    82        self.assertFalse(bool(dict()))
    83        # self.assertTrue(dict())
    84        self.assertFalse(dict())
    85
    86        a_dictionary = {'key': 'value'}
    87        # self.assertFalse(bool({'key': 'value'}))
    88        self.assertTrue(bool({'key': 'value'}))
    89        # self.assertFalse({'key': 'value'})
    90        self.assertTrue({'key': 'value'})
    91
    92
    93# NOTES
    
  • I use the variable to remove repetition of {'key': 'value'}

    86        a_dictionary = {'key': 'value'}
    87        # self.assertFalse(bool({'key': 'value'}))
    88        # self.assertTrue(bool({'key': 'value'}))
    89        self.assertTrue(bool(a_dictionary))
    90        # self.assertFalse({'key': 'value'})
    91        # self.assertTrue({'key': 'value'})
    92        self.assertTrue(a_dictionary)
    93
    94
    95# NOTES
    

    the test is still green.

  • I remove the commented lines

    80    def test_is_a_dictionary_falsy_or_truthy(self):
    81        self.assertFalse(bool(dict()))
    82        self.assertFalse(dict())
    83
    84        a_dictionary = {'key': 'value'}
    85        self.assertTrue(bool(a_dictionary))
    86        self.assertTrue(a_dictionary)
    87
    88
    89# NOTES
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'add test_is_a_dictionary_falsy_or_truthy'
    
  • the empty dictionary is grouped as False and a set with things is grouped as True

  • the empty set is grouped as False and a set with things is grouped as True

  • the empty list is grouped as False and a list with things is grouped as True

  • the empty tuple is grouped as False and a tuple with things is grouped as True

  • the empty string is grouped as False and a string with things is grouped as True

  • zero is grouped as False, and positive and negative numbers are grouped as True

  • None is grouped as False


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

    cd ..
    

    the terminal shows

    .../pumping_python
    

    I am back in the pumping_python directory.


review

I know from the tests, that True and False are different and are both booleans. In Python the following objects are grouped as

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

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?

you now know

Would you like to test the truth table? It will help you understand writing programs that make decisions based on conditions.


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.