booleans 1


There are only 2 booleans - True and False. I can use the assertFalse and assertTrue methods to test if any of the Python basic data structures seen so far - None, integers, floats, strings, tuples, lists, sets and dictionaries, are False or True.

preview

These are the tests I have 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.assertFalse(False)
 9        self.assertFalse(None)
10        self.assertFalse(0)
11        self.assertFalse(0.0)
12        self.assertFalse(str())
13        self.assertFalse(tuple())
14        self.assertFalse(list())
15        self.assertFalse(set())
16        self.assertFalse(dict())
17
18    def test_what_is_true(self):
19        self.assertIsInstance(True, bool)
20        self.assertTrue(True)
21        self.assertTrue(-1)
22        self.assertTrue(1)
23        self.assertTrue(-0.1)
24        self.assertTrue(0.1)
25        self.assertTrue("text")
26        self.assertTrue((1, 2, 3, 'n'))
27        self.assertTrue([1, 2, 3, 'n'])
28        self.assertTrue({1, 2, 3, 'n'})
29        self.assertTrue({'key': 'value'})
30
31
32# NOTES
33# a dictionary with things is True
34# a set with things is True
35# a list with things is True
36# a tuple with things is True
37# a string with things is True
38# positive and negative numbers are True
39# True is True
40# True is not false
41# True is a boolean
42# the empty dictionary is False
43# the empty set is False
44# the empty list is False
45# the empty tuple is False
46# the empty string is False
47# 0 is False
48# None is False
49# False is False
50# False is not true
51# False is a boolean
52
53
54# Exceptions seen
55# AssertionError

questions about Booleans

Here are questions you can answer after going through this chapter


start the project

  • I name this project booleans

  • I open a terminal

  • I make a directory for the project

    mkdir booleans
    
  • 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
    

    Note

    on Windows without Windows Subsystem for Linux use New-Item tests/__init__.py instead of touch tests/__init__.py

    New-Item tests/__init__.py
    
  • I make a Python file for the tests in the tests directory

    touch tests/test_booleans.py
    

    Note

    on Windows without Windows Subsystem for Linux use New-Item tests/test_booleans.py instead of touch tests/test_booleans.py

    New-Item tests/test_booleans.py
    
  • I open test_booleans.py in the editor of the Integrated Development Environment (IDE)

    Tip

    I can use the terminal to open a file in the Integrated Development Environment (IDE) by typing the name of the program and the name of the file. That means when I type this in the terminal

    code tests/test_booleans.py
    

    Visual Studio Code opens test_booleans.py in the editor

  • 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 make a requirements file for the Python packages I need, in the terminal

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

    echo "pytest-watcher" >> requirements.txt
    
  • I setup the project with uv

    uv init
    

    the terminal shows

    Initialized project `booleans`
    
  • I remove main.py from the project because I do not use it

    rm main.py
    
  • I install the Python packages I gave in the requirements file

    uv add --requirement requirements.txt
    
  • I use pytest-watcher to run the tests automatically

    uv run pytest-watcher . --now
    

    the terminal shows

    ================================ 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 ============================
    
  • I add AssertionError to the list of Exceptions seen in test_booleans.py in the editor

     4class TestBooleans(unittest.TestCase):
     5
     6    def test_failure(self):
     7        self.assertFalse(True)
     8
     9
    10# Exceptions seen
    11# AssertionError
    
  • then 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 testing None to check if False is a child (instance) of the bool class. I think this will fail

     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 shows AssertionError

    AssertionError: False is an instance of <class 'bool'>
    
  • I add comments

     4class TestBooleans(unittest.TestCase):
     5
     6    def test_what_is_false(self):
     7        self.assertIsInstance(False, bool)
     8
     9
    10# NOTES
    11# False is a boolean
    12
    13
    14# Exceptions seen
    15# AssertionError
    

    so far this is something I already know from testing None


GREEN: make it pass


I change assertNotIsInstance to assertIsInstance

7        self.assertIsInstance(False, bool)

