Null and None
Lesson 7 of 48 in Coddy's Python for GDScript Developers course.
GDScript uses null, true and false; Python uses None, True and False. Python tests absence with is None. GDScript's is operator tests a type, whereas Python is tests identity, so an absence check is not a direct keyword-for-keyword port. A dictionary can contain a key whose value is absent, and key membership must be tested separately from that stored value.
GDScript
extends SceneTree
func _init():
var data = {"seats": null}
print(data["seats"] == null)
print(data.has("seats"))
quit()Python
data = {"seats": None}
print(data["seats"] is None)
print("seats" in data)Porting trap
A freed Godot Object may require is_instance_valid rather than a simple null check. Python None is an absence sentinel, not an engine object-lifecycle check. This course uses ordinary data objects rather than Nodes that need freeing.
Complete the task in its editable modules. The locked driver.py supplies execution; keep the required API names.
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 GDScript Developers
1From GDScript to Python
Run Your First Python ProgramPrint Is Not printsVar, Const, and BindingsInteger Division DifferencesSimilar Indentation, New ScopeRecap: Rewrite a Receipt4Iteration and Transformations
Ranges and RebindingItems and enumerateWhile and Early ExitArray Callbacks and KeysRecap: Build a Ranking2Values and Decisions
Null and NoneThe is Keyword Changes MeaningLogical Results and OperandsConditional ExpressionsRecap: Eligibility Rules3Collections and Shared State
Array and List APIsArrays, Vectors, and TuplesDictionary Lookup RulesDuplicate and Shallow CopyRecap: Clean an Inventory6Input, Errors, and Modules
Console Work Outside GodotValidation and ExceptionsScript Resources and ImportsTyped GDScript and AnnotationsRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler