Equality and Identity
Lesson 8 of 48 in Coddy's Python for JavaScript Developers course.
Choose a value comparison or an identity check based on the question you mean to ask.
JavaScript
const answer = null;
console.log(answer === null);
console.log(4 === "4");
console.log(4 === 4.0);Python
answer = None
print(answer is None)
print(4 == "4")
print(4 == 4.0)What changes
Both complete programs report true, false, and true in that order, with each language using its own boolean capitalization. Python == asks about equality of values. is asks whether two expressions refer to the very same object. Use value is None to test the singleton absence value, and value is not None for its opposite. For text content and ordinary numeric comparisons, use == or !=. Python does not make a string equal to a number by automatically parsing it, so 4 == "4" is false. However, numeric types can compare equal: 4 == 4.0 is true. Booleans also participate numerically, so True == 1 is true.
Common mistake
JavaScript === is not a universal instruction to write is. Equal strings or numbers need not have identical object identity, and implementation reuse can hide a mistaken identity check in small examples. Do not rely on that reuse.
Your task
Compare two optional labels for equality, then check each for absence. The empty string remains a present label and must not be converted to None.
Challenge
BeginnerRead two text labels on separate lines. The reserved token missing means an absent label; all other lines, including an empty line, are actual strings. The scaffold converts that token to None. Print whether the labels have equal values, then whether the first is absent, then whether the second is absent, one Python boolean per line.
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 JavaScript Developers
1From JavaScript to Python
Run Your First Python ProgramPrint, Comments, and StringsVariables Without let or constNumbers and ConversionBlocks Use IndentationRecap: Rewrite a Receipt4Iteration and Transformations
for Loops Iterate ValuesIndexes and Dict Iterationwhile, break, and continueComprehensions and SortingRecap: Build a Ranking2Values and Decisions
None, True, and FalseEquality and IdentityTruthiness and Short-CircuitBranches and ExpressionsRecap: Eligibility Rules3Collections and References
Lists Instead of ArraysTuples and UnpackingDictionaries and RecordsSets, Aliasing, and CopiesRecap: Clean an Inventory6Reliable Python Programs
Input and Output ContractsExceptions and ValidationModules and the Main GuardReading Type HintsRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler