Assignment operators are used to store values in variables.
You can also use them to update the value of a variable in a shorter way — this is called compound assignment.
Operator | Example | What It Means |
|---|---|---|
|
| Set |
|
| Add 3 to |
|
| Subtract 2 from |
|
| Multiply |
|
| Divide |
|
| Remainder of |
|
| Rounds the result down to the nearest whole number |
|
| Raises a variable to the power of a specified value |
|
| Performs a bitwise AND and updates the variable. |
|
| Performs a bitwise OR and updates the variable. |
|
| Performs a bitwise XOR and updates the variable. |
|
| Shifts bits to the right and updates the variable. |
|
| Shifts bits to the left and updates the variable. |
|
| Assigns a value to a variable inside an expression.Assigns |
💼 Example
= Simple Assingment
x = 5+= Addition Assingment
x = 3
x += 5 # Output: 8-= Subtraction Assingment
x = 9
x -= 6 # Output: 3*= Multiplication Assingment
x = 3
x *= 5 # Output: 15/= Division Assingment
x = 10
x /= 5 # Output: 2%= Modulus Assingment
x = 9
x %= 5 # Output: 4 //= Floor Division Assignment
x = 3
x //= 2 # Output: 1**= Exponentiation Assignment
x = 3
x **= 2 # Output: 9&= Bitwise AND Assignment
x = 3
x &= 0 # Output: 3Convert to binary:
3in binary is00110in binary is0000
Apply Bitwise AND (
&): A bitwise AND outputs a1only if both corresponding bits are1.
0011 (Decimal 3)
& 0000 (Decimal 0)
------
0000 (Decimal 0)|= Bitwise OR Assignment
x = 3
x &= 0 # Output: 3Convert to binary:
3in binary is00110in binary is0000
Apply Bitwise OR (
|): A bitwise OR outputs a1only if any bit is1.
0011 (Decimal 3)
| 0000 (Decimal 0)
------
0011 (Decimal 3)^= Bitwise XOR Assignment
x = 3
x ^= 1 # Output: 2Initial Assignment:
x = 3stores the integer3in memory. In binary,3is represented as011.,1is represented as001Binary Evaluation: XOR returns
1only if the bits are different. XORing any number with0always leaves the original number completely unchanged.
011 (binary for 3)
^ 001 (binary for 1)
------
010 (binary for 2)>>= Right Shift Assignment
x = 8
x >>= 1 # Output: 4Initial Assignment:
x = 8stores the integer8in memory. In binary,8is represented as1000.Binary Evaluation: The
>>operator shifts all the bits to the right by the specified number of positions. Bits that move beyond the right side are discarded, and0s are added on the left for positive numbers.
1000 (binary for 8)
>> 1 (shift right by 1 position)
------
0100 (binary for 4)<<= Left Shift Assignment
x = 3
x <<= 1 # Output: 6Initial Assignment:
x = 3stores the integer3in memory. In binary,3is represented as011.Binary Evaluation: The
<<operator shifts all the bits to the left by the specified number of positions.0s are added on the right.
011 (binary for 3)
<< 1 (shift left by 1 position)
-----
110 (binary for 6):= Walrus Assignment
if (x := 5) > 3:
print(x) # Output: 5Assignment and Evaluation:
x := 5assigns the value5toxand at the same time returns the value5.Condition Evaluation: Since
5 > 3isTrue, theifblock executes.
x := 5
↓
x gets 5
↓
expression also gives 5
↓
5 > 3 → True<div class="infographic-pyassign2">
<style>
.infographic-pyassign2 {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--line: #ddd6c9;
--error: #c0392b;
--error-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-pyassign2[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #121212;
--paper-alt: #1e1e26;
--accent: #5b8def;
--accent-soft:#202942;
--line: #2c313d;
--error: #e57373;
--error-soft: #3a2222;
}
.infographic-pyassign2 *, .infographic-pyassign2 *::before, .infographic-pyassign2 *::after { box-sizing: border-box; }
.infographic-pyassign2 .as-wrap { padding: 0 clamp(1rem, 4vw, 2rem) 1.5rem; }
.infographic-pyassign2 h1 {
font-family: var(--display-font); font-weight: 600;
font-size: clamp(1.5rem, 5vw, 2.1rem); text-align: center; margin: 1.5rem 0 0.25rem; letter-spacing: -0.01em;
}
.infographic-pyassign2 .as-sub { text-align: center; color: var(--ink-soft); font-size: 0.88rem; margin: 0 0 1.4rem; }
.infographic-pyassign2 .as-inputs { display: flex; align-items: center; justify-content: center; gap: 0.9rem; margin-bottom: 1.2rem; }
.infographic-pyassign2 .as-input-slot { display: flex; flex-direction: column; align-items: center; gap: 0.3rem; }
.infographic-pyassign2 .as-input {
width: 84px; font-family: var(--mono-font); font-size: 1.1rem; text-align: center;
padding: 0.42rem 0.3rem; border: 1.5px solid var(--line); background: var(--paper-alt); color: var(--ink);
border-radius: 5px;
}
.infographic-pyassign2 .as-input:focus { outline: none; border-color: var(--accent); }
.infographic-pyassign2 .as-input-label { font-size: 0.72rem; color: var(--ink-soft); font-weight: 600; font-family: var(--mono-font); }
.infographic-pyassign2 .as-btn-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 0.4rem; margin-bottom: 1.2rem; }
@media (max-width: 640px) { .infographic-pyassign2 .as-btn-grid { grid-template-columns: repeat(4, 1fr); } }
@media (max-width: 460px) { .infographic-pyassign2 .as-btn-grid { grid-template-columns: repeat(3, 1fr); } }
.infographic-pyassign2 .as-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.3rem; cursor: pointer; border-radius: 5px; text-align: center;
}
.infographic-pyassign2 .as-btn.is-selected { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.infographic-pyassign2 .as-btn.walrus-btn { grid-column: span 2; }
@media (max-width: 460px) { .infographic-pyassign2 .as-btn.walrus-btn { grid-column: span 3; } }
.infographic-pyassign2 .as-name { text-align: center; font-size: 0.8rem; font-weight: 700; color: var(--ink); margin-bottom: 0.7rem; }
/* code readout — always a dark editor look */
.infographic-pyassign2 .device {
--ed-bg: #1e1e1e; --ed-bar: #2d2d2d; --ed-text: #d4d4d4; --ed-soft: #8a8a8a; --ed-ok: #6fdc8c; --ed-err: #ff6b6b;
background: var(--ed-bg); border: 1px solid var(--line); border-radius: 8px; overflow: hidden;
}
.infographic-pyassign2 .device-bar {
display: flex; align-items: center; gap: 0.4rem;
background: var(--ed-bar); padding: 0.5rem 0.7rem; border-bottom: 1px solid #3a3a3a;
font-size: 0.72rem; color: var(--ed-soft); font-weight: 600;
}
.infographic-pyassign2 .device-bar .dot { width: 9px; height: 9px; border-radius: 50%; background: #5a5a5a; }
.infographic-pyassign2 .device-bar .dot:nth-child(1) { background: #ff5f56; }
.infographic-pyassign2 .device-body { padding: 0.9rem 1rem; font-family: var(--mono-font); font-size: 0.9rem; color: var(--ed-text); }
.infographic-pyassign2 .code-row { display: flex; gap: 0.6rem; align-items: baseline; margin-bottom: 0.55rem; flex-wrap: wrap; }
.infographic-pyassign2 .code-row .tag { font-size: 0.65rem; font-weight: 700; color: var(--ed-soft); width: 76px; flex: none; text-transform: uppercase; letter-spacing: 0.03em; }
.infographic-pyassign2 .code-row .val { color: var(--ed-text); }
.infographic-pyassign2 .code-row.result .val { color: var(--ed-ok); font-weight: 700; }
.infographic-pyassign2 .code-row.result .val.is-error { color: var(--ed-err); font-weight: 600; }
.infographic-pyassign2 .type-chip {
font-family: var(--mono-font); font-size: 0.65rem; font-weight: 700; color: var(--ed-soft);
border: 1px solid #3a3a3a; border-radius: 3px; padding: 0.05rem 0.4rem; margin-left: 0.4rem;
}
.infographic-pyassign2 .note-row { font-size: 0.72rem; color: var(--ed-soft); font-style: italic; margin-top: 0.3rem; }
@media (prefers-reduced-motion: reduce) {
.infographic-pyassign2 *, .infographic-pyassign2 *::before, .infographic-pyassign2 *::after {
animation-duration: 0.001ms !important; transition-duration: 0.001ms !important;
}
}
</style>
<div class="as-wrap">
<h1>Assignment Operators</h1>
<p class="as-sub">Edit x and n. Tap any operator to see it work.</p>
<div class="as-inputs">
<div class="as-input-slot">
<input class="as-input" id="asX" type="text" value="5" inputmode="decimal" />
<span class="as-input-label">x</span>
</div>
<div class="as-input-slot">
<input class="as-input" id="asN" type="text" value="3" inputmode="decimal" />
<span class="as-input-label">n</span>
</div>
</div>
<div class="as-btn-grid">
<button class="as-btn is-selected" data-op="assign">=</button>
<button class="as-btn" data-op="add">+=</button>
<button class="as-btn" data-op="sub">-=</button>
<button class="as-btn" data-op="mul">*=</button>
<button class="as-btn" data-op="div">/=</button>
<button class="as-btn" data-op="floordiv">//=</button>
<button class="as-btn" data-op="mod">%=</button>
<button class="as-btn" data-op="pow">**=</button>
<button class="as-btn" data-op="band">&=</button>
<button class="as-btn" data-op="bor">|=</button>
<button class="as-btn" data-op="bxor">^=</button>
<button class="as-btn" data-op="rshift">>>=</button>
<button class="as-btn" data-op="lshift"><<=</button>
<button class="as-btn walrus-btn" data-op="walrus">:= walrus</button>
</div>
<div class="as-name" id="asName">Simple Assignment</div>
<div class="device">
<div class="device-bar"><span class="dot"></span><span class="dot"></span><span class="dot"></span> Example</div>
<div class="device-body">
<div class="code-row"><span class="tag">Example</span><span class="val" id="asExample">x = 5</span></div>
<div class="code-row"><span class="tag">Equivalent</span><span class="val" id="asEquivalent">x = 5</span></div>
<div class="code-row result"><span class="tag">Result</span><span class="val" id="asResult">5</span><span class="type-chip" id="asTypeChip">int</span></div>
<div class="note-row" id="asNote" style="display:none;"></div>
</div>
</div>
</div>
<script>
(function () {
var root = document.currentScript.closest('.infographic-pyassign2')
|| document.querySelector('.infographic-pyassign2');
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 xInput = root.querySelector('#asX');
var nInput = root.querySelector('#asN');
var btns = root.querySelectorAll('.as-btn[data-op]');
var nameEl = root.querySelector('#asName');
var exampleEl = root.querySelector('#asExample');
var equivEl = root.querySelector('#asEquivalent');
var resultEl = root.querySelector('#asResult');
var typeChip = root.querySelector('#asTypeChip');
var noteEl = root.querySelector('#asNote');
var currentOp = 'assign';
var meta = {
assign: { name: 'Simple Assignment', sym: '=', sign: '=' },
add: { name: 'Addition Assignment', sym: '+=', sign: '+' },
sub: { name: 'Subtraction Assignment', sym: '-=', sign: '-' },
mul: { name: 'Multiplication Assignment', sym: '*=', sign: '*' },
div: { name: 'Division Assignment', sym: '/=', sign: '/', note: 'Always returns a float.' },
floordiv: { name: 'Floor Division Assignment', sym: '//=', sign: '//' },
mod: { name: 'Modulus Assignment', sym: '%=', sign: '%', note: 'Stores the remainder.' },
pow: { name: 'Exponentiation Assignment', sym: '**=', sign: '**' },
band: { name: 'Bitwise AND Assignment', sym: '&=', sign: '&', bitwise: true },
bor: { name: 'Bitwise OR Assignment', sym: '|=', sign: '|', bitwise: true },
bxor: { name: 'Bitwise XOR Assignment', sym: '^=', sign: '^', bitwise: true },
rshift: { name: 'Bitwise Right Shift Assignment', sym: '>>=', sign: '>>', bitwise: true },
lshift: { name: 'Bitwise Left Shift Assignment', sym: '<<=', sign: '<<', bitwise: true },
walrus: { name: 'Walrus Operator (Assignment Expression)', sym: ':=' }
};
function isFloatLiteral(raw) { return /\./.test(raw); }
function formatNum(n, isFloat) {
if (isFloat) {
var s = String(n);
if (!/\./.test(s)) s += '.0';
return s;
}
return String(Math.trunc(n));
}
function pyFloorDiv(a, b) { return Math.floor(a / b); }
function pyMod(a, b) { return a - Math.floor(a / b) * b; }
function compute(op, x, n) {
var m = meta[op];
if (op === 'assign') return { ok: true, value: n };
if (m.bitwise) {
if (!Number.isInteger(x) || !Number.isInteger(n)) {
return { ok: false, msg: "TypeError: unsupported operand type(s) for " + m.sign + ": 'float'" };
}
switch (op) {
case 'band': return { ok: true, value: x & n };
case 'bor': return { ok: true, value: x | n };
case 'bxor': return { ok: true, value: x ^ n };
case 'rshift': return { ok: true, value: x >> n };
case 'lshift': return { ok: true, value: x << n };
}
}
switch (op) {
case 'add': return { ok: true, value: x + n };
case 'sub': return { ok: true, value: x - n };
case 'mul': return { ok: true, value: x * n };
case 'div':
if (n === 0) return { ok: false, msg: 'ZeroDivisionError: division by zero' };
return { ok: true, value: x / n, forceFloat: true };
case 'floordiv':
if (n === 0) return { ok: false, msg: 'ZeroDivisionError: division by zero' };
return { ok: true, value: pyFloorDiv(x, n) };
case 'mod':
if (n === 0) return { ok: false, msg: 'ZeroDivisionError: modulo by zero' };
return { ok: true, value: pyMod(x, n) };
case 'pow':
return { ok: true, value: Math.pow(x, n), forceFloat: n < 0 };
}
}
function recompute() {
var rawX = xInput.value.trim();
var rawN = nInput.value.trim();
var m = meta[currentOp];
nameEl.textContent = m.name;
noteEl.style.display = 'none';
if (currentOp === 'walrus') {
var nRaw = rawN === '' ? '4' : rawN;
var nFloat = isFloatLiteral(nRaw);
var nVal = parseFloat(nRaw);
exampleEl.textContent = 'print(y := ' + nRaw + ')';
equivEl.textContent = 'Assigns ' + formatNum(nVal, nFloat) + ' to y, evaluates to ' + formatNum(nVal, nFloat);
resultEl.textContent = formatNum(nVal, nFloat);
resultEl.classList.remove('is-error');
typeChip.textContent = nFloat ? 'float' : 'int';
typeChip.style.display = 'inline-block';
return;
}
var x = parseFloat(rawX);
var n = parseFloat(rawN);
if (isNaN(x) || isNaN(n)) {
exampleEl.textContent = 'x ' + m.sym + ' ' + rawN;
equivEl.textContent = '—';
resultEl.textContent = 'enter valid numbers';
resultEl.classList.add('is-error');
typeChip.style.display = 'none';
return;
}
var xFloat = isFloatLiteral(rawX);
var nFloat = isFloatLiteral(rawN);
exampleEl.textContent = currentOp === 'assign'
? ('x = ' + formatNum(n, nFloat))
: ('x ' + m.sym + ' ' + formatNum(n, nFloat));
equivEl.textContent = currentOp === 'assign'
? ('x = ' + formatNum(n, nFloat))
: ('x = x ' + m.sign + ' ' + formatNum(n, nFloat));
var res = compute(currentOp, x, n);
if (!res.ok) {
resultEl.textContent = res.msg;
resultEl.classList.add('is-error');
typeChip.style.display = 'none';
return;
}
var resultIsFloat = currentOp === 'assign' ? nFloat : (res.forceFloat || xFloat || nFloat);
if (m.bitwise) resultIsFloat = false;
var display = formatNum(res.value, resultIsFloat);
resultEl.textContent = 'x = ' + display;
resultEl.classList.remove('is-error');
typeChip.textContent = resultIsFloat ? 'float' : 'int';
typeChip.style.display = 'inline-block';
if (m.note) {
noteEl.textContent = m.note;
noteEl.style.display = 'block';
}
}
btns.forEach(function (btn) {
btn.addEventListener('click', function () {
btns.forEach(function (b) { b.classList.toggle('is-selected', b === btn); });
currentOp = btn.dataset.op;
recompute();
});
});
xInput.addEventListener('input', recompute);
nInput.addEventListener('input', recompute);
recompute();
})();
</script>
</div>