Equality and Identity
Lesson 8 of 48 in Coddy's Python for Kotlin Developers course.
Kotlin == delegates to equality and === tests reference identity. Python == and is are similar in purpose, but Python is is a keyword, not Kotlin's triple equals. Lists in both languages compare contents for equality. Arrays require special attention in Kotlin because their default equality is not list-style element comparison. Compare Python strings by value rather than relying on interning or object caching.
Kotlin
fun main() {
val a = mutableListOf(4)
val b = mutableListOf(4)
println(a == b)
println(a === b)
}Python
a = [4]
b = [4]
print(a == b)
print(a is b)Porting trap
Python equality is not a static type check: 1 == True is true. Identity asks whether two references denote one object, not whether their printed forms match.
Complete the task in its editable modules. The locked driver.py supplies execution; keep the required API names.
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 Kotlin Developers
1From Kotlin to Python
Run Your First Python ProgramPrint and String TemplatesVal, Var, and Python NamesDivision and Integer WidthSuites and Block ScopeRecap: Rewrite a Receipt4Iteration and Transformations
Ranges and Loop BindingsEntries and withIndexUpdates and Loop ControlCollection PipelinesRecap: Build a Ranking2Values and Decisions
Nullable Types and NoneEquality and IdentityBoolean Conditions and orIf Expressions and ElifRecap: Eligibility Rules3Collections and Shared State
Read-only Lists and MutationPair and Tuple UnpackingMap Lookup and Missing KeysShallow Copies and SetsRecap: Clean an Inventory6Input, Errors, and Modules
Console Input and FormattingExceptions and Resource ScopeImports and Entry PointsStatic Types and AnnotationsRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler