Back to List

Python Variables and Memory References

Understand how variables actually work in Python and the concept of memory references.

Beginner
|
6min
|
Verified (2026-07)
Progress0/18 (0%)

Python Variables and Memory References

After completing this topic, you will understand that in Python, a variable is not a "box that holds a value," but rather a "name tag that points to an object."


Variables are not boxes.

Many textbooks describe variables as "boxes that hold values." However, this is an inaccurate analogy in Python.

python
a = 10

This code does not mean "put 10 into a box named 'a'." Instead, it means:

  1. An integer object 10 is created in memory.
  2. The name a points to (references) that object.
python
a = 10
b = a # b also points to the same object, 10
print(id(a)) # 4350114064
print(id(b)) # 4350114064 # Same address!
print(a is b) # True # It points to the same object

id() returns the memory address of an object. Both a and b are pointing to the same address. It's not copying the value; it's attaching another name tag to the same object.


Reassignment moves the name tag.

python
a = 10
b = a
a = 20 # Reassign 'a' to a new value
print(a) # 20
print(b) # 10 # 'b' still points to the original object

a = 20 does not mean "change the object 'a' points to to 20." Instead, a new object 20 is created in memory, and the name tag 'a' is moved to point to it. b still points to the original 10.


== vs. is

python
x = [1, 2, 3]
y = [1, 2, 3]
print(x == y) # True # The values are the same
print(x is y) # False # They are different objects
z = x
print(x is z) # True # They point to the same object
OperatorComparison TargetMeaning
==ValueAre the contents the same?
isIdentityAre they the same object? (memory address)

"Two copies of the same book" would be True for == and False for is. If "the same book is held by two people," then both would be True.


Data Types and Conversion

Python's basic data types:

python
# Numbers
integer = 42 # int (integer)
floating = 3.14 # float (floating-point number)
# String
text = "μ•ˆλ…•ν•˜μ„Έμš”" # str
# Boolean
flag = True # bool (True/False)
# None - a special type that represents "no value"
result = None
# Type checking
print(type(42)) # <class 'int'>
print(type("hello")) # <class 'str'>

Type Conversion

python
# String to number
age = int("25") # 25
price = float("9.99") # 9.99
# Number to string
str(42) # "42"
# Caution: Error if conversion is not possible
int("hello") # ValueError!

Naming Rules

python
# Good variable names - names that reveal their meaning
user_name = "κΉ€ν›ˆ"
total_score = 95
is_active = True
# Bad variable names
x = "κΉ€ν›ˆ" # Cannot tell what value it holds
a1 = 95 # Meaningless
# Rules
# - You can use letters, numbers, and underscores (_)
# - Cannot start with a number (2name β†’ Error)
# - snake_case is recommended (Python convention)
# - Cannot use reserved words (if, for, class, etc.)

Python uses snake_case rather than camelCase. Instead of userName, use user_name. This is a recommendation from PEP 8 (the official Python style guide).


πŸ’¬ Questions & Comments

0 comments

You can post without signing in. Guest comments cannot be edited or deleted by their author.

0/2000

Loading...