Menu
CoddyTech

== 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 icon

Challenge

Beginner

Read 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__")
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Python for Ruby Developers

Practice on your own: Online Python compiler