Null Safety and None
Lesson 7 of 48 in Coddy's Python for Dart Developers course.
Dart distinguishes nullable and non-nullable static types; a question-mark suffix allows null and flow analysis can promote checked values. Python None represents absence, but ordinary execution does not enforce annotations such as int or int-or-None. Check absence explicitly before arithmetic. Dart booleans print lowercase true and false; Python uses capitalized True and False. Python is None tests the singleton by identity. Do not confuse a valid zero with absence. Dart's null assertion operator has no direct Python postfix equivalent; a Python annotation or assertion is not a replacement for a robust input-validation policy.
Dart
void main() {
int? value;
print(value == null);
print(false);
}Python
value = None
print(value is None)
print(False)Porting trap
A port can lose Dart's static null-safety guarantees. Add the runtime checks required by the data contract rather than trusting hints alone.
Challenge
BeginnerRead an integer seat count from 0 to 20. Print whether any seats remain, using Python's boolean spelling. On the next line print the count if it is positive, or None if it is zero. Initialize the reported value to None and replace it when seats remain.
Print only these lines, ending each with a newline. Keep the supplied input scaffold; it reads the stated values without prompts.
Your code goes in main.py. The locked driver.py starts your program and cannot be edited.
Try it yourself
# Starts the program in main.py. Edit main.py to solve the challenge.
from runpy import run_module
run_module("main", run_name="__main__")
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Python for Dart Developers
1From Dart to Python
Run Your First Python ProgramInterpolation and printvar, final, and Python NamesFloor Versus TruncationBraces and Block ScopeRecap: Rewrite a Receipt4Iteration and Transformations
for-in and rangeentries and enumerateLoop Updates and continuewhere, map, and List BuildingRecap: Build a Ranking2Values and Decisions
Null Safety and NoneList Equality ChangesConditions and Null DefaultsTernaries and elifRecap: Eligibility Rules3Collections and References
Lists and Negative IndexesRecords and Tuple UnpackingMap Lookups and KeyErrorSets and Shared ObjectsRecap: Clean an Inventory6Reliable Python Programs
stdin and Exact Formattingon, catch, finally, and withLibraries and Import ExecutionStatic Types Versus HintsRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler