Menu
CoddyTech

Strict Equality Is Not is

Lesson 8 of 48 in Coddy's Python for PHP Developers course.

PHP === requires matching types and values for scalars and arrays, but tests identity for objects. Python separates value equality == from identity is; there is no === operator. Python does not convert a numeric string to an integer for equality. However, 1 == True is true, so Python equality is not a universal replacement for PHP strict comparison. Use is for None, not ordinary string or integer values.

PHP 8.3

<?php
$a = [4];
$b = [4];
echo $a === $b ? 'yes' : 'no', "\n";

Python

a = [4]
b = [4]
print(a == b)
print(a is b)

Porting trap

Two separately created Python lists can compare equal without being the same object. Porting every PHP === to Python is is wrong.

Complete the task in the editable Python files. Keep the required function and class names; the locked driver.py supplies execution.

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

Practice on your own: Online Python compiler