3.4. Logic None¶
Empty (null) or unknown (unset) value
It is not
False
valueFirst letter capitalized, other are lower cased
3.4.1. Syntax¶
First letter capitalized, other are lower cased
>>> data = None
3.4.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
3.4.3. Check If None¶
x is None
-x
is the same object asy
x is not None
-x
is not the same object asy
>>> data = None
>>>
>>>
>>> data is None
True
>>>
>>> data is not None
False
Do not use
==
or!=
to checkNone
valuesIt works, but it is a subject to change
>>> data = None
>>>
>>>
>>> data == None
True
>>>
>>> data != None
False
3.4.4. Type Casting¶
With if
statements behaves like negative values
>>> bool(None)
False
>>> bool(False)
False
>>> None == False
False
>>>
>>> None is False
False
3.4.5. Use Case - 0x01¶
>>> adult = False # Person is not adult
>>> adult = None # We don't know is person is adult
3.4.6. Use Case - 0x02¶
>>> age = False # False is invalid in this context
>>> age = None # Age is unknown
3.4.7. Use Case - 0x03¶
>>> firstname = 'Melissa'
>>> lastname = 'Lewis'
>>> age = None
>>>
>>>
>>> age is None
True
3.4.8. Assignments¶
"""
* 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 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 = ...