the test passes

False is a boolean


test_what_is_true

I do the same thing with True


RED: make it fail


  • I add another failing test

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

    the terminal shows AssertionError

    AssertionError: True is an instance of <class 'bool'>
    
  • I add a comment

    13# NOTES
    14# True is a boolean
    15# False is a boolean
    16
    17
    18# Exceptions seen
    19# AssertionError
    

GREEN: make it pass


I change the assert method

 9    def test_what_is_true(self):
10        self.assertIsInstance(True, bool)
11
12
13# NOTES

the test passes

True is a boolean


REFACTOR: make it better

  • I add a line that will fail to the test_what_is_true method

     9    def test_what_is_true(self):
    10        self.assertIsInstance(True, bool)
    11        self.assertTrue(False)
    12
    13
    14# NOTES
    

    the terminal shows AssertionError

    AssertionError: False is not true
    
  • I add a comment

    14# NOTES
    15# True is a boolean
    16# False is not true
    17# False is a boolean
    18
    19
    20# Exceptions seen
    21# AssertionError
    
  • I change assertTrue to assertFalse

     9    def test_what_is_true(self):
    10        self.assertIsInstance(True, bool)
    11        self.assertFalse(False)
    12
    13
    14# NOTES
    

    the test passes

  • I add a comment

    14# NOTES
    15# True is a boolean
    16# False is False
    17# False is not true
    18# False is a boolean
    19
    20
    21# Exceptions seen
    
  • I move the assertion from test_what_is_true to the test_what_is_false method

    Tip

    In Visual Studio Code I can use alt (Windows/Linux) or option (MacOS) with the up/down arrows on the keyboard to move a line up or down

     4class TestBooleans(unittest.TestCase):
     5
     6    def test_what_is_false(self):
     7        self.assertIsInstance(False, bool)
     8        self.assertFalse(False)
     9
    10    def test_what_is_true(self):
    11        self.assertIsInstance(True, bool)
    12
    13
    14# NOTES
    
  • I add a line that will fail to the test_what_is_false method

     6    def test_what_is_false(self):
     7        self.assertIsInstance(False, bool)
     8        self.assertFalse(False)
     9        self.assertFalse(True)
    10
    11    def test_what_is_true(self):
    

    the terminal shows AssertionError

    AssertionError: True is not false
    
  • I add a comment

    15# NOTES
    16# True is not false
    17# True is a boolean
    18# False is False
    19# False is not true
    20# False is a boolean
    21
    22
    23# Exceptions seen
    
  • I change assertFalse to assertTrue

     6    def test_what_is_false(self):
     7        self.assertIsInstance(False, bool)
     8        self.assertFalse(False)
     9        self.assertTrue(True)
    10
    11    def test_what_is_true(self):
    12        self.assertIsInstance(True, bool)
    13
    14
    15# NOTES
    

    the test passes

  • I add a comment

    15# NOTES
    16# True is True
    17# True is not false
    18# True is a boolean
    19# False is False
    20# False is not true
    21# False is a boolean
    22
    23
    24# Exceptions seen
    25# AssertionError
    
  • I move the assertion from test_what_is_false to the test_what_is_true method

     6    def test_what_is_false(self):
     7        self.assertIsInstance(False, bool)
     8        self.assertFalse(False)
     9
    10    def test_what_is_true(self):
    11        self.assertIsInstance(True, bool)
    12        self.assertTrue(True)
    13
    14
    15# NOTES
    

All of these are still statements from the AssertionError and None chapters.

Next up, I test the other Python basic data structures to see which ones are False or True. What do you think?


is None False or True?


RED: make it fail


I add an assertion to test_what_is_true to test if None is True

10    def test_what_is_true(self):
11        self.assertIsInstance(True, bool)
12        self.assertTrue(True)
13        self.assertTrue(None)
14
15
16# NOTES

the terminal shows AssertionError

AssertionError: None is not true

GREEN: make it pass


I change the method

