4.2. Logic None

  • Empty (null) or unknown (unset) value

  • It is not False value

  • First letter capitalized, other are lower cased

4.2.1. Syntax

  • First letter capitalized, other are lower cased

>>> data = None

4.2.2. 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

4.2.3. Check If None

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

4.2.4. Type Conversion

With if statements behaves like negative values

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

4.2.5. Use Case - 0x01

>>> adult = False  # Person is not adult
>>> adult = None   # We don't know is person is adult

4.2.6. Use Case - 0x02

>>> age = False  # False is invalid in this context
>>> age = None   # Age is unknown

4.2.7. Use Case - 0x03

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

4.2.8. Assignments

"""
* Assignment: Type None
* Type: class assignment
* 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 a is not Ellipsis, \
    'Assign your result to variable `a`'
    >>> assert b is not Ellipsis, \
    'Assign your result to variable `b`'
    >>> assert c is not Ellipsis, \
    'Assign your result to variable `c`'
    >>> assert type(a) is type(None), \
    'Variable `a` has invalid type, should be NoneType'
    >>> assert type(b) is type(None), \
    'Variable `b` has invalid type, should be NoneType'
    >>> assert type(c) is type(None), \
    'Variable `c` has invalid type, should be NoneType'

    >>> a is None
    True
    >>> b is not None
    False
    >>> bool(c) is not bool(c) == False
    False
"""

# Result of `... is None` must be True
# type: bool
a = ...

# Result of `... is not None` must be False
# type: bool
b = ...

# Result of `bool(...) is not bool(...) == False` must be True
# type: bool
c = ...