✅ Identity Operators
These operators compare memory locations — not just the values.
Operator | Meaning | Returns |
|---|---|---|
| Same object | Both variables point to the same memory location |
| Not same object | They point to different memory locations |
💡 Example:
a = [1, 2] # a is a list
b = a # b refers to the same list as a
print(a is b) # ✅ True – both a and b point to the same object
print(a is not b) # ❌ False – they are not different objects✅ Membership Operators
These operators are used to check if a value is present in a sequence like a list, string, tuple, etc.
Operator | Meaning | Returns |
|---|---|---|
| Value exists | The value is inside the sequence |
| Value doesn’t exist | The value is not inside the sequence |
💡 Example:
colors = ["red", "green"]
print("red" in colors) # ✅ True – "red" is in the list
print("blue" not in colors) # ✅ True – "blue" is not in the list<div class="infographic-pyidmem">
<style>
.infographic-pyidmem {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--line: #ddd6c9;
--true-c: #1f9d55;
--true-soft: #e3f6ea;
--false-c: #c0392b;
--false-soft: #fbe9e7;
--mono-font: 'IBM Plex Mono', 'SFMono-Regular', Menlo, monospace;
--display-font: 'Fraunces', Georgia, serif;
--body-font: 'Inter', system-ui, sans-serif;
box-sizing: border-box;
max-width: 760px;
margin: 0 auto;
font-family: var(--body-font);
color: var(--ink);
background: var(--paper);
}
.infographic-pyidmem[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #121212;
--paper-alt: #1e1e26;
--accent: #5b8def;
--accent-soft:#202942;
--line: #2c313d;
--true-c: #6fdc8c;
--true-soft: #1c3324;
--false-c: #e57373;
--false-soft: #3a2222;
}
.infographic-pyidmem *, .infographic-pyidmem *::before, .infographic-pyidmem *::after { box-sizing: border-box; }
.infographic-pyidmem .im-wrap { padding: 0 clamp(1rem, 4vw, 2rem) 1.5rem; }
.infographic-pyidmem h1 {
font-family: var(--display-font); font-weight: 600;
font-size: clamp(1.55rem, 5vw, 2.1rem); text-align: center; margin: 1.5rem 0 0.25rem; letter-spacing: -0.01em;
}
.infographic-pyidmem .im-sub { text-align: center; color: var(--ink-soft); font-size: 0.88rem; margin: 0 0 1.6rem; }
.infographic-pyidmem .im-section { margin-bottom: 2rem; }
.infographic-pyidmem .im-section.is-hidden-tab { display: none; }
.infographic-pyidmem .im-tabs { display: flex; justify-content: center; gap: 0.5rem; margin-bottom: 1.6rem; border-bottom: 1px solid var(--line); }
.infographic-pyidmem .im-tab-btn {
font: inherit; font-family: var(--mono-font); font-size: 0.88rem; font-weight: 700;
background: none; border: none; border-bottom: 2.5px solid transparent; color: var(--ink-soft);
padding: 0.6rem 1rem; cursor: pointer; margin-bottom: -1px;
}
.infographic-pyidmem .im-tab-btn.is-active { color: var(--accent); border-bottom-color: var(--accent); }
.infographic-pyidmem .im-section-head { display: flex; align-items: center; gap: 0.6rem; margin-bottom: 0.9rem; justify-content: center; }
.infographic-pyidmem .im-badge-num {
font-family: var(--display-font); font-size: 1.05rem; color: var(--accent);
width: 28px; height: 28px; border: 1.5px solid var(--accent); border-radius: 50%;
display: flex; align-items: center; justify-content: center; flex: none;
}
.infographic-pyidmem .im-section-title { font-weight: 700; font-size: 1rem; }
.infographic-pyidmem .im-section-tag { font-size: 0.72rem; color: var(--ink-soft); display: block; }
/* ---- identity section ---- */
.infographic-pyidmem .id-mode-row { display: flex; justify-content: center; gap: 0.5rem; margin-bottom: 1.1rem; }
.infographic-pyidmem .id-mode-btn {
font: inherit; font-family: var(--mono-font); font-size: 0.78rem; font-weight: 700;
background: var(--paper-alt); border: 1.5px solid var(--line); color: var(--ink-soft);
padding: 0.45rem 0.8rem; cursor: pointer; border-radius: 5px;
}
.infographic-pyidmem .id-mode-btn.is-selected { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.infographic-pyidmem .id-value-row { display: flex; justify-content: center; margin-bottom: 1rem; }
.infographic-pyidmem .id-value-input {
font-family: var(--mono-font); font-size: 0.9rem; text-align: center;
padding: 0.4rem 0.6rem; border: 1.5px solid var(--line); background: var(--paper-alt); color: var(--ink);
border-radius: 5px; width: 200px;
}
.infographic-pyidmem .id-value-input:focus { outline: none; border-color: var(--accent); }
.infographic-pyidmem .id-diagram { display: flex; justify-content: center; gap: 3rem; margin-bottom: 0.7rem; min-height: 130px; align-items: flex-start; }
.infographic-pyidmem .id-pointer-col { display: flex; flex-direction: column; align-items: center; }
.infographic-pyidmem .id-var-label {
font-family: var(--display-font); font-size: 1.1rem; font-weight: 600; color: var(--ink);
width: 34px; height: 34px; border: 1.5px solid var(--ink-soft); border-radius: 50%;
display: flex; align-items: center; justify-content: center; margin-bottom: 0.3rem;
}
.infographic-pyidmem .id-arrow { color: var(--ink-soft); font-size: 1.1rem; line-height: 1; }
.infographic-pyidmem .id-box {
margin-top: 0.3rem; background: var(--accent-soft); border: 2px solid var(--accent); border-radius: 6px;
padding: 0.6rem 0.9rem; font-family: var(--mono-font); font-size: 0.85rem; color: var(--ink);
text-align: center; transition: transform 0.2s;
}
.infographic-pyidmem .id-addr { font-size: 0.65rem; color: var(--ink-soft); margin-top: 0.25rem; text-align: center; }
.infographic-pyidmem .id-shared-wrap { display: flex; flex-direction: column; align-items: center; }
.infographic-pyidmem .id-shared-arrows { display: flex; gap: 1.6rem; margin-bottom: -0.1rem; }
.infographic-pyidmem .id-op-row { display: flex; justify-content: center; gap: 0.5rem; margin-bottom: 1rem; }
.infographic-pyidmem .id-op-btn {
font: inherit; font-family: var(--mono-font); font-size: 0.85rem; font-weight: 700;
background: var(--paper-alt); border: 1.5px solid var(--line); color: var(--ink-soft);
padding: 0.5rem 0.9rem; cursor: pointer; border-radius: 5px;
}
.infographic-pyidmem .id-op-btn.is-selected { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
/* ---- membership section ---- */
.infographic-pyidmem .mem-inputs { display: flex; justify-content: center; gap: 0.8rem; margin-bottom: 1rem; flex-wrap: wrap; }
.infographic-pyidmem .mem-input-slot { display: flex; flex-direction: column; align-items: center; gap: 0.3rem; }
.infographic-pyidmem .mem-input-label { font-size: 0.7rem; color: var(--ink-soft); font-weight: 600; font-family: var(--mono-font); }
.infographic-pyidmem .mem-list-input {
font-family: var(--mono-font); font-size: 0.85rem; text-align: center;
padding: 0.4rem 0.5rem; border: 1.5px solid var(--line); background: var(--paper-alt); color: var(--ink);
border-radius: 5px; width: 220px;
}
.infographic-pyidmem .mem-needle-input {
font-family: var(--mono-font); font-size: 0.9rem; text-align: center;
padding: 0.4rem 0.5rem; border: 1.5px solid var(--line); background: var(--paper-alt); color: var(--ink);
border-radius: 5px; width: 110px;
}
.infographic-pyidmem .mem-list-input:focus, .infographic-pyidmem .mem-needle-input:focus { outline: none; border-color: var(--accent); }
.infographic-pyidmem .mem-chip-row { display: flex; flex-wrap: wrap; justify-content: center; gap: 0.4rem; margin-bottom: 1rem; min-height: 40px; align-items: center; }
.infographic-pyidmem .mem-chip {
font-family: var(--mono-font); font-size: 0.82rem; font-weight: 600;
padding: 0.35rem 0.65rem; border-radius: 5px; border: 1.5px solid var(--line);
background: var(--paper-alt); color: var(--ink-soft); transition: background 0.25s, border-color 0.25s, color 0.25s, transform 0.2s;
}
.infographic-pyidmem .mem-chip.is-match { background: var(--accent); border-color: var(--accent); color: #fff; transform: scale(1.08); }
.infographic-pyidmem .mem-op-row { display: flex; justify-content: center; gap: 0.5rem; margin-bottom: 1rem; }
.infographic-pyidmem .mem-op-btn {
font: inherit; font-family: var(--mono-font); font-size: 0.85rem; font-weight: 700;
background: var(--paper-alt); border: 1.5px solid var(--line); color: var(--ink-soft);
padding: 0.5rem 0.9rem; cursor: pointer; border-radius: 5px;
}
.infographic-pyidmem .mem-op-btn.is-selected { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
/* ---- shared result badge ---- */
.infographic-pyidmem .im-equation { text-align: center; font-family: var(--mono-font); font-size: 1rem; font-weight: 700; margin-bottom: 0.7rem; }
.infographic-pyidmem .im-result-row { display: flex; justify-content: center; margin-bottom: 0.3rem; }
.infographic-pyidmem .im-result-badge {
display: inline-flex; align-items: center; gap: 0.5rem;
font-family: var(--display-font); font-size: 1.4rem; font-weight: 600;
padding: 0.4rem 1.1rem; border-radius: 999px; border: 2px solid transparent;
transition: background 0.25s, color 0.25s, border-color 0.25s;
}
.infographic-pyidmem .im-result-badge.is-true { background: var(--true-soft); color: var(--true-c); border-color: var(--true-c); }
.infographic-pyidmem .im-result-badge.is-false { background: var(--false-soft); color: var(--false-c); border-color: var(--false-c); }
.infographic-pyidmem .im-result-badge .badge-icon { width: 20px; height: 20px; }
.infographic-pyidmem .im-result-badge.is-pulsing { animation: im-pop 0.3s ease; }
@keyframes im-pop { 0% { transform: scale(1); } 45% { transform: scale(1.1); } 100% { transform: scale(1); } }
.infographic-pyidmem .im-caption { text-align: center; font-size: 0.8rem; color: var(--ink-soft); }
@media (prefers-reduced-motion: reduce) {
.infographic-pyidmem *, .infographic-pyidmem *::before, .infographic-pyidmem *::after {
animation-duration: 0.001ms !important; transition-duration: 0.001ms !important;
}
}
</style>
<div class="im-wrap">
<h1>Identity & Membership</h1>
<p class="im-sub">Same object, or just equal values? Is it in there, or not?</p>
<div class="im-tabs">
<button class="im-tab-btn is-active" data-tab="identity">Identity</button>
<button class="im-tab-btn" data-tab="membership">Membership</button>
</div>
<!-- IDENTITY -->
<div class="im-section" id="tabIdentity">
<div class="im-section-head">
<div class="im-badge-num">1</div>
<div>
<div class="im-section-title">Identity — is / is not</div>
<span class="im-section-tag">Same object in memory, not just equal value</span>
</div>
</div>
<div class="id-value-row">
<input class="id-value-input" id="idValue" type="text" value="[1, 2, 3]" />
</div>
<div class="id-mode-row">
<button class="id-mode-btn is-selected" data-mode="same">b = a</button>
<button class="id-mode-btn" data-mode="copy">b = a.copy()</button>
</div>
<div class="id-diagram" id="idDiagram"></div>
<div class="id-op-row">
<button class="id-op-btn is-selected" data-op="is">is</button>
<button class="id-op-btn" data-op="isnot">is not</button>
</div>
<div class="im-equation" id="idEquation">a is b</div>
<div class="im-result-row">
<div class="im-result-badge is-true" id="idBadge">
<svg class="badge-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" id="idIcon"><polyline points="4,13 9,18 20,6"/></svg>
<span id="idResultText">True</span>
</div>
</div>
<p class="im-caption" id="idCaption"></p>
</div>
<!-- MEMBERSHIP -->
<div class="im-section is-hidden-tab" id="tabMembership">
<div class="im-section-head">
<div class="im-badge-num">2</div>
<div>
<div class="im-section-title">Membership — in / not in</div>
<span class="im-section-tag">Does this value exist in the sequence?</span>
</div>
</div>
<div class="mem-inputs">
<div class="mem-input-slot">
<input class="mem-list-input" id="memList" type="text" value="apple, banana, cherry, mango" />
<span class="mem-input-label">list (comma separated)</span>
</div>
<div class="mem-input-slot">
<input class="mem-needle-input" id="memNeedle" type="text" value="banana" />
<span class="mem-input-label">looking for</span>
</div>
</div>
<div class="mem-chip-row" id="memChipRow"></div>
<div class="mem-op-row">
<button class="mem-op-btn is-selected" data-op="in">in</button>
<button class="mem-op-btn" data-op="notin">not in</button>
</div>
<div class="im-equation" id="memEquation">"banana" in fruits</div>
<div class="im-result-row">
<div class="im-result-badge is-true" id="memBadge">
<svg class="badge-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" id="memIcon"><polyline points="4,13 9,18 20,6"/></svg>
<span id="memResultText">True</span>
</div>
</div>
<p class="im-caption" id="memCaption"></p>
</div>
</div>
<script>
(function () {
var root = document.currentScript.closest('.infographic-pyidmem')
|| document.querySelector('.infographic-pyidmem');
var bodyAttr = document.body.getAttribute('data-darkmode');
var dark;
if (bodyAttr === 'true') { dark = true; }
else if (bodyAttr === 'false') { dark = false; }
else {
var match = document.cookie.match(/(?:^|;\s*)darkMode=([^;]+)/);
dark = match ? match[1] === 'true' : false;
}
if (dark) root.setAttribute('data-theme', 'dark');
var checkIcon = '<polyline points="4,13 9,18 20,6"/>';
var crossIcon = '<line x1="5" y1="5" x2="19" y2="19"/><line x1="19" y1="5" x2="5" y2="19"/>';
/* ---------------- Tabs ---------------- */
var tabBtns = root.querySelectorAll('.im-tab-btn');
var tabPanels = {
identity: root.querySelector('#tabIdentity'),
membership: root.querySelector('#tabMembership')
};
tabBtns.forEach(function (btn) {
btn.addEventListener('click', function () {
tabBtns.forEach(function (b) { b.classList.toggle('is-active', b === btn); });
var target = btn.dataset.tab;
Object.keys(tabPanels).forEach(function (key) {
tabPanels[key].classList.toggle('is-hidden-tab', key !== target);
});
});
});
function setBadge(badgeEl, iconEl, textEl, result) {
badgeEl.classList.remove('is-true', 'is-false');
badgeEl.classList.add(result ? 'is-true' : 'is-false');
textEl.textContent = result ? 'True' : 'False';
iconEl.innerHTML = result ? checkIcon : crossIcon;
badgeEl.classList.remove('is-pulsing');
void badgeEl.offsetWidth;
badgeEl.classList.add('is-pulsing');
}
/* ---------------- Identity ---------------- */
var idValueInput = root.querySelector('#idValue');
var idModeBtns = root.querySelectorAll('.id-mode-btn');
var idOpBtns = root.querySelectorAll('.id-op-btn');
var idDiagram = root.querySelector('#idDiagram');
var idEquation = root.querySelector('#idEquation');
var idBadge = root.querySelector('#idBadge');
var idIcon = root.querySelector('#idIcon');
var idResultText = root.querySelector('#idResultText');
var idCaption = root.querySelector('#idCaption');
var idMode = 'same';
var idOp = 'is';
var ADDR_A = '0x7f3a10a0';
var ADDR_B = '0x7f3a10f8';
function renderIdentityDiagram() {
var value = idValueInput.value.trim() || '[]';
idDiagram.innerHTML = '';
if (idMode === 'same') {
var wrap = document.createElement('div');
wrap.className = 'id-shared-wrap';
wrap.innerHTML =
'<div class="id-shared-arrows">' +
'<div class="id-pointer-col"><div class="id-var-label">a</div><div class="id-arrow">↓</div></div>' +
'<div class="id-pointer-col"><div class="id-var-label">b</div><div class="id-arrow">↓</div></div>' +
'</div>' +
'<div class="id-box">' + value + '</div>' +
'<div class="id-addr">' + ADDR_A + '</div>';
idDiagram.appendChild(wrap);
} else {
var colA = document.createElement('div');
colA.className = 'id-pointer-col';
colA.innerHTML =
'<div class="id-var-label">a</div><div class="id-arrow">↓</div>' +
'<div class="id-box">' + value + '</div>' +
'<div class="id-addr">' + ADDR_A + '</div>';
var colB = document.createElement('div');
colB.className = 'id-pointer-col';
colB.innerHTML =
'<div class="id-var-label">b</div><div class="id-arrow">↓</div>' +
'<div class="id-box">' + value + '</div>' +
'<div class="id-addr">' + ADDR_B + '</div>';
idDiagram.appendChild(colA);
idDiagram.appendChild(colB);
}
}
function recomputeIdentity() {
renderIdentityDiagram();
var sameObject = idMode === 'same';
var result = idOp === 'is' ? sameObject : !sameObject;
idEquation.textContent = 'a ' + (idOp === 'is' ? 'is' : 'is not') + ' b';
setBadge(idBadge, idIcon, idResultText, result);
idCaption.textContent = sameObject
? 'a and b point to the exact same object.'
: 'a and b hold equal values, but are different objects — == would be True here, is is not.';
}
idModeBtns.forEach(function (btn) {
btn.addEventListener('click', function () {
idModeBtns.forEach(function (b) { b.classList.toggle('is-selected', b === btn); });
idMode = btn.dataset.mode;
recomputeIdentity();
});
});
idOpBtns.forEach(function (btn) {
btn.addEventListener('click', function () {
idOpBtns.forEach(function (b) { b.classList.toggle('is-selected', b === btn); });
idOp = btn.dataset.op;
recomputeIdentity();
});
});
idValueInput.addEventListener('input', recomputeIdentity);
/* ---------------- Membership ---------------- */
var memListInput = root.querySelector('#memList');
var memNeedleInput = root.querySelector('#memNeedle');
var memChipRow = root.querySelector('#memChipRow');
var memOpBtns = root.querySelectorAll('.mem-op-btn');
var memEquation = root.querySelector('#memEquation');
var memBadge = root.querySelector('#memBadge');
var memIcon = root.querySelector('#memIcon');
var memResultText = root.querySelector('#memResultText');
var memCaption = root.querySelector('#memCaption');
var memOp = 'in';
function recomputeMembership() {
var items = memListInput.value.split(',').map(function (s) { return s.trim(); }).filter(function (s) { return s.length > 0; });
var needle = memNeedleInput.value.trim();
memChipRow.innerHTML = '';
var found = false;
items.forEach(function (item) {
var chip = document.createElement('span');
var isMatch = item === needle && needle !== '';
if (isMatch) found = true;
chip.className = 'mem-chip' + (isMatch ? ' is-match' : '');
chip.textContent = item;
memChipRow.appendChild(chip);
});
var result = memOp === 'in' ? found : !found;
memEquation.textContent = '"' + needle + '" ' + (memOp === 'in' ? 'in' : 'not in') + ' list';
setBadge(memBadge, memIcon, memResultText, result);
memCaption.textContent = found
? ('"' + needle + '" was found in the list.')
: ('"' + needle + '" is not in the list.');
}
memOpBtns.forEach(function (btn) {
btn.addEventListener('click', function () {
memOpBtns.forEach(function (b) { b.classList.toggle('is-selected', b === btn); });
memOp = btn.dataset.op;
recomputeMembership();
});
});
memListInput.addEventListener('input', recomputeMembership);
memNeedleInput.addEventListener('input', recomputeMembership);
recomputeIdentity();
recomputeMembership();
})();
</script>
</div>