6.10. Boolean Negation

  • not logically inverts

>>> not True
False
>>> not False
True

6.10.1. SetUp

Simulate user input (for test automation):

>>> from unittest.mock import MagicMock
>>> input = MagicMock(side_effect=['', 'Mark Watney'])

6.10.2. Checking If Empty

>>> name = input('What is your name?: ')  #input: ''
>>>
>>> if name:
...     print('Not empty')
...
>>> if not name:
...     print('Empty')
Empty
>>> name = input('What is your name?: ')  #input: 'Mark Watney'
>>>
>>> if name is not None:
...     print(f'Hello {name}')
Hello Mark Watney

6.10.3. Control Flow

  • not negates (logically inverts) condition

>>> crew = {'mwatney', 'mlewis', 'rmartinez'}
>>>
>>> if 'avogel' not in crew:
...     print('You are not assigned to the crew')
You are not assigned to the crew