13        self.assertFalse(None)

the test passes


REFACTOR: make it better


  • I add a comment

    16# NOTES
    17# True is True
    18# True is not false
    19# True is a boolean
    20# None is False
    21# False is False
    22# False is not true
    23# False is a boolean
    24
    25
    26# Exceptions seen
    
  • I move the assertion from test_what_is_true to test_what_is_false

     6    def test_what_is_false(self):
     7        self.assertIsInstance(False, bool)
     8        self.assertFalse(False)
     9        self.assertFalse(None)
    10
    11    def test_what_is_true(self):
    12        self.assertIsInstance(True, bool)
    13        self.assertTrue(True)
    14
    15
    16# NOTES
    

None is False and I learned from the AssertionError chapter that False is not None


is an integer False or True?


RED: make it fail


I add a line that will fail to the test_what_is_false method to see if an integer (a whole number) is False

 6    def test_what_is_false(self):
 7        self.assertIsInstance(False, bool)
 8        self.assertFalse(False)
 9        self.assertFalse(None)
10        self.assertFalse(-1)
11
12    def test_what_is_true(self):

the terminal shows AssertionError

AssertionError: -1 is not false

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


GREEN: make it pass


I change the method

10        self.assertTrue(-1)

the test passes


REFACTOR: make it better


  • I add a comment

    19# NOTES
    20# negative integers are True
    21# True is True
    22# True is not false
    23# True is a boolean
    24# None is False
    25# False is False
    26# False is not true
    27# False is a boolean
    
  • I move the assertion from test_what_is_false to test_what_is_true

     6    def test_what_is_false(self):
     7        self.assertIsInstance(False, bool)
     8        self.assertFalse(False)
     9        self.assertFalse(None)
    10
    11    def test_what_is_true(self):
    12        self.assertIsInstance(True, bool)
    13        self.assertTrue(True)
    14        self.assertTrue(-1)
    15
    16
    17# NOTES
    

    negative integers are True in Python

  • I add a new failing line to test_what_is_true to see if 0 is True

    11    def test_what_is_true(self):
    12        self.assertIsInstance(True, bool)
    13        self.assertTrue(True)
    14        self.assertTrue(-1)
    15        self.assertTrue(0)
    16
    17
    18# NOTES
    

    the terminal shows AssertionError

    AssertionError: 0 is not true
    
  • I change the method to assertFalse

    15        self.assertFalse(0)
    

    the test passes

  • I add a comment

    18# NOTES
    19# negative integers are True
    20# True is True
    21# True is not false
    22# True is a boolean
    23# 0 is False
    24# None is False
    25# False is False
    26# False is not true
    27# False is a boolean
    
  • I move the assertion to test_what_is_false

     6    def test_what_is_false(self):
     7        self.assertIsInstance(False, bool)
     8        self.assertFalse(False)
     9        self.assertFalse(None)
    10        self.assertFalse(0)
    11
    12    def test_what_is_true(self):
    

    0 is False in Python

  • I add another failing line to test_what_is_false to see if 1 is False

     6    def test_what_is_false(self):
     7        self.assertIsInstance(False, bool)
     8        self.assertFalse(False)
     9        self.assertFalse(None)
    10        self.assertFalse(0)
    11        self.assertFalse(1)
    12
    13    def test_what_is_true(self):
    

    the terminal shows AssertionError

    AssertionError: 1 is not false
    

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

  • I change the method

    11        self.assertTrue(1)
    

    the test passes

  • I add to the comment about negative integers

    19# NOTES
    20# positive and negative integers are True
    21# True is True
    22# True is not false
    23# True is a boolean
    24# 0 is False
    25# None is False
    26# False is False
    27# False is not true
    28# False is a boolean
    
  • I move the assertion to test_what_is_true

     6    def test_what_is_false(self):
     7        self.assertIsInstance(False, bool)
     8        self.assertFalse(False)
     9        self.assertFalse(None)
    10        self.assertFalse(0)
    11
    12    def test_what_is_true(self):
    13        self.assertIsInstance(True, bool)
    14        self.assertTrue(True)
    15        self.assertTrue(-1)
    16        self.assertTrue(1)
    17
    18
    19# NOTES
    

