Menu
CoddyTech

Optionals and None

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

Swift models absence with Optional and unwraps it through binding, matching, chaining, or coalescing. Python commonly uses a value or None, without a compiler-enforced unwrap step. Check is None to distinguish missing data from zero or empty text. Python does not automatically prevent operations on None; attempting an unsupported operation fails at runtime. Boolean literals and printed booleans use initial capitals in Python.

Swift example

let seats: Int? = nil
print(seats == nil)
let available = false
print(available)

Python

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

Porting trap

Python truthiness is not optional binding. A count of zero can be present and valid even though it tests false.

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

Practice on your own: Online Python compiler