Python
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax and Data Types
-
Section 3: Collections
-
39: Set Operations: Union, Intersection, Difference
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Turtle Graphics and Early Practice Projects
-
Section 7: Working with Files and I/O
-
Section 8: Regular Expressions
-
Section 9: Object-Oriented Python
-
Section 10: Error Handling
-
Section 11: Modules and Packages
-
Section 12: Iterators, Generators, and Functional Tools
-
Section 13: Decorators and Metaprogramming
-
Section 14: Concurrency and Parallelism
-
Section 15: Working with Dates, Times, and Numbers
-
Section 16: Standard Library Deep Dive I: Data Structures
-
Section 17: Standard Library Deep Dive II: System and Introspection
-
Section 18: Standard Library Deep Dive III: Security and Encoding
-
Section 19: Standard Library Deep Dive IV: Text and Data Utilities
-
Section 20: Networking and Web Basics
-
Section 21: Working with Databases
-
Section 22: Testing and Quality
-
Section 23: Advanced Typing
-
Section 24: Context Managers and Resource Handling
-
Section 25: Text, Unicode, and Binary Data
-
Section 26: More Functional and Iteration Tools
-
Section 27: Data Validation and Configuration
-
Section 28: Working with Images and Media
-
Section 29: Property-Based and Documentation Testing
-
Section 30: Packaging and Deployment
-
Section 31: Performance and Internals
-
Section 32: Design Patterns in Python
-
Section 33: GUI Programming
-
Section 34: Security Basics
-
Section 35: Data Structures and Algorithms
-
Section 36: Practical Projects
-
Section 37: Capstone Projects
-
Section 38: Interview and Algorithm Practice
-
Section 39: Writing Idiomatic Python
27: The None Type and Its Uses
What exactly is None, and is it just like zero or False?
I see this come up a lot. The short answer is: no, it's not. In Python, None is its own data type (NoneType). It isn't a number, it isn't an empty string, and it isn't a boolean False.
Think of None as a way of saying, "There is no value here," or "This is intentionally empty." If you have a variable set to 0, you have a number. If you have a variable set to None, you have a placeholder indicating the absence of a value. I usually use it when I want to initialize a variable but I'm not yet sure what its actual value should be.
# None is not the same as other "empty" things
print(None == 0) # False
print(None == False) # False
print(None == "") # False
Why does my function return None even when I didn't write a return statement?
This is one of those things that trips everyone up at first. In Python, every single function returns something. If you don't explicitly use the return keyword to send a value back, Python automatically returns None behind the scenes.
A classic example is using print() inside a function. Remember that print() just puts text on the screen; it doesn't "give" a value back to the code. If your function's last action is a print() call, the function itself still returns None.
def greet(name):
print(f"Hello, {name}!")
result = greet("Alice")
# "Hello, Alice!" is printed to the console, but...
print(result) # This will print: None
If you want result to actually hold a value, you have to use return. I've spent way too many hours debugging my own code only to realize I forgot one return statement at the end of a helper function.
Should I use == None or is None to check for emptiness?
You'll see both in old tutorials, but in modern Python, you should almost always use is None.
Here is the technical reason: == checks for equality (do these two things have the same value?), while is checks for identity (are these two things actually the exact same object in memory?). Since None is a singleton—meaning there is only ever one instance of None in your entire running program—checking identity is faster and safer.
user_bio = None
if user_bio is None:
print("The user hasn't written a bio yet.")
if user_bio is not None:
print("Here is the bio!")
How do I use None to handle optional function arguments?
This is probably the most practical "pro tip" I can give you regarding None. You might be tempted to use an empty list as a default argument, like def add_item(item, my_list=[]). Don't do that. Python creates that default list once when the function is defined, not every time it's called, which leads to some very weird bugs where items persist between different function calls.
Instead, use None as the default, and then assign the actual empty list inside the function. It's a standard Python idiom that you'll see in almost every professional codebase.
def add_to_inventory(item, inventory=None):
if inventory is None:
inventory = [] # Create a fresh list only if one wasn't provided
inventory.append(item)
return inventory
# Now it behaves exactly as you'd expect
print(add_to_inventory("Sword")) # ['Sword']
print(add_to_inventory("Shield")) # ['Shield']
📋 Practical Task
Building a User Profile Formatter
You are building a system to display user profiles. Some users provide a middle name and a biography, while others leave them blank (represented as None). Your goal is to write a function that cleans up this data for display.
Requirements:
- Create a function called
format_profile(first, middle, last, bio). - If
middleisNone, the full name should be"First Last". - If
middleis provided, the full name should be"First Middle Last". - If
bioisNone, the bio section should display as"No biography provided.". - The function should return a formatted string combining the full name and the bio.
Test your code with these two scenarios:
# Scenario 1: Complete profile
print(format_profile("Jane", "Anne", "Doe", "Software Engineer from NY"))
# Scenario 2: Missing middle name and bio
print(format_profile("John", None, "Smith", None))
There are no comments for now.