3.4. Logic None

3.4.1. Rationale

  • Empty (null) or unknown (unset) value

  • It is not False value

3.4.2. Syntax

  • First letter capitalized, other are lower cased

>>> data = None

3.4.3. None, Null, Nil

>>> data = none
Traceback (most recent call last):
NameError: name 'none' is not defined
>>>
>>> data = NONE
Traceback (most recent call last):
NameError: name 'NONE' is not defined
>>> data = null
Traceback (most recent call last):
NameError: name 'null' is not defined
>>>
>>> data = Null
Traceback (most recent call last):
NameError: name 'Null' is not defined
>>>
>>> data = NULL
Traceback (most recent call last):
NameError: name 'NULL' is not defined
>>> data = nil
Traceback (most recent call last):
NameError: name 'NIL' is not defined
>>>
>>> data = NIL
Traceback (most recent call last):
NameError: name 'NIL' is not defined

3.4.4. Identity Check

  • x is None - x is the same object as y

  • x is not None - x is not the same object as y

>>> data = None
>>>
>>>
>>> data is None
True
>>>
>>> data is not None
False

3.4.5. Value Check

  • Do not use == or != to check None values

  • It works, but it is a subject to change

>>> data = None
>>>
>>>
>>> data == None
True
>>>
>>> data != None
False

3.4.6. Type Casting

With if statements behaves like negative values

>>> bool(None)
False
>>> bool(False)
False
>>>
>>>
>>> None == False
False
>>>
>>> None is False
False

3.4.7. Use Case - 0x01

>>> firstname = 'Melissa'
>>> lastname = 'Lewis'
>>> age = None
>>>
>>>
>>> age is None
True

3.4.8. Assignments

Code 3.19. Solution
"""
* Assignment: Type None
* Required: yes
* Complexity: easy
* Lines of code: 5 lines
* Time: 3 min

English:
    1. What you need to put in expressions to get the expected outcome?
    2. Run doctests - all must succeed

Polish:
    1. Co należy podstawić w wyrażeniach aby otrzymać wartość oczekiwaną?
    2. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert result_a is not Ellipsis, \
    'Assign result to variable: `result_a`'

    >>> assert result_b is not Ellipsis, \
    'Assign result to variable: `result_b`'

    >>> assert result_c is not Ellipsis, \
    'Assign result to variable: `result_c`'

    >>> assert result_d is not Ellipsis, \
    'Assign result to variable: `result_d`'

    >>> assert type(result_a) is bool, \
    'Variable `result_a` has invalid type, should be bool'

    >>> assert type(result_b) is bool, \
    'Variable `result_b` has invalid type, should be bool'

    >>> assert type(result_c) is bool, \
    'Variable `result_c` has invalid type, should be bool'

    >>> assert type(result_d) is bool, \
    'Variable `result_d` has invalid type, should be bool'

    >>> bool(result_a)
    True
    >>> bool(result_b)
    False
    >>> bool(result_c)
    True
    >>> bool(result_d)
    False
"""

# bool: result must be logic True
a = ...

# bool: result must be logic False
b = ...

# bool: result must be logic True
c = ...

# bool: result must be logic False
d = ...

# Do not modify following lines
result_a = a is None
result_b = b is not None
result_c = bool(bool(c) is not bool(c)) == False
result_d = bool(bool(d) is not bool(d)) == False and bool(d)