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
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 R Developers
1From R to Python
Run Your First Python Programcat, print, and StringsBindings and Copy SemanticsInteger Arithmetic and NABraces Become SuitesRecap: Rewrite a Receipt4Iteration and Transformations
seq_along and rangeitems and enumeratenext and continuelapply and ComprehensionsRecap: Build a Ranking2Values and Decisions
NULL, NA, and NoneVector Equality and IdentityScalar Conditions, Not Vectorsifelse and Scalar SelectionRecap: Eligibility Rules3Vectors to Containers
Vectors, Lists, and Index ZeroTuples and Exact UnpackingNamed Lists and Dictionariesunique and Shallow CopiesRecap: Clean an Inventory6Reliable Python Programs
readLines and Text ContractsConditions and Cleanupsource and Module ImportsAnnotations and Runtime TypesRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler