Menu
CoddyTech

nil Becomes None

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

Python spells the absence singleton None and its booleans True and False. Ruby uses nil, true, and false. Test absence with value is None, corresponding to the intent of Ruby value.nil?. The Python test checks identity with the singleton, not whether a quantity is empty or zero. Ruby predicate names may end in a question mark; Python identifiers cannot use that suffix. Python prints booleans with capital letters and prints the word None for absence, whereas Ruby puts nil prints a blank line. Keep missing data distinct from legitimate zero values when adapting input records.

Ruby

value = nil
puts value.nil?
puts false

Python

value = None
print(value is None)
print(False)

Porting trap

A Python condition such as if not seats catches zero as well as None. Use an explicit absence check when zero seats is valid data.

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

Practice on your own: Online Python compiler