π·οΈ Type Hints
Type hints tell us what type of data a variable or function is expected to contain or return.
They make code easier to understand and help tools like IDEs and type checkers detect possible mistakes.
1οΈβ£ Variable Type Hints
You can specify the expected type of a variable using :.
name: str = "Tisha"
age: int = 23
height: float = 5.5
is_student: bool = TrueHere:
name β str
age β int
height β float
is_student β boolπ Collections
You can also specify the types of values inside collections:
names: list[str] = ["Tisha", "Rahul"]
marks: dict[str, int] = {
"Math": 90,
"English": 85
}
numbers: tuple[int, ...] = (1, 2, 3)This tells us:
list[str] β List containing strings
dict[str, int] β Dictionary with string keys and integer values
tuple[int, ...] β Tuple containing integers2οΈβ£ Function Type Hints
Type hints can be added to parameters and the return value of a function.
def add(a: int, b: int) -> int:
return a + bHere:
a: int β a is expected to be an integer
b: int β b is expected to be an integer
-> int β function is expected to return an integerπΉ Another Example
def greet(name: str) -> str:
return "Hello " + namename: str β Parameter should be a string
-> str β Function returns a stringπΉ Function Returning Nothing
If a function doesn't return a value:
def greet(name: str) -> None:
print("Hello", name)
-> Noneindicates that the function does not return a meaningful value.
β οΈ Important
Type hints do not automatically enforce types.
def add(a: int, b: int) -> int:
return a + b
print(add("Hello", "World"))Python can still execute this and produce:
HelloWorldType hints are mainly guidance for developers, IDEs, and static type-checking tools.
<div class="infographic-pytypehints">
<style>
.infographic-pytypehints {
--ink:#1a1a1a; --ink-soft:#5c5c5c; --paper:#fff; --paper-alt:#f0ece4;
--accent:#2563eb; --accent-soft:#e3ebfd; --line:#ddd6c9;
--err:#c0392b; --err-soft:#fbe9e7; --ok:#1f9d55; --ok-soft:#e3f5ec;
--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:700px; margin:0 auto; font-family:var(--body-font); color:var(--ink); background:var(--paper);
}
.infographic-pytypehints[data-theme="dark"] {
--ink:#e8e6e1; --ink-soft:#9a968c; --paper:#121212; --paper-alt:#1e1e26;
--accent:#5b8def; --accent-soft:#202942; --line:#2c313d;
--err:#e57373; --err-soft:#3a2222; --ok:#6fdc8c; --ok-soft:#16342a;
}
.infographic-pytypehints *,.infographic-pytypehints *::before,.infographic-pytypehints *::after{box-sizing:border-box;}
.infographic-pytypehints .th-wrap{padding:0 clamp(1rem,4vw,2rem) 1.5rem;}
.infographic-pytypehints h1{font-family:var(--display-font);font-weight:600;font-size:clamp(1.4rem,5vw,1.9rem);text-align:center;margin:1.5rem 0 0.25rem;letter-spacing:-0.01em;}
.infographic-pytypehints .th-sub{text-align:center;color:var(--ink-soft);font-size:0.85rem;margin:0 0 1.3rem;}
.infographic-pytypehints .th-tabs{display:flex;justify-content:center;gap:0.35rem;margin-bottom:1.4rem;border-bottom:1px solid var(--line);flex-wrap:wrap;}
.infographic-pytypehints .th-tab-btn{font:inherit;font-family:var(--mono-font);font-size:0.8rem;font-weight:700;background:none;border:none;border-bottom:2.5px solid transparent;color:var(--ink-soft);padding:0.55rem 0.85rem;cursor:pointer;margin-bottom:-1px;}
.infographic-pytypehints .th-tab-btn.is-active{color:var(--accent);border-bottom-color:var(--accent);}
.infographic-pytypehints .th-panel.is-hidden-tab{display:none;}
.infographic-pytypehints .group-title{text-align:center;font-family:var(--mono-font);font-size:0.72rem;font-weight:700;text-transform:uppercase;letter-spacing:0.03em;color:var(--ink-soft);margin:1.1rem 0 0.5rem;}
.infographic-pytypehints .group-title:first-child{margin-top:0;}
.infographic-pytypehints .chip-row{display:flex;flex-wrap:wrap;justify-content:center;gap:0.4rem;margin-bottom:0.5rem;}
.infographic-pytypehints .hint-chip{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);padding:0.4rem 0.7rem;cursor:pointer;border-radius:6px;}
.infographic-pytypehints .hint-chip.is-selected{border-color:var(--accent);color:var(--accent);background:var(--accent-soft);}
.infographic-pytypehints .hint-meaning{text-align:center;font-family:var(--mono-font);font-size:0.85rem;font-weight:700;color:var(--accent);margin:1rem 0 0.3rem;min-height:1.3em;}
.infographic-pytypehints .hint-desc{text-align:center;font-size:0.82rem;color:var(--ink-soft);margin-bottom:0.5rem;}
.infographic-pytypehints .code-block{font-family:var(--mono-font);font-size:0.8rem;line-height:1.75;white-space:pre;background:var(--paper-alt);border:1px solid var(--line);border-radius:8px;padding:0.8rem 1rem;margin-bottom:1rem;}
.infographic-pytypehints .code-block .kw{color:var(--accent);font-weight:700;}
.infographic-pytypehints .code-block .hint{color:#b45309;font-weight:700;}
.infographic-pytypehints[data-theme="dark"] .code-block .hint{color:#e0a458;}
.infographic-pytypehints .row{display:flex;justify-content:center;gap:0.5rem;margin-bottom:0.9rem;flex-wrap:wrap;align-items:center;}
.infographic-pytypehints .row span{font-family:var(--mono-font);font-size:0.82rem;color:var(--ink-soft);}
.infographic-pytypehints .row input{font-family:var(--mono-font);font-size:0.85rem;padding:0.4rem 0.6rem;border:1.5px solid var(--line);border-radius:5px;background:var(--paper-alt);color:var(--ink);width:110px;text-align:center;}
.infographic-pytypehints .row input:focus{outline:none;border-color:var(--accent);}
.infographic-pytypehints .preset-row{display:flex;justify-content:center;gap:0.4rem;margin-bottom:0.9rem;flex-wrap:wrap;}
.infographic-pytypehints .preset-btn{font:inherit;font-family:var(--mono-font);font-size:0.74rem;font-weight:700;background:var(--paper-alt);border:1.5px dashed var(--line);color:var(--ink-soft);padding:0.3rem 0.6rem;cursor:pointer;border-radius:999px;}
.infographic-pytypehints .btn{font:inherit;font-family:var(--mono-font);font-size:0.85rem;font-weight:700;background:var(--accent);color:#fff;border:none;border-radius:999px;padding:0.55rem 1.2rem;cursor:pointer;}
.infographic-pytypehints .result-box{text-align:center;font-family:var(--mono-font);font-size:0.9rem;font-weight:700;padding:0.6rem 0.9rem;border-radius:8px;border:1.5px solid var(--line);background:var(--paper-alt);color:var(--ink-soft);margin-bottom:0.6rem;min-height:1.4em;}
.infographic-pytypehints .result-box.ok-r{border-color:var(--ok);background:var(--ok-soft);color:var(--ok);}
.infographic-pytypehints .result-box.err-r{border-color:var(--err);background:var(--err-soft);color:var(--err);}
.infographic-pytypehints .th-caption{text-align:center;font-size:0.85rem;color:var(--ink-soft);margin-top:0.6rem;}
@media (prefers-reduced-motion:reduce){.infographic-pytypehints *,.infographic-pytypehints *::before,.infographic-pytypehints *::after{animation-duration:.001ms!important;transition-duration:.001ms!important;}}
</style>
<div class="th-wrap">
<h1>Type Hints</h1>
<p class="th-sub">Labels for what a variable or function is expected to hold β guidance, not a guarantee.</p>
<div class="th-tabs">
<button class="th-tab-btn is-active" data-tab="ref">Hint Types</button>
<button class="th-tab-btn" data-tab="enforce">Hints Aren't Enforced</button>
</div>
<!-- REFERENCE TAB -->
<div class="th-panel" id="tabRef">
<div class="group-title">Variable Hints</div>
<div class="chip-row" id="varChips"></div>
<div class="group-title">Collection Hints</div>
<div class="chip-row" id="colChips"></div>
<div class="group-title">Function Hints</div>
<div class="chip-row" id="fnChips"></div>
<p class="hint-meaning" id="hintMeaning">name: str = "Tisha"</p>
<p class="hint-desc" id="hintDesc">name is expected to hold a string.</p>
</div>
<!-- ENFORCE TAB -->
<div class="th-panel is-hidden-tab" id="tabEnforce">
<div class="code-block"><span class="kw">def</span> add(a: <span class="hint">int</span>, b: <span class="hint">int</span>) -> <span class="hint">int</span>:
<span class="kw">return</span> a + b</div>
<div class="row">
<span>add(</span><input id="aInput" type="text" value="10" /><span>,</span><input id="bInput" type="text" value="5" /><span>)</span>
</div>
<div class="preset-row">
<button class="preset-btn" data-a="10" data-b="5">10, 5 (matches hints)</button>
<button class="preset-btn" data-a="Hello" data-b="World">"Hello", "World" (wrong type, still runs)</button>
<button class="preset-btn" data-a="10" data-b="World">10, "World" (wrong type, actually errors)</button>
</div>
<div class="row"><button class="btn" id="runBtn">Run: add(a, b)</button></div>
<p class="result-box" id="thResult">Pick a preset or type your own, then hit Run.</p>
<p class="th-caption">Python only ever checks whether + works for the actual values β it never once looks at "int" in the hint.</p>
</div>
</div>
<script>
(function(){
var root = document.currentScript.closest('.infographic-pytypehints') || document.querySelector('.infographic-pytypehints');
var bodyAttr = document.body.getAttribute('data-darkmode');
var dark = bodyAttr === 'true' ? true : bodyAttr === 'false' ? false : (document.cookie.match(/(?:^|;\s*)darkMode=([^;]+)/) || [])[1] === 'true';
if (dark) root.setAttribute('data-theme', 'dark');
var tabBtns = root.querySelectorAll('.th-tab-btn');
var panels = { ref: root.querySelector('#tabRef'), enforce: root.querySelector('#tabEnforce') };
tabBtns.forEach(function (btn) {
btn.addEventListener('click', function () {
tabBtns.forEach(function (b) { b.classList.toggle('is-active', b === btn); });
var t = btn.dataset.tab;
Object.keys(panels).forEach(function (k) { panels[k].classList.toggle('is-hidden-tab', k !== t); });
});
});
/* ---------------- REFERENCE ---------------- */
var hints = [
{ group: 'var', code: 'name: str = "Tisha"', meaning: 'name β str', desc: 'name is expected to hold a string.' },
{ group: 'var', code: 'age: int = 23', meaning: 'age β int', desc: 'age is expected to hold a whole number.' },
{ group: 'var', code: 'height: float = 5.5', meaning: 'height β float', desc: 'height is expected to hold a decimal number.' },
{ group: 'var', code: 'is_student: bool = True', meaning: 'is_student β bool', desc: 'is_student is expected to hold True or False.' },
{ group: 'col', code: 'names: list[str]', meaning: 'list[str]', desc: 'A list containing strings.' },
{ group: 'col', code: 'marks: dict[str, int]', meaning: 'dict[str, int]', desc: 'A dict with string keys and integer values.' },
{ group: 'col', code: 'numbers: tuple[int, ...]', meaning: 'tuple[int, ...]', desc: 'A tuple containing any number of integers.' },
{ group: 'fn', code: 'def add(a: int, b: int) -> int', meaning: 'a: int, b: int, -> int', desc: 'Both parameters and the return value are expected to be integers.' },
{ group: 'fn', code: 'def greet(name: str) -> str', meaning: 'name: str, -> str', desc: 'Takes a string, returns a string.' },
{ group: 'fn', code: 'def greet(name: str) -> None', meaning: '-> None', desc: 'This function isn\'t expected to return a meaningful value.' }
];
var groupContainers = { var: root.querySelector('#varChips'), col: root.querySelector('#colChips'), fn: root.querySelector('#fnChips') };
var hintMeaning = root.querySelector('#hintMeaning');
var hintDesc = root.querySelector('#hintDesc');
hints.forEach(function (h, idx) {
var btn = document.createElement('button');
btn.className = 'hint-chip' + (idx === 0 ? ' is-selected' : '');
btn.textContent = h.code;
btn.addEventListener('click', function () {
root.querySelectorAll('.hint-chip').forEach(function (b) { b.classList.remove('is-selected'); });
btn.classList.add('is-selected');
hintMeaning.textContent = h.meaning;
hintDesc.textContent = h.desc;
});
groupContainers[h.group].appendChild(btn);
});
/* ---------------- ENFORCEMENT DEMO ---------------- */
var aInput = root.querySelector('#aInput');
var bInput = root.querySelector('#bInput');
var runBtn = root.querySelector('#runBtn');
var resultEl = root.querySelector('#thResult');
root.querySelectorAll('#tabEnforce .preset-btn').forEach(function (btn) {
btn.addEventListener('click', function () {
aInput.value = btn.dataset.a;
bInput.value = btn.dataset.b;
});
});
function classify(raw) {
return /^-?\d+(\.\d+)?$/.test(raw.trim()) ? 'num' : 'str';
}
runBtn.addEventListener('click', function () {
var aRaw = aInput.value, bRaw = bInput.value;
var aType = classify(aRaw), bType = classify(bRaw);
var call = 'add(' + (aType === 'num' ? aRaw : '"' + aRaw + '"') + ', ' + (bType === 'num' ? bRaw : '"' + bRaw + '"') + ')';
if (aType === bType && aType === 'num') {
var sum = parseFloat(aRaw) + parseFloat(bRaw);
resultEl.textContent = call + ' β ' + sum + ' (both were numbers β this matches the hints too)';
resultEl.className = 'result-box ok-r';
} else if (aType === bType && aType === 'str') {
resultEl.textContent = call + ' β "' + aRaw + bRaw + '" (concatenated β runs fine even though the hints say int)';
resultEl.className = 'result-box ok-r';
} else {
resultEl.textContent = call + ' β TypeError: unsupported operand type(s) for +: \'' + (aType === 'num' ? 'int' : 'str') + '\' and \'' + (bType === 'num' ? 'int' : 'str') + '\'';
resultEl.className = 'result-box err-r';
}
});
})();
</script>
</div>