Menu
CoddyTech

Nullable Values and None

Lesson 7 of 48 in Coddy's Python for C# Developers course.

Represent missing values without confusing them with zero or False.

C# reference excerpt

Executable statements use C# top-level program style; type-only examples are declarations. System names assume the usual using System directive.

int? seats = null;
bool missing = seats == null;
Console.WriteLine(missing);

Python

seats = None
missing = seats is None
print(missing)

What changes

None is a singleton object used for absence. C# nullable value types such as int? and nullable reference annotations solve different static-type problems; Python names need no nullable declaration to hold None. Python boolean literals are True and False. Comparisons create bool values, and print displays their capitalized names.

Transfer mistake

None, zero and an empty string are different values. Test absence with is None rather than treating every false-valued object as missing.

Your task

Read a seat count from 0 through 20. Output the bool result of testing whether seats remain. On line two output the count if positive, otherwise None. Start the reported value at None and replace it only when seats remain. Use Python's printed boolean and None spellings.

challenge icon

Challenge

Beginner

Read a seat count from 0 through 20. Output the bool result of testing whether seats remain. On line two output the count if positive, otherwise None. Start the reported value at None and replace it only when seats remain. Use Python's printed boolean and None spellings.

Editable implementation files: main.py. The locked driver.py is the supplied launcher or input/output harness and must remain unchanged.

Use the supplied input contract without prompts. Print only the required lines, each ending in a newline.

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 C# Developers

Practice on your own: Online Python compiler