in Python 0 is False, and positive and negative integers are True


is a float False or True?


RED: make it fail


I add an assertion to test if floats (binary floating point decimal numbers) are False in test_what_is_false

 6    def test_what_is_false(self):
 7        self.assertIsInstance(False, bool)
 8        self.assertFalse(False)
 9        self.assertFalse(None)
10        self.assertFalse(0)
11        self.assertFalse(-0.1)
12
13    def test_what_is_true(self):

the terminal shows AssertionError

AssertionError: -0.1 is not false

I use -0.1 for all the binary floating point numbers that are smaller than 0.0.


GREEN: make it pass


  • I change the method

    11        self.assertTrue(-0.1)
    

    the test passes

  • I add comments

    20# NOTES
    21# negative floats are True
    22# positive and negative integers are True
    23# True is True
    24# True is not false
    25# True is a boolean
    26# 0 is False
    27# None is False
    28# False is False
    29# False is not true
    30# False is a boolean
    
  • I move the assertion to test_what_is_true

     9        self.assertFalse(None)
    10        self.assertFalse(0)
    11
    12    def test_what_is_true(self):
    13        self.assertIsInstance(True, bool)
    14        self.assertTrue(True)
    15        self.assertTrue(-1)
    16        self.assertTrue(1)
    17        self.assertTrue(-0.1)
    18
    19
    20# NOTES
    

Negative floats are True in Python


REFACTOR: make it better


  • I add another failing line to test_what_is_true to see if 0.0 is True

    12    def test_what_is_true(self):
    13        self.assertIsInstance(True, bool)
    14        self.assertTrue(True)
    15        self.assertTrue(-1)
    16        self.assertTrue(1)
    17        self.assertTrue(-0.1)
    18        self.assertTrue(0.0)
    19
    20
    21# NOTES
    

    the terminal shows AssertionError

    AssertionError: 0.0 is not true
    
  • I change the method

    18        self.assertFalse(0.0)
    

    the test passes

  • I add a comment

    21# NOTES
    22# negative floats are True
    23# positive and negative integers are True
    24# True is True
    25# True is not false
    26# True is a boolean
    27# 0.0 is False
    28# 0 is False
    29# None is False
    30# False is False
    31# False is not true
    32# False is a boolean
    
  • I move the assertion to test_what_is_false

     6    def test_what_is_false(self):
     7        self.assertIsInstance(False, bool)
     8        self.assertFalse(False)
     9        self.assertFalse(None)
    10        self.assertFalse(0)
    11        self.assertFalse(0.0)
    12
    13    def test_what_is_true(self):
    

    0.0 is False in Python

  • I add another assertion to test_what_is_false to see if 0.1 is False

    10        self.assertFalse(0)
    11        self.assertFalse(0.0)
    12        self.assertFalse(0.1)
    13
    14    def test_what_is_true(self):
    

    the terminal shows AssertionError

    AssertionError: 0.1 is not false
    

    I use 0.1 for all the binary floating point numbers that are bigger than 0.0.

  • I change the method

    12        self.assertTrue(0.1)
    

    the test passes

  • I add to the comment about negative floats

    22# NOTES
    23# positive and negative floats are True
    24# positive and negative integers are True
    25# True is True
    26# True is not false
    27# True is a boolean
    28# 0.0 is False
    29# 0 is False
    30# None is False
    31# False is False
    32# False is not true
    33# False is a boolean
    
  • I move the assertion to test_what_is_true

    10        self.assertFalse(0)
    11        self.assertFalse(0.0)
    12
    13    def test_what_is_true(self):
    14        self.assertIsInstance(True, bool)
    15        self.assertTrue(True)
    16        self.assertTrue(-1)
    17        self.assertTrue(1)
    18        self.assertTrue(-0.1)
    19        self.assertTrue(0.1)
    20
    21
    22# 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

    22# NOTES
    23# positive and negative numbers are True
    24# True is True
    25# True is not false
    26# True is a boolean
    27# 0 is False
    28# None is False
    29# False is False
    30# False is not true
    31# False is a boolean
    

