Menu
CoddyTech

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 icon

Challenge

Beginner

Read 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__")
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Python for Dart Developers

Practice on your own: Online Python compiler