Menu
CoddyTech

Values, Addresses, Identity

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

C++ vector equality compares elements, while comparing the addresses of two vector objects asks whether they are the same object. Python == also asks for equality defined by the operand types; is asks for object identity. Separately constructed lists with equal elements are equal but not identical. Python assignment shares an existing list rather than invoking a C++ copy constructor.

C++17 excerpt: inside main

Standard-library headers are supplied in the runnable companion program.

std::vector<int> a{4};
auto b = a;
std::cout << std::boolalpha << (a == b) << '\n' << (&a == &b) << '\n';

Python

a = ["red"]
b = ["red"]
print(a == b)
print(a is b)

Porting trap

Use == for string content and is None for absence. Integer caching or string interning can hide mistakes when is is used for ordinary value comparisons.

Implement the task below in its editable Python modules. The locked driver.py supplies execution and checks. Keep the specified imports and API names.

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 C++ Developers

Practice on your own: Online Python compiler