in Python, 0.0 is False and positive and negative floats are True


is a string False or True?


RED: make it fail


I add a line that will fail to test_what_is_true to test if a string (anything in quotes) is True

13    def test_what_is_true(self):
14        self.assertIsInstance(True, bool)
15        self.assertTrue(True)
16        self.assertTrue(-1)
17        self.assertTrue(1)
18        self.assertTrue(-0.1)
19        self.assertTrue(0.1)
20        self.assertTrue(str())
21
22
23# NOTES

the terminal shows AssertionError

AssertionError: '' is not true

the empty string (‘’) is not True


GREEN: make it pass


I change the method

20        self.assertFalse(str())

the test passes


REFACTOR: make it better


  • I add a comment

    23# NOTES
    24# positive and negative numbers are True
    25# True is True
    26# True is not false
    27# True is a boolean
    28# the empty string is False
    29# 0 is False
    30# None is False
    31# False is False
    32# False is not true
    33# False is a boolean
    
  • I move the assertion to test_what_is_false

     6    def test_what_is_false(self):
     7        self.assertIsInstance(False, bool)
     8        self.assertFalse(False)
     9        self.assertFalse(None)
    10        self.assertFalse(0)
    11        self.assertFalse(0.0)
    12        self.assertFalse(str())
    13
    14    def test_what_is_true(self):
    
  • I add a line that will fail to test_what_is_false

    11        self.assertFalse(0.0)
    12        self.assertFalse(str())
    13        self.assertFalse("text")
    

    the terminal shows AssertionError

    AssertionError: 'text' is not false
    
  • I change the method

    13        self.assertTrue("text")
    

    the test passes

  • I add a comment

    24# NOTES
    25# a string with things is True
    26# positive and negative numbers are True
    27# True is True
    28# True is not false
    29# True is a boolean
    30# the empty string is False
    31# 0 is False
    32# None is False
    33# False is False
    34# False is not true
    35# False is a boolean
    
  • I move the assertion to test_what_is_true

    11        self.assertFalse(0.0)
    12        self.assertFalse(str())
    13
    14    def test_what_is_true(self):
    15        self.assertIsInstance(True, bool)
    16        self.assertTrue(True)
    17        self.assertTrue(-1)
    18        self.assertTrue(1)
    19        self.assertTrue(-0.1)
    20        self.assertTrue(0.1)
    21        self.assertTrue("text")
    22
    23
    24# NOTES
    

in Python, the empty string is False and a string with things is True


is a tuple False or True?


RED: make it fail


I add an assertion to test_what_is_true to see if a tuple (anything in parentheses (()) separated by commas) is True

14    def test_what_is_true(self):
15        self.assertIsInstance(True, bool)
16        self.assertTrue(True)
17        self.assertTrue(-1)
18        self.assertTrue(1)
19        self.assertTrue(-0.1)
20        self.assertTrue(0.1)
21        self.assertTrue("text")
22        self.assertTrue(tuple())
23
24
25# NOTES

The terminal shows AssertionError

AssertionError: () is not true

the empty tuple is not True


GREEN: make it pass


  • I change the assert method

    22        self.assertFalse(tuple())
    

    the test passes

  • I add a comment

    25# NOTES
    26# a string with things is True
    27# positive and negative numbers are True
    28# True is True
    29# True is not false
    30# True is a boolean
    31# the empty tuple is False
    32# the empty string is False
    33# 0 is False
    34# None is False
    35# False is False
    36# False is not true
    37# False is a boolean
    
  • I move the assertion to test_what_is_false

     6    def test_what_is_false(self):
     7        self.assertIsInstance(False, bool)
     8        self.assertFalse(False)
     9        self.assertFalse(None)
    10        self.assertFalse(0)
    11        self.assertFalse(0.0)
    12        self.assertFalse(str())
    13        self.assertFalse(tuple())
    14
    15    def test_what_is_true(self):
    

