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

  • none, ``NONE, null, Null, NULL, nil, NIL - does not exist in Python

>>> data = None

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.2. NoneType

  • NoneType

>>> type(None)
<class 'NoneType'>

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

4.2.4. Is vs Eq

  • 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.5. Recap

  • Empty (null) or unknown (unset) value

  • It is not False value

  • First letter capitalized, other are lower cased

  • none, ``NONE, null, Null, NULL, nil, NIL - does not exist in Python

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

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

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

Definition:

>>> data = None

Check If None:

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

4.2.6. Use Case - 1

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

4.2.7. Use Case - 2

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

4.2.8. Use Case - 3

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

4.2.9. Assignments

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Type None
# - Difficulty: easy
# - Lines: 5
# - Minutes: 3

# %% 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 sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> 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 `a is None` must be True
# type: NoneType
a = ...

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

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