booleans


what are booleans?

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

preview

Here 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 the questions you can answer after going through this chapter


start the project

  • I name this project booleans

  • I open a terminal

  • then I make a directory for the project

    mkdir booleans
    

    the terminal goes back to the command line

    .../pumping_python
    
  • I change directory to the project

    cd booleans
    

    the terminal shows I am now in the booleans folder

    .../pumping_python/booleans
    
  • I make a folder for the source code

    mkdir src
    

    the terminal goes back to the command line

    .../pumping_python/booleans
    
  • I use touch to make an empty file for the program in the src folder

    touch src/booleans.py
    

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

    New-Item src/booleans.py
    

    the terminal goes back to the command line

    .../pumping_python/booleans
    
  • I make a directory for the tests

    mkdir tests
    

    the terminal goes back to the command line

  • I use touch to make an empty file in the tests folder to tell Python that it is a Python package

    Attention

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

    touch tests/__init__.py
    

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

    New-Item tests/__init__.py
    

    the terminal goes back to the command line

  • I make an empty file for the actual test

    touch tests/test_booleans.py
    

    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
    

    the terminal goes back to the command line

  • I open test_booleans.py in the editor of the Integrated Development Environment (IDE)

    Tip

    I can open a file from the terminal in Visual Studio Code by typing code and the name of the file with

    code tests/test_booleans.py
    

    test_booleans.py opens up 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 virtual environment in the terminal

    python3 -m venv .venv
    

    on Windows without Windows Subsystem for Linux use python3 -m venv .venv instead of python3 -m venv .venv

    python -m venv .venv
    

    the terminal takes some time then goes back to the command line

  • I activate the virtual environment

    source .venv/bin/activate
    

    on Windows without Windows Subsystem for Linux use .venv/bin/activate.ps1 instead of source .venv/bin/activate

    .venv/scripts/activate.ps1
    

    the terminal shows

    (.venv) .../pumping_python/booleans
    
  • I upgrade the Python package manager (pip) to the latest version

    python3 -m pip install --upgrade pip
    

    the terminal shows pip being uninstalled then installs the latest version or shows that it is already the latest version

  • I make a requirements.txt file for the Python programs my project needs

    echo "pytest-watch" > requirements.txt
    

    the terminal goes back to the command line

  • I use pip to use the requirements file to install pytest-watch

    python3 -m pip install --requirement requirements.txt
    

    on Windows without Windows Subsystem for Linux use python -m pip install --requirement requirements.txt instead of python3 -m pip install --requirement requirements.txt

    python -m pip install --requirement requirements.txt
    

    the terminal shows pip downloads and installs the Python programs that pytest-watch needs to run

  • I use pytest-watch to run the test

    pytest-watch
    

    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 hold ctrl (Windows/Linux) or option or command (MacOS) on the keyboard and use the mouse to click on tests/test_booleans.py:7 to open it in the editor

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

     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 from testing None to check if False is a child/instance of the bool class expecting a failure

    1import unittest
    2
    3
    4class TestBooleans(unittest.TestCase):
    5
    6    def test_what_is_false(self):
    7        self.assertNotIsInstance(False, bool)
    

    the terminal shows AssertionError

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

GREEN: make it pass

I change assertNotIsInstance to assertIsInstance

7        self.assertIsInstance(False, bool)

the test passes and I add a note

10# NOTES
11# False is a boolean
12
13
14# Exceptions seen

so far this is going over what I already know from testing None


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'>

GREEN: make it pass

  • I change the assert method

    10        self.assertIsInstance(True, bool)
    

    the test passes

  • I add another note

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

REFACTOR: make it better

  • I add a failing line to test_what_is_true

     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 change assertTrue to assertFalse

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

    the test passes

  • I add a note

    13# NOTES
    14# True is a boolean
    15# False is not true
    16# False is a boolean
    17
    18
    19# Exceptions seen
    20# AssertionError
    
  • I move the line from test_what_is_true to the test_what_is_false 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)
    

    Tip

    You do not need to copy and paste to move a line. If you are using Visual Studio Code you can use alt (Windows/Linux) or option (MacOS) with the up/down arrows on the keyboard to move a line up or down

  • I add a note

    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 add a failing line to test_what_is_false

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

    the terminal shows AssertionError

    AssertionError: True is not false
    
  • 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)
    

    the test passes

  • I add a note

    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 move the line 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)
    
  • I add another note

    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
    

All of this is still a repetition of what I did with AssertionError. Next up, I test the other Python basic data types to see which of are False or True


is None False or True?

RED: make it fail

I add a line in 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)

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 move the line 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)

I add a note

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

None is False though I learned in test_assertion_error_w_false that False is not None


is an integer False or True?

RED: make it fail

I add a failing line test_what_is_false 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

GREEN: make it pass

I change the method

10        self.assertTrue(-1)