REFACTOR: make it better


  • I add another assertion to test_what_is_false to see if a tuple with things is False

    11        self.assertFalse(0.0)
    12        self.assertFalse(str())
    13        self.assertFalse(tuple())
    14        self.assertFalse((1, 2, 3, 'n'))
    15
    16    def test_what_is_true(self):
    

    the terminal shows AssertionError

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

    a tuple with things is NOT False

  • I change the method

    14        self.assertTrue((1, 2, 3, 'n'))
    

    the test passes

  • I add a comment

    26# NOTES
    27# a tuple with things is True
    28# a string with things is True
    29# positive and negative numbers are True
    30# True is True
    31# True is not false
    32# True is a boolean
    33# the empty tuple is False
    34# the empty string is False
    35# 0 is False
    36# None is False
    37# False is False
    38# False is not true
    39# False is a boolean
    
  • I move the assertion to test_what_is_true

    Tip

    In Visual Studio Code I can use alt (Windows/Linux) or option (MacOS) with the up/down arrows on the keyboard to move a line up or down

    12        self.assertFalse(str())
    13        self.assertFalse(tuple())
    14
    15    def test_what_is_true(self):
    16        self.assertIsInstance(True, bool)
    17        self.assertTrue(True)
    18        self.assertTrue(-1)
    19        self.assertTrue(1)
    20        self.assertTrue(-0.1)
    21        self.assertTrue(0.1)
    22        self.assertTrue("text")
    23        self.assertTrue((1, 2, 3, 'n'))
    24
    25
    26# NOTES
    

in Python, the empty tuple is False and a tuple with things is True


is a list False or True?


RED: make it fail


I add an assertion to test if a list (anything in square brackets ([])) is True

15    def test_what_is_true(self):
16        self.assertIsInstance(True, bool)
17        self.assertTrue(True)
18        self.assertTrue(-1)
19        self.assertTrue(1)
20        self.assertTrue(-0.1)
21        self.assertTrue(0.1)
22        self.assertTrue("text")
23        self.assertTrue((1, 2, 3, 'n'))
24        self.assertTrue(list())
25
26
27# NOTES

the terminal shows AssertionError

AssertionError: [] is not true

the empty list is NOT True


GREEN: make it pass


  • I change the method

    24        self.assertFalse(list())
    

    the test passes

  • I add a comment

    27# NOTES
    28# a tuple with things is True
    29# a string with things is True
    30# positive and negative numbers are True
    31# True is True
    32# True is not false
    33# True is a boolean
    34# the empty list is False
    35# the empty tuple is False
    36# the empty string is False
    37# 0 is False
    38# None is False
    39# False is False
    40# False is not true
    41# False is a boolean
    
  • I move the assertion to test_what_is_false

    12        self.assertFalse(str())
    13        self.assertFalse(tuple())
    14        self.assertFalse(list())
    15
    16    def test_what_is_true(self):
    

REFACTOR: make it better


  • I add another assertion to test_what_is_false to see if a list with things is False

    12        self.assertFalse(str())
    13        self.assertFalse(tuple())
    14        self.assertFalse(list())
    15        self.assertFalse([1, 2, 3, 'n'])
    16
    17    def test_what_is_true(self):
    

    the terminal shows AssertionError

    AssertionError: [1, 2, 3, 'n'] is not false
    
  • I change the method

    15        self.assertTrue([1, 2, 3, 'n'])
    

    the test passes

  • I add a comment

    28# NOTES
    29# a list with things is True
    30# a tuple with things is True
    31# a string with things is True
    32# positive and negative numbers are True
    33# True is True
    34# True is not false
    35# True is a boolean
    36# the empty list is False
    37# the empty tuple is False
    38# the empty string is False
    39# 0 is False
    40# None is False
    41# False is False
    42# False is not true
    43# False is a boolean
    
  • I move the assertion to test_what_is_true

    13        self.assertFalse(tuple())
    14        self.assertFalse(list())
    15
    16    def test_what_is_true(self):
    17        self.assertIsInstance(True, bool)
    18        self.assertTrue(True)
    19        self.assertTrue(-1)
    20        self.assertTrue(1)
    21        self.assertTrue(-0.1)
    22        self.assertTrue(0.1)
    23        self.assertTrue("text")
    24        self.assertTrue((1, 2, 3, 'n'))
    25        self.assertTrue([1, 2, 3, 'n'])
    26
    27
    28# NOTES
    

