Menu
CoddyTech

None, nullptr, and bool

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

C++ nullptr is a null pointer value; Python None is an ordinary singleton object representing absence. Python names do not have pointer types to dereference. Test absence with is None. None is distinct from zero and False. Python boolean literals are capitalized True and False, and print keeps those capitals. A missing quantity should be handled before comparing it with an integer.

C++17 excerpt: inside main

Standard-library headers are supplied in the runnable companion program.

int* seats = nullptr;
std::cout << std::boolalpha << (seats == nullptr) << '\n' << false << '\n';

Python

seats = None
print(seats is None)
available = False
print(available)

Porting trap

Do not turn every false value into missing data. An available count of zero can be valid even though bool(0) is False.

Implement the task below in its editable Python modules. The locked driver.py supplies execution and checks. Keep the specified imports and API names.

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

Practice on your own: Online Python compiler