Variables and Name Binding
15 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)In Python, a variable is not a typed memory box. Instead, a variable is a name tag bound to an object in memory.
Variables as name tags
Variable is a name that refers to an object in memory. In Python the name has no type; the object it points to holds the type and the value.
When you write an assignment, Python creates an object in memory and binds the variable name to that object.
total_score = 95print(total_score)Example 1 · Three variables, three types
course_name = "Python Placement Track"modules_count = 12is_active = True
print(course_name)print(modules_count)print(is_active)print(type(course_name))Python Placement Track 12 True <class 'str'>
What this code does
course_namereferences a string object.modules_countreferences an integer object.is_activereferences a boolean valueTrue.type(course_name)prints<class 'str'>.
Unlike statically typed languages like Java or C++, you do not declare a type such as int or String. The object holds the type, not the variable name.
Dynamic rebinding
Rebinding is pointing an existing name at a different object. The old object is not changed; the name simply refers to something new.
Because variables are name tags, you can rebind a name to an object of a completely different type at any time.
status = "pending"print(status)status = 200print(status)Example 2 · Two names, one object
price = 100same_price = priceprice = 120print(price)print(same_price)120 100
What this code does
same_price = pricemakes both names point to the same object, 100.price = 120rebinds onlypriceto a new object.same_pricestill points to 100, so it keeps its value.
Python updates the reference smoothly. While dynamic typing gives great flexibility, write clear code so variable roles stay predictable.
PEP 8 naming rules and snake_case
PEP 8 is the official Python style guide. It asks for snake_case names: lowercase words joined by underscores, such as order_total.
The official Python style guide, PEP 8, recommends snake_case for variable and function names.
- Use snake_case: write
order_totalanduser_email, notorderTotalorOrderTotal. - Start with a letter or underscore:
user_1is valid;1_userraises a SyntaxError. - Avoid Python keywords: names like
def,class,returnandifare reserved. - Constants use UPPER_SNAKE_CASE: write
MAX_RETRIES = 3for values that should not change.
Inspecting types with type()
Type is the kind of an object, such as int, float, str or bool. It decides what the object can hold and what you can do with it.
Use the built-in type() function whenever you want to check what kind of object a variable currently points to.
price = 49.99print(type(price))Example 3 · The type follows the object
status = "pending"print(type(status))status = 200print(type(status))<class 'str'> <class 'int'>
What this code does
- At first
statuspoints to a string, sotypeshowsstr. - After rebinding it points to an integer, so
typeshowsint. - The name never had a type; the object did.
Python displays <class 'float'>, confirming that price references a floating-point number object.
Next step
Check your understanding
9 quick questions