1.2.3Introduction to Programming (Python)

Variables — naming rules, assignment, reassignment

1,683 words8 min readdifficulty · medium

WHAT is a variable?


HOW assignment works

Why right-to-left? Because the value must exist before a name can point to it. You can't label nothing.


HOW reassignment works

Figure — Variables — naming rules, assignment, reassignment

Naming RULES (must obey — else SyntaxError)


Steel-man: common mistakes


Flashcards

What does the = operator do in Python?
Evaluates the right-hand expression to an object, then binds the left-hand name to that object (assignment, not equality).
In what order is name = expression processed?
Right-to-left: the expression is evaluated first, then the name is bound.
Why is x = x + 5 valid in Python?
= is assignment; the old value of x is used to compute the result, then x is rebound to the new object.
After a=100; b=a; a=200, what is b?
100 — b still labels the original object; reassigning a only moved a's reference.
Can a variable name start with a digit?
No. It must start with a letter or underscore.
Is Age the same variable as age?
No — Python identifiers are case-sensitive.
Name three things forbidden in a variable name.
Starting with a digit, spaces/symbols like - @, and reserved keywords.
What error comes from using a name before it's assigned?
NameError: name '...' is not defined.
What does reassignment do to the OLD object?
Nothing — the name just stops pointing at it; if unreferenced it's garbage-collected.
What's the convention for naming a constant?
ALL_CAPS, e.g. MAX_SPEED = 120.

Recall Feynman: explain to a 12-year-old

Imagine names are sticky labels and values are toys. Saying score = 10 makes a toy "10" and sticks the label score on it. Saying score = 20 does NOT change the old toy — it peels the label off and sticks it on a new toy "20". If your friend put a label copy on the first toy, their label is still on toy "10" even after you moved yours. The = sign isn't saying "these are equal" — it's saying "go stick this label here."


Connections

  • 1.2.01-Data-Types — the objects that variables point to.
  • 1.2.02-Operators= (assign) vs == (compare).
  • 1.2.04-Mutable-vs-Immutable — why b=a is safe for ints but tricky for lists.
  • 1.2.05-Namespaces-and-Scope — where names live.
  • 1.1.00-Memory-and-References — heap objects and the garbage collector.

Concept Map

bound via

points to

reads

creates

binds

rebinds name to new

orphaned object reclaimed by

copies reference not value

must obey else

must satisfy

Variable name in namespace

Object on heap

Reference

Assignment name = expr

Evaluate right side first

Reassignment

Garbage collector

Two names one object

Naming rules

SyntaxError

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, Python me ek variable koi dabba (box) nahi hai jisme value rakhi ho — wo ek naam ka label hai jo memory me pade ek object ko point karta hai. Jab tum likhte ho x = 10, to pehle right side ka object 10 banta hai, phir naam x us object par chipak jaata hai. Isiliye = ko "barabar" mat padho — ise padho "right side run karo, phir uska label name ko de do". Yahi reason hai ki x = x + 5 bilkul valid hai: pehle purana x (10) use karke 15 banta hai, phir x ko naya label mil jaata hai.

Reassignment ka matlab — naam ka sticky note utha kar dusre object par chipka dena. Purana object change nahi hota; agar uspe koi aur label nahi bacha to garbage collector use saaf kar deta hai. Aur ek important baat: b = a value ki copy nahi, reference ki copy karta hai. To agar baad me a = 200 karo, to sirf a ka label hila, b abhi bhi purane 100 ko point kar raha hota hai.

Naming rules simple hain: sirf letters, digits aur underscore; pehla character digit nahi ho sakta; spaces ya -, @ jaise symbols nahi; aur keywords (if, class, for) use nahi kar sakte. Case matter karta hai — age aur Age alag hain. Convention me snake_case aur descriptive naam use karo, kyunki 80% bugs galat ya confusing naam se aate hmain. Yeh chhoti cheez lagti hai par poore programming ka foundation hai.

Test yourself — Introduction to Programming (Python)

Connections