in Python, the empty list is False and a list with things is True. I can see a pattern.


is a set False or True?


RED: make it fail


I add an assertion to test_what_is_true to see if a set is True

23        self.assertTrue("text")
24        self.assertTrue((1, 2, 3, 'n'))
25        self.assertTrue([1, 2, 3, 'n'])
26        self.assertTrue(set())
27
28
29# NOTES

the terminal shows AssertionError

AssertionError: set() is not true

the empty set is NOT True


GREEN: make it pass


  • I change the method

    26        self.assertFalse(set())
    

    the test passes

  • I add a comment

    29# NOTES
    30# a list with things is True
    31# a tuple with things is True
    32# a string with things is True
    33# positive and negative numbers are True
    34# True is True
    35# True is not false
    36# True is a boolean
    37# the empty set is False
    38# the empty list is False
    39# the empty tuple is False
    40# the empty string is False
    41# 0 is False
    42# None is False
    43# False is False
    44# False is not true
    45# False is a boolean
    
  • I move the assertion to test_what_is_false

    12        self.assertFalse(str())
    13        self.assertFalse(tuple())
    14        self.assertFalse(list())
    15        self.assertFalse(set())
    16
    17    def test_what_is_true(self):
    

REFACTOR: make it better


  • I add another assertion to test_what_is_false to see if a set with things is False

    12        self.assertFalse(str())
    13        self.assertFalse(tuple())
    14        self.assertFalse(list())
    15        self.assertFalse(set())
    16        self.assertFalse({1, 2, 3, 'n'})
    17
    18    def test_what_is_true(self):
    

    the terminal shows AssertionError

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

    a set with things is NOT False

  • I change the method

    16        self.assertTrue({1, 2, 3, 'n'})
    

    the test passes

  • I add a comment

    30# NOTES
    31# a set with things is True
    32# a list with things is True
    33# a tuple with things is True
    34# a string with things is True
    35# positive and negative numbers are True
    36# True is True
    37# True is not false
    38# True is a boolean
    39# the empty set is False
    40# the empty list is False
    41# the empty tuple is False
    42# the empty string is False
    43# 0 is False
    44# None is False
    45# False is False
    46# False is not true
    47# False is a boolean
    
  • I move the assertion to test_what_is_true

    13        self.assertFalse(tuple())
    14        self.assertFalse(list())
    15        self.assertFalse(set())
    16
    17    def test_what_is_true(self):
    18        self.assertIsInstance(True, bool)
    19        self.assertTrue(True)
    20        self.assertTrue(-1)
    21        self.assertTrue(1)
    22        self.assertTrue(-0.1)
    23        self.assertTrue(0.1)
    24        self.assertTrue("text")
    25        self.assertTrue((1, 2, 3, 'n'))
    26        self.assertTrue([1, 2, 3, 'n'])
    27        self.assertTrue({1, 2, 3, 'n'})
    28
    29
    30# NOTES
    

in Python, the empty set is False and a set with things is True


is a dictionary False or True?


RED: make it fail


I add an assertion to test_what_is_true to test if a dictionary is True