the test passes

REFACTOR: make it better

  • I move the line 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)
    

    I use -1 for all the integers (whole numbers) that are smaller than 0. Negative integers are True in Python

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

    14        self.assertTrue(-1)
    15        self.assertTrue(0)
    

    the terminal shows AssertionError

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

    15        self.assertFalse(0)
    

    the test passes

  • I move the line to test_what_is_false

     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

    10        self.assertFalse(0)
    11        self.assertFalse(1)
    12
    13    def test_what_is_true(self):
    14        self.assertIsInstance(True, bool)
    

    the terminal shows AssertionError

    AssertionError: 1 is not false
    
  • I change the method

    11        self.assertTrue(1)
    

    the test passes

  • I move the line to test_what_is_true

    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)
    

    I use 1 for all the integers (whole numbers) that are bigger than 0. Positive and negative integers are True in Python

  • I add notes

    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
    29
    30
    31# Exceptions seen
    32# AssertionError
    

is a float False or True?

RED: make it fail

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

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

GREEN: make it pass

  • I change the method

    11        self.assertTrue(-0.1)
    

    the test passes

  • I move the line to test_what_is_true

    14        self.assertTrue(True)
    15        self.assertTrue(-1)
    16        self.assertTrue(1)
    17        self.assertTrue(-0.1)
    

    I use -0.1 for all the floating point numbers that are smaller than 0.0. 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

    17        self.assertTrue(-0.1)
    18        self.assertTrue(0.0)
    

    the terminal shows AssertionError

    AssertionError: 0.0 is not true
    

    I change the method

    18        self.assertFalse(0.0)
    

    the test passes

  • I move the line to test_what_is_false

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

    0.0 is False in Python

  • I add another line to test_what_is_false to see if 0.1 is False in Python

    11        self.assertFalse(0.0)
    12        self.assertFalse(0.1)
    13
    14    def test_what_is_true(self):
    15        self.assertIsInstance(True, bool)
    

    the terminal shows AssertionError

    AssertionError: 0.1 is not false
    
  • I change the method

    15        self.assertTrue(0.1)
    

    the test passes

  • I move the line to test_what_is_true

    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 use 0.1 for all the floating point numbers that are bigger than 0.0. Positive and negative floats are also True in Python

  • I add notes

    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 make the new notes 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
    32
    33
    34# Exceptions seen
    

is a string False or True?

RED: make it fail

I add a failing line 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 move the line to test_what_is_false

    11        self.assertFalse(0.0)
    12        self.assertTrue(str())
    13
    14    def test_what_is_true(self):
    

    the empty string (‘’) is not False

  • I add a failing line 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 move the line to test_what_is_true

    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
    
  • I add notes

    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
    36
    37
    38# Exceptions seen
    39# AssertionError
    

is a tuple False or True?

RED: make it fail

I add a line to test_what_is_true to see if a tuple (anything in parentheses (())) 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 move the line 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 line to test_what_is_false to see if a tuple with things is False

    13        self.assertFalse(tuple())
    14        self.assertFalse((1, 2, 3, 'n'))
    

    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 move the line to test_what_is_true

    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
    

    Tip

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

  • I add notes

    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
    

is a list False or True?

RED: make it fail

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

23    self.assertTrue((1, 2, 3, 'n'))
24    self.assertTrue(list())

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 move the line to test_what_is_false

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

REFACTOR: make it better

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

    14        self.assertFalse(list())
    15        self.assertFalse([1, 2, 3, 'n'])
    16
    17    def test_what_is_true(self):
    18        self.assertIsInstance(True, bool)
    

    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 move the line to test_what_is_true

    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
    
  • I add notes

    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
    44
    45
    46# Exceptions seen
    47# AssertionError
    

I can see a pattern forming


is a set False or True?

RED: make it fail

I add a line to in test_what_is_true to see if a set is True

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 move the line to test_what_is_false

    14        self.assertFalse(list())
    15        self.assertFalse(set())
    16
    17    def test_what_is_true(self):
    

REFACTOR: make it better

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

    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 move the line to test_what_is_true

    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
    
  • I add to the notes

    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
    48
    49
    50# Exceptions seen
    51# AssertionError
    

is a dictionary False or True?

RED: make it fail

I add a line to test_what_is_true to test if a dictionary is True

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 move the line 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 line 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'})
    

    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 move the line 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
    
  • I add the last 2 notes

    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
    

close the project

  • I close the file(s) I have open in the editor(s)

  • I click in the terminal and exit the tests with ctrl+c on the keyboard

  • I deactivate the virtual environment

    deactivate
    

    the terminal goes back to the command line, (.venv) is no longer on the left side

    .../pumping_python/booleans
    
  • 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 can see that in Python

these things come in handy when I write conditions in programs, because I can make decisions on whether the data is empty 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


please leave a review