Best afterA comparison is a yes/no value·Filtering a list
Palindrome check
A palindrome reads the same forwards and backwards. Checking one means pairing the first letter with the last, the second with the second-to-last, and so on — and it is easy to pair the wrong letters.
Decide whether a word reads the same forwards and backwards.
Palindrome — comparing the wrong pair execution-derived · CPython
variables
state after line 1 runs
| variable | value |
|---|---|
s | "racecar" |
step 1 / 12
Click a line, drag the slider, or use the ← → keys.
What you are looking at
Watch is_palindrome as i walks the first half of the word.
- Buggy — compares each letter with its NEIGHBOUR (
s[i]vss[i + 1]), so 'racecar' fails at the very first pair even though it is a palindrome. - Procedural — compares each letter with its MIRROR (
s[i]vss[len(s) - 1 - i]), so 'racecar' stays True. - Idiomatic —
s == s[::-1]compares the whole word to its reverse in one line.s[::-1]is the Pythonic way to reverse a sequence.
This is a milestone — it puts several earlier ideas together into one small program.
Every state you see came from running the program under CPython's tracer at build time — see how GlassBox stays honest.