17    def test_what_is_true(self):
18        self.assertIsInstance(True, bool)
19        self.assertTrue(True)
20        self.assertTrue(-1)
21        self.assertTrue(1)
22        self.assertTrue(-0.1)
23        self.assertTrue(0.1)
24        self.assertTrue("text")
25        self.assertTrue((1, 2, 3, 'n'))
26        self.assertTrue([1, 2, 3, 'n'])
27        self.assertTrue({1, 2, 3, 'n'})
28        self.assertTrue(dict())
29
30
31# NOTES

the terminal shows AssertionError

AssertionError: {} is not true

the empty dictionary is NOT True


GREEN: make it pass


  • I change assertTrue to assertFalse

    28        self.assertFalse(dict())
    

    the test passes

  • I add a comment

    31# NOTES
    32# a set with things is True
    33# a list with things is True
    34# a tuple with things is True
    35# a string with things is True
    36# positive and negative numbers are True
    37# True is True
    38# True is not false
    39# True is a boolean
    40# the empty dictionary is False
    41# the empty set is False
    42# the empty list is False
    43# the empty tuple is False
    44# the empty string is False
    45# 0 is False
    46# None is False
    47# False is False
    48# False is not true
    49# False is a boolean
    
  • I move the assertion to the test_what_is_false method

     6    def test_what_is_false(self):
     7        self.assertIsInstance(False, bool)
     8        self.assertFalse(False)
     9        self.assertFalse(None)
    10        self.assertFalse(0)
    11        self.assertFalse(0.0)
    12        self.assertFalse(str())
    13        self.assertFalse(tuple())
    14        self.assertFalse(list())
    15        self.assertFalse(set())
    16        self.assertFalse(dict())
    17
    18    def test_what_is_true(self):
    

REFACTOR: make it better


  • I add another assertion to test if a dictionary with things is also False

    14        self.assertFalse(list())
    15        self.assertFalse(set())
    16        self.assertFalse(dict())
    17        self.assertFalse({'key': 'value'})
    18
    19    def test_what_is_true(self):
    

    the terminal shows AssertionError

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

    a dictionary with things is NOT False

  • I change assertFalse to assertTrue

    17        self.assertTrue({'key': 'value'})
    

    the test passes

  • I add the last comment

    32# NOTES
    33# a dictionary with things is True
    34# a set with things is True
    35# a list with things is True
    36# a tuple with things is True
    37# a string with things is True
    38# positive and negative numbers are True
    39# True is True
    40# True is not false
    41# True is a boolean
    42# the empty dictionary is False
    43# the empty set is False
    44# the empty list is False
    45# the empty tuple is False
    46# the empty string is False
    47# 0 is False
    48# None is False
    49# False is False
    50# False is not true
    51# False is a boolean
    52
    53
    54# Exceptions seen
    55# AssertionError
    
  • I move the assertion to the test_what_is_true method

     6    def test_what_is_false(self):
     7        self.assertIsInstance(False, bool)
     8        self.assertFalse(False)
     9        self.assertFalse(None)
    10        self.assertFalse(0)
    11        self.assertFalse(0.0)
    12        self.assertFalse(str())
    13        self.assertFalse(tuple())
    14        self.assertFalse(list())
    15        self.assertFalse(set())
    16        self.assertFalse(dict())
    17
    18    def test_what_is_true(self):
    19        self.assertIsInstance(True, bool)
    20        self.assertTrue(True)
    21        self.assertTrue(-1)
    22        self.assertTrue(1)
    23        self.assertTrue(-0.1)
    24        self.assertTrue(0.1)
    25        self.assertTrue("text")
    26        self.assertTrue((1, 2, 3, 'n'))
    27        self.assertTrue([1, 2, 3, 'n'])
    28        self.assertTrue({1, 2, 3, 'n'})
    29        self.assertTrue({'key': 'value'})
    30
    31
    32# NOTES
    

in Python, the empty dictionary is False, and a dictionary with things is True


close the project

  • I close test_booleans.py in the editor

  • I click in the terminal and use q on the keyboard to leave the tests and 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

From the tests I know that in Python

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 False (0, empty or None ) or is 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