5.2. Logic None
Empty (null) or unknown (unset) value
It is not
FalsevalueFirst letter capitalized, other are lower cased
Example:
>>> name = None
>>> age = None
>>> result = None
>>> friends = None
>>> groups = None
5.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
5.2.2. NoneType
NoneType
>>> type(None)
<class 'NoneType'>
5.2.3. Check If None
x is None-xis the same object asyx is not None-xis not the same object asy
>>> data = None
>>>
>>>
>>> data is None
True
>>>
>>> data is not None
False
5.2.4. Is vs Eq
Do not use
==or!=to checkNonevaluesIt works, but it is a subject to change
>>> data = None
>>>
>>>
>>> data == None
True
>>>
>>> data != None
False
5.2.5. Recap
Empty (null) or unknown (unset) value
It is not
FalsevalueFirst letter capitalized, other are lower cased
none, ``NONE,null,Null,NULL,nil,NIL- does not exist in Pythonx is None-xis the same object asyx is not None-xis not the same object asyDo not use
==or!=to checkNonevalues
Definition:
>>> data = None
Check If None:
>>> data = None
>>>
>>>
>>> data is None
True
>>>
>>> data is not None
False
5.2.6. Use Case - 1
>>> adult = False # Person is not adult
>>> adult = None # We don't know is person is adult
5.2.7. Use Case - 2
>>> age = False # False is invalid in this context
>>> age = None # Age is unknown
5.2.8. Use Case - 3
>>> firstname = 'Alice'
>>> lastname = 'Apricot'
>>> age = None
>>>
>>> age is None
True
5.2.9. Assignments
# %% About
# - Name: Type None
# - Difficulty: easy
# - Lines: 5
# - Minutes: 3
# %% 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
# %% English
# 1. Define `result1: bool` with result of `result1 is None == True`
# 2. Define `result2: bool` with result of `result2 is not None == False`
# 3. Define `result3: bool` with result of `bool(result3) is not bool(result3) == False`
# 4. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result1: bool` z wynikiem `result1 is None == True`
# 2. Zdefiniuj `result2: bool` z wynikiem `result2 is not None == False`
# 3. Zdefiniuj `result3: bool` z wynikiem `bool(result3) is not bool(result3) == False`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> type(result1)
# <class 'NoneType'>
#
# >>> type(result2)
# <class 'NoneType'>
#
# >>> type(result3)
# <class 'NoneType'>
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert 'result1' in globals(), \
'Variable `result1` is not defined; assign result of your program to it.'
>>> assert result1 is not Ellipsis, \
'Variable `result1` has an invalid value; assign result of your program to it.'
>>> assert 'result2' in globals(), \
'Variable `result2` is not defined; assign result of your program to it.'
>>> assert result2 is not Ellipsis, \
'Variable `result2` has an invalid value; assign result of your program to it.'
>>> assert 'result3' in globals(), \
'Variable `result3` is not defined; assign result of your program to it.'
>>> assert result3 is not Ellipsis, \
'Variable `result3` has an invalid value; assign result of your program to it.'
>>> assert type(result1) is type(None), \
'Variable `result1` has an invalid type; expected: `None`.'
>>> assert type(result2) is type(None), \
'Variable `result2` has an invalid type; expected: `None`.'
>>> assert type(result3) is type(None), \
'Variable `result3` has an invalid type; expected: `None`.'
>>> type(result1)
<class 'NoneType'>
>>> type(result2)
<class 'NoneType'>
>>> type(result3)
<class 'NoneType'>
>>> result1 is None
True
>>> result2 is not None
False
>>> bool(result3) is not bool(result3) == False
False
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result1: None
result2: None
result3: None
# %% Data
# %% Result
result1 = ...
result2 = ...
result3 = ...