3.2. Logic None

3.2.1. Rationale

  • Empty (null) or unknown (unset) value

>>> firstname = 'Mark'
>>> lastname = 'Watney'
>>> age = None

3.2.2. Definition

  • First letter capitalized, other are lower cased

>>> data = None

It is not False value:

>>> None == False
False
>>>
>>> None is False
False

3.2.3. Type Casting

With if statements behaves like negative values

>>> bool(False)
False
>>>
>>> bool(None)
False

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

>>> firstname = 'Mark'
>>> lastname = 'Watney'
>>> age = None
>>>
>>> age is None
True
>>> age is not None
False

3.2.5. Value Check

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

  • It works, but it is a subject to change

>>> firstname = 'Mark'
>>> lastname = 'Watney'
>>> age = None
>>>
>>> age == None
True
>>> age != None
False

3.2.6. Use Case

>>> dane = [3, 1, 2]
>>> dane.append(4)
>>> dane.sort()
>>> dane
[1, 2, 3, 4]
>>> dane = [3, 1, 2]
>>> dane.append(4).sort()
Traceback (most recent call last):
AttributeError: 'NoneType' object has no attribute 'sort'

3.2.7. Assignments

Code 3.3. 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 result_e is not Ellipsis, \
    'Assign result to variable: `result_e`'
    >>> 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'
    >>> assert type(result_e) is bool, \
    'Variable `result_e` has invalid type, should be bool'

    >>> bool(result_a)
    True
    >>> bool(result_b)
    False
    >>> bool(result_c)
    True
    >>> bool(result_d)
    False
    >>> bool(result_e)
    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 = ...

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

# 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)
result_e = (bool(bool(e) is not bool(e)) == False and bool(e)) and (e is not None)