== and equal?
Lesson 8 of 48 in Coddy's Python for Ruby Developers course.
Ruby == asks for equality and equal? asks whether two references identify the same object. Python uses == and is for those roles. Both Ruby arrays and Python lists compare their contents by value. Assigning a second name to a list or array shares identity; creating another literal with the same elements does not. Use Python == for string contents and is None for absence. Do not infer identity from equal small integers or strings, since implementation caching can make an incorrect identity comparison seem to work. Custom classes can define their own equality, which a later lesson covers.
Ruby
a = [4]
b = [4]
puts(a == b)
puts a.equal?(b)Python
a = [4]
b = [4]
print(a == b)
print(a is b)Porting trap
Ruby eql? also participates in hash-key semantics; it is not the name of Python's identity operator. Keep value comparison and identity separate.
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 Ruby Developers
1From Ruby to Python
Run Your First Python ProgramStrings and InterpolationNames and Mutable StringsDivision and ParsingFrom end to IndentationRecap: Rewrite a Receipt4Iteration and Transformations
each and Python foreach_pair and enumeratenext Becomes continueselect, map, and sort_byRecap: Build a Ranking2Values and Decisions
nil Becomes None== and equal?Zero Is False in Pythonelsif and Conditional ValuesRecap: Eligibility Rules3Collections and References
Array Methods and ListsParallel Assignment and TuplesHash Defaults and Dict Keysdup, Copies, and SetsRecap: Clean an Inventory6Reliable Python Programs
gets, input, and Formattingrescue and Context Managersrequire and Import GuardsHints Do Not Enforce TypesRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler