✅ What are Truthy and Falsy Values?
In Python, every value can be evaluated as either True or False when used in a condition.
Truthy → Values that behave like
TrueFalsy → Values that behave like
False
🟢 Truthy Values
Most values in Python are Truthy.
if "Hello":
print("This will execute")Other examples:
True
10
-5
"Python"
[1, 2, 3]
{"name": "Tisha"}🔴 Falsy Values
The following are commonly Falsy:
False
None
0
0.0
""
[]
()
{}
set()Example:
name = ""
if name:
print("Name exists")
else:
print("Name is empty") # This block will be executed🔍 Checking Truthy/Falsy with bool()
The bool() function converts a value into True or False.
print(bool(10)) # True
print(bool(0)) # False
print(bool("Hello")) # True
print(bool("")) # False
print(bool([1, 2])) # True
print(bool([])) # False💡 Common Practical Use
Instead of writing:
name = ""
if name != "":
print("Name entered")You can simply write:
if name:
print("Name entered")This works because an empty string "" is Falsy.
<div class="infographic-pytruthy">
<style>
.infographic-pytruthy {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--line: #ddd6c9;
--true-c: #1f9d55; --true-soft: #e3f5ec;
--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: 780px;
margin: 0 auto;
font-family: var(--body-font);
color: var(--ink);
background: var(--paper);
}
.infographic-pytruthy[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-pytruthy *, .infographic-pytruthy *::before, .infographic-pytruthy *::after { box-sizing: border-box; }
.infographic-pytruthy .pt-wrap { padding: 0 clamp(1rem, 4vw, 2rem) 1.5rem; }
.infographic-pytruthy h1 {
font-family: var(--display-font); font-weight: 600;
font-size: clamp(1.5rem, 5vw, 2.05rem); text-align: center; margin: 1.5rem 0 0.25rem; letter-spacing: -0.01em;
}
.infographic-pytruthy .pt-sub { text-align: center; color: var(--ink-soft); font-size: 0.86rem; margin: 0 0 1.5rem; }
.infographic-pytruthy .tester-row { display: flex; justify-content: center; align-items: center; gap: 0.5rem; margin-bottom: 0.9rem; flex-wrap: wrap; }
.infographic-pytruthy .tester-row span { font-family: var(--mono-font); font-size: 1.05rem; color: var(--ink-soft); }
.infographic-pytruthy .tester-input {
font-family: var(--mono-font); font-size: 1.05rem; font-weight: 700; text-align: center;
padding: 0.5rem 0.7rem; border: 2px solid var(--line); border-radius: 8px;
background: var(--paper-alt); color: var(--ink); width: 200px;
}
.infographic-pytruthy .tester-input:focus { outline: none; border-color: var(--accent); }
.infographic-pytruthy .result-row { display: flex; justify-content: center; margin-bottom: 0.3rem; }
.infographic-pytruthy .result-badge {
display: inline-flex; align-items: center; gap: 0.5rem;
font-family: var(--display-font); font-size: 1.5rem; font-weight: 600;
padding: 0.45rem 1.2rem; border-radius: 999px; border: 2px solid transparent;
transition: background 0.2s, color 0.2s, border-color 0.2s;
}
.infographic-pytruthy .result-badge.is-true { background: var(--true-soft); color: var(--true-c); border-color: var(--true-c); }
.infographic-pytruthy .result-badge.is-false { background: var(--false-soft); color: var(--false-c); border-color: var(--false-c); }
.infographic-pytruthy .result-badge .badge-icon { width: 20px; height: 20px; }
.infographic-pytruthy .result-badge.is-pulsing { animation: pt-pop 0.3s ease; }
@keyframes pt-pop { 0% { transform: scale(1); } 45% { transform: scale(1.1); } 100% { transform: scale(1); } }
.infographic-pytruthy .result-type { text-align: center; font-family: var(--mono-font); font-size: 0.75rem; color: var(--ink-soft); margin-bottom: 1.4rem; }
.infographic-pytruthy .gallery-title {
text-align: center; font-size: 0.72rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.03em;
color: var(--ink-soft); margin-bottom: 0.6rem;
}
.infographic-pytruthy .gallery-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.8rem; margin-bottom: 1.3rem; }
@media (max-width: 560px) { .infographic-pytruthy .gallery-grid { grid-template-columns: 1fr; } }
.infographic-pytruthy .gallery-col {
border-radius: 10px; padding: 0.7rem 0.7rem 0.8rem; border: 1.5px solid;
}
.infographic-pytruthy .gallery-col.falsy-col { border-color: var(--false-c); background: var(--false-soft); }
.infographic-pytruthy .gallery-col.truthy-col { border-color: var(--true-c); background: var(--true-soft); }
.infographic-pytruthy .gallery-col-title { font-family: var(--mono-font); font-size: 0.78rem; font-weight: 700; text-align: center; margin-bottom: 0.55rem; }
.infographic-pytruthy .gallery-col.falsy-col .gallery-col-title { color: var(--false-c); }
.infographic-pytruthy .gallery-col.truthy-col .gallery-col-title { color: var(--true-c); }
.infographic-pytruthy .gallery-chips { display: flex; flex-wrap: wrap; gap: 0.35rem; justify-content: center; }
.infographic-pytruthy .gallery-chip {
font: inherit; font-family: var(--mono-font); font-size: 0.78rem; font-weight: 700;
background: var(--paper); border: 1.5px solid var(--line); color: var(--ink);
padding: 0.3rem 0.55rem; cursor: pointer; border-radius: 6px;
}
.infographic-pytruthy .gallery-chip:hover { border-color: var(--accent); color: var(--accent); }
.infographic-pytruthy .gotcha-title {
text-align: center; font-size: 0.72rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.03em;
color: var(--ink-soft); margin: 1.2rem 0 0.6rem;
}
.infographic-pytruthy .gotcha-row { display: flex; flex-wrap: wrap; justify-content: center; gap: 0.4rem; margin-bottom: 0.5rem; }
.infographic-pytruthy .gotcha-chip {
font: inherit; font-family: var(--mono-font); font-size: 0.78rem; font-weight: 700;
background: var(--accent-soft); border: 1.5px solid var(--accent); color: var(--accent);
padding: 0.3rem 0.6rem; cursor: pointer; border-radius: 999px;
}
.infographic-pytruthy .pt-note { text-align: center; font-size: 0.8rem; color: var(--ink-soft); margin-top: 0.6rem; }
@media (prefers-reduced-motion: reduce) {
.infographic-pytruthy *, .infographic-pytruthy *::before, .infographic-pytruthy *::after {
animation-duration: 0.001ms !important; transition-duration: 0.001ms !important;
}
}
</style>
<div class="pt-wrap">
<h1>Truthy & Falsy Values</h1>
<p class="pt-sub">Every value in Python is secretly True or False when tested. Tap a chip, or type your own.</p>
<div class="tester-row">
<span>bool(</span>
<input class="tester-input" id="testerInput" type="text" value="0" spellcheck="false" />
<span>)</span>
</div>
<div class="result-row">
<div class="result-badge is-false" id="resultBadge">
<svg class="badge-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" id="resultIcon"><line x1="5" y1="5" x2="19" y2="19"/><line x1="19" y1="5" x2="5" y2="19"/></svg>
<span id="resultText">False</span>
</div>
</div>
<p class="result-type" id="resultType">int, equal to zero</p>
<div class="gallery-grid">
<div class="gallery-col falsy-col">
<div class="gallery-col-title">Falsy — bool() is False</div>
<div class="gallery-chips" id="falsyChips"></div>
</div>
<div class="gallery-col truthy-col">
<div class="gallery-col-title">Truthy — bool() is True</div>
<div class="gallery-chips" id="truthyChips"></div>
</div>
</div>
<div class="gotcha-title">Gotchas — click to test</div>
<div class="gotcha-row" id="gotchaChips"></div>
<p class="pt-note">A non-empty string is always truthy — even "False", even a single space.</p>
</div>
<script>
(function () {
var root = document.currentScript.closest('.infographic-pytruthy')
|| document.querySelector('.infographic-pytruthy');
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 testerInput = root.querySelector('#testerInput');
var resultBadge = root.querySelector('#resultBadge');
var resultIcon = root.querySelector('#resultIcon');
var resultText = root.querySelector('#resultText');
var resultType = root.querySelector('#resultType');
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"/>';
function parseLiteral(raw) {
if (raw === 'None') return { kind: 'NoneType', falsy: true, desc: 'None — always falsy' };
if (raw === 'True') return { kind: 'bool', falsy: false, desc: 'the boolean True' };
if (raw === 'False') return { kind: 'bool', falsy: true, desc: 'the boolean False' };
if (/^-?\d+$/.test(raw)) {
var n = parseInt(raw, 10);
return { kind: 'int', falsy: n === 0, desc: 'int, ' + (n === 0 ? 'equal to zero' : 'nonzero') };
}
if (/^-?\d*\.\d+$/.test(raw)) {
var f = parseFloat(raw);
return { kind: 'float', falsy: f === 0, desc: 'float, ' + (f === 0 ? 'equal to zero' : 'nonzero') };
}
var strMatch = raw.match(/^"(.*)"$/) || raw.match(/^'(.*)'$/);
if (strMatch) {
var s = strMatch[1];
return { kind: 'str', falsy: s.length === 0, desc: 'str, ' + (s.length === 0 ? 'empty' : 'length ' + s.length) };
}
if (raw === 'set()') return { kind: 'set', falsy: true, desc: 'empty set' };
if (raw === '[]') return { kind: 'list', falsy: true, desc: 'empty list' };
if (/^\[.*\]$/.test(raw) && raw.length > 2) return { kind: 'list', falsy: false, desc: 'non-empty list' };
if (raw === '()') return { kind: 'tuple', falsy: true, desc: 'empty tuple' };
if (/^\(.*\)$/.test(raw) && raw.length > 2) return { kind: 'tuple', falsy: false, desc: 'non-empty tuple' };
if (raw === '{}') return { kind: 'dict', falsy: true, desc: 'empty dict' };
if (/^\{.*\}$/.test(raw) && raw.length > 2) return { kind: 'dict/set', falsy: false, desc: 'non-empty dict/set' };
// bare text, no quotes: treat as a string literal
return { kind: 'str', falsy: raw.length === 0, desc: raw.length === 0 ? 'empty string' : ('str, length ' + raw.length + ' — not quoted, still a string') };
}
function recompute() {
var raw = testerInput.value;
var result = parseLiteral(raw);
var isTrue = !result.falsy;
resultBadge.classList.remove('is-true', 'is-false');
resultBadge.classList.add(isTrue ? 'is-true' : 'is-false');
resultText.textContent = isTrue ? 'True' : 'False';
resultIcon.innerHTML = isTrue ? checkIcon : crossIcon;
resultBadge.classList.remove('is-pulsing');
void resultBadge.offsetWidth;
resultBadge.classList.add('is-pulsing');
resultType.textContent = result.desc + ' · type: ' + result.kind;
}
testerInput.addEventListener('input', recompute);
function makeChip(container, label, value) {
var chip = document.createElement('button');
chip.className = container.dataset.style === 'gotcha' ? 'gotcha-chip' : 'gallery-chip';
chip.textContent = label;
chip.addEventListener('click', function () {
testerInput.value = value;
recompute();
});
container.appendChild(chip);
}
var falsyChips = root.querySelector('#falsyChips');
var truthyChips = root.querySelector('#truthyChips');
var gotchaChips = root.querySelector('#gotchaChips');
gotchaChips.dataset.style = 'gotcha';
[
['0', '0'], ['0.0', '0.0'], ['""', '""'], ['[]', '[]'],
['()', '()'], ['{}', '{}'], ['set()', 'set()'], ['None', 'None'], ['False', 'False']
].forEach(function (pair) { makeChip(falsyChips, pair[0], pair[1]); });
[
['1', '1'], ['-1', '-1'], ['"hi"', '"hi"'], ['[0]', '[0]'],
['(0,)', '(0,)'], ['{0: 0}', '{0: 0}'], ['True', 'True'], ['3.14', '3.14']
].forEach(function (pair) { makeChip(truthyChips, pair[0], pair[1]); });
[
['"False"', '"False"'], ['" "', '" "'], ['[0]', '[0]'],
['"0"', '"0"'], ['[None]', '[None]'], ['{0: False}', '{0: False}']
].forEach(function (pair) { makeChip(gotchaChips, pair[0], pair[1]); });
recompute();
})();
</script>
</div>