nil Versus None
Lesson 7 of 48 in Coddy's Python for Lua Developers course.
Python uses the singleton None to represent absence and tests it with is None. Lua uses nil, which also removes a table key when assigned as its value. Python dictionaries can store None as an actual value without removing a key. Missing Python dictionary subscripts raise KeyError rather than returning an absence value. Python boolean literals start with capitals: True and False. Keep zero distinct from missing data even though Python treats zero as false.
Lua example
local seats = nil
print(seats == nil)
local values = {seat = 2}
values.seat = nil
print(values.seat == nil)Python
seats = None
print(seats is None)
available = False
print(available)Porting trap
Translating table[key] = nil to d[key] = None preserves the key. Use del or pop when removal is intended.
Implement the specified API in the editable Python files. The locked driver.py runs the program and supplies input/output. Keep its imports and names unchanged.
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 Lua Developers
1From Lua to Python
Run Your First Python ProgramPrinting and TextNames and Local ScopeNumbers and ConversionSuites Instead of endRecap: Rewrite a Receipt4Iteration and Transformations
Numeric for to rangepairs, ipairs, and itemsLoop Control and SkipsComprehensions and SortingRecap: Build a Ranking2Values and Decisions
nil Versus NoneTable Identity and EqualityTruthiness Is Differentelif and Value SelectionRecap: Eligibility Rules3Tables to Typed Collections
Sequence Tables to ListsTuples and Multiple ReturnsRecords to DictionariesSets and Shared TablesRecap: Clean an Inventory6Errors and Modules
Input and Exact Textpcall to Exceptionsrequire to importHints and Runtime TypesRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler