
1. Python’s falsy Values
None
False
0
(integer)
0.0
(float)
""
(empty string)
[]
(empty list)
()
(empty tuple)
{}
(empty dictionary)
set()
(empty set)
2. Use-Case Of falsy Values
If some variable
is None
, then:
if not variable:
-> Will evaluate to True
3. Empty Node
: Code Example
Node
and LinkedList
previously introduced here.
class Node():
def __init__(self, data):
self.data=data
self.next_node=None
class LinkedList():
def __init__(self, first_node:Node|None=None):
self.first_node: Node|None = first_node
from typing import Optional
class LinkedList_v2():
def __init__(self, first_node: Optional[str|Node]=None):
if isinstance(first_node,str):
self.first_node = Node(first_node)
elif isinstance(first_node,Node):
self.first_node = first_node
else:
self.first_node = None
ll = LinkedList()
print(f"LinkedList without an allocated initial Node means")
print(f"The [ll.first_node] is: \n\t{ll.first_node}") # Empty Node
print()
if not ll.first_node:
print(f"The expression 'If not ll.first_node:' will evaluate to:")
print(f"\t{not ll.first_node}")
LinkedList without an allocated initial Node means
The [ll.first_node] is:
None
The expression 'If not ll.first_node:' will evaluate to:
True