Variables in Python look like variables in C, but they work differently underneath. Understanding this difference now will prevent many surprises when you work with lists and dictionaries later.
Names and objects
x = 5, then x = 'five'
x ──► 5 x ──► 'five' 5 (no longer referenced: freed)
Syntax:
Assignment
variable_name = expression
The expression on the right is evaluated first, then the name on the left is bound to the result. The = sign means “make this name refer to”, not mathematical equality; comparison uses ==.
Forms of assignment
Form
Example
Effect
Simple
number = 100
Binds number to 100
Reassignment, possibly to another type
year = 2020, then year = "twenty twenty"
year now refers to a string
Same value to several names
x = y = z = 5
All three names refer to the same object 5
Several values at once
name, age = "Nav", 31
name gets “Nav”, age gets 31
Augmented
count += 1
Same as count = count + 1
Using a name that has never been assigned raises NameError.
Key takeaways
name = expression evaluates the right side, then binds the name.
Names have no fixed type; values do.
x = y = 5 and a, b = 1, 2 assign several names in one statement.
An unknown name raises NameError.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Explain the difference between x = 5 in C and in Python.
Show worked answer
In C, int x = 5; declares a fixed-size memory location of type int named x and stores 5 in it; x can only ever hold an int. In Python, x = 5 creates (or reuses) an integer object 5 and binds the name x to it; there is no declaration, and later x = "five" simply rebinds the name to a string object. In Python the type belongs to the object, not to the name.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/data-structures-and-control-statements-in-python/variables-and-assignment/ to read this page.