Menu
CoddyTech

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 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 GDScript Developers

Practice on your own: Online Python compiler