Python Booleans, None & Console I/O

๐Ÿ Python 3.12+ ๐ŸŸข Chapter 5 of 65 ๐Ÿ“‚ Phase 1: Python Basics ๐Ÿ“… 2026 Edition
๐Ÿ“Œ Covered in this chapter: bool Data Type ยท Truthy vs Falsy ยท None Singleton ยท print() & input() Guide
Deep dive into boolean logic, Truthy and Falsy rules, the NoneType singleton, and console I/O with print() and input().
1The Boolean Data Type (bool)

The bool data type holds True or False. In Python, bool is a subclass of int (where True == 1 and False == 0):

๐Ÿ’ป Example 1: Boolean Values in Python
# Boolean flags representing user status:
is_logged_in = True
has_premium_access = False

print("Logged in status:", is_logged_in)
print("Premium access:", has_premium_access)

# In Python arithmetic, True acts as 1 and False acts as 0:
print("True + True equals:", True + True)  # 2
print("True * 50 equals:", True * 50)      # 50
๐Ÿ” Boolean Architecture:

Because issubclass(bool, int) is True, booleans participate in arithmetic operations seamlessly.

2Truthy vs Falsy Evaluation Rules

In Python, empty sequences ("", [], {}), zero numbers (0, 0.0), and None evaluate to Falsy. Everything else evaluates to Truthy:

๐Ÿ’ป Example 2: Truthy vs Falsy Evaluation
# An empty shopping cart list is Falsy:
cart = []

if not cart:
    print("๐Ÿ›’ Your cart is currently empty! Please add items.")
else:
    print("Cart items:", cart)

# Adding an item makes the list Truthy:
cart.append("Python Masterclass Book")
if cart:
    print("โœ… Cart now contains:", cart)
๐Ÿ” The Pythonic Way:

Never write if len(cart) == 0:. The clean Pythonic approach is simply if not cart:.

3The None Singleton Object & "is None" Identity Check

None represents the absence of a value or null state. Always check for it using if val is None: (not == None):

๐Ÿ’ป Example 3: Checking None with the "is" Operator
# A variable representing uninitialized profile data:
user_address = None

if user_address is None:
    print("Address has not been provided yet.")
else:
    print("Delivery Address:", user_address)
๐Ÿ” Why use "is None"?

None is a singleton object in memory. is None checks pointer address equality in a single CPU instruction, which is faster and safer than == None.

โš ๏ธ Common Developer Pitfall: Comparing Booleans with == True or == False

Avoid writing "if is_valid == True:". The Pythonic way is simply "if is_valid:". Similarly, instead of "if is_valid == False:", write "if not is_valid:".

๐Ÿ’ป Hands-on Interactive Practice Challenge

Check if a shopping cart list is empty using Truthy/Falsy evaluation.

Python 3 Practice Challenge โ–ถ Run in Compiler
cart_items = []  # Empty list is Falsy

if not cart_items:
    print("๐Ÿ›’ Your cart is empty! Please add items.")
else:
    print("Cart has items:", cart_items)
Run This Challenge in Online Python IDE โ†’
โ“ Frequently Asked Questions (FAQ)

Q Why does bool(True + True) equal True, but True + True equals 2?

In Python, bool inherits from int. In arithmetic expressions (True + True), Python treats them as integers (1 + 1 = 2). When passed to bool(2), any non-zero integer evaluates to True.

Q Why should I use "is None" instead of "== None"?

None is a singleton object in Python memory. is None checks pointer identity in a single CPU instruction without invoking the class equality operator __eq__(), which could be overridden by custom objects.

Q How do I read input securely without displaying passwords on screen?

Use the standard library getpass module: import getpass; pwd = getpass.getpass("Password: ") hides user keystrokes in the terminal.

OC
Written by Our Compiler Technical Editorial Team
Reviewed for accuracy & tested on Python 3.12+ runtime ยท Last updated August 2026