Menu
CoddyTech

NULL, NA, and None

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

R distinguishes NULL, a zero-length absence object, from NA, a missing element that can appear inside a vector. Python None is a singleton often used for absence; it does not automatically implement R's missing-value propagation. Python booleans are True and False, not R's TRUE and FALSE. Use is None for an absence check. R is.na tests elementwise missingness; is.null tests NULL itself. Do not translate every NA into a zero or assume Python arithmetic will propagate None: arithmetic with None generally raises a type error. The exercises explicitly define missing tokens and valid zero quantities.

R

cat(is.null(NULL), "\n", sep="")
cat(is.na(NA_integer_), "\n", sep="")
cat(length(NULL), "\n", sep="")

Python

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

Porting trap

Choose a missing-data policy at the boundary. None is not a drop-in replacement for a typed R NA vector element.

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

Practice on your own: Online Python compiler