No history yet

Python Memory and Syntax

Variables Are Just Labels

In many programming languages, a variable is like a box where you store a value. If you assign one variable to another, you're copying the value from one box to a new one. Python works differently. Variables are more like labels or name tags pointing to objects that live in your computer's memory.

price = 20
sale_price = price

Here, you haven't created a new object. Instead, you've created a new label, sale_price, and stuck it onto the same integer object that price already points to. Every object in Python has a unique identifier, which you can see with the id() function. Think of it as the object's memory address.

# Both variables point to the same object
print(id(price))
print(id(sale_price))

# The output will show identical numbers

This concept of variables as references is fundamental. It's why Python is described as having dynamic typing a system where the type of a variable is determined at runtime, not when you first write the code. The type belongs to the object, not the variable name itself. The name price can point to an integer now and a string later.

Mutable vs. Immutable

Understanding that variables point to objects leads to another key idea: some objects can be changed, and some can't. This is the distinction between mutable (changeable) and immutable (unchangeable) types.

Common immutable types include integers, floats, strings, and tuples. When you perform an operation that seems to modify an immutable object, Python actually creates a new object and reassigns the variable label to it.

greeting = "hello"
print(id(greeting))  # Original ID

# This creates a NEW string object
greeting = greeting + " world"
print(id(greeting))  # A different ID

Mutable types, like lists and dictionaries, can be changed in-place without creating a new object. This can have surprising effects if you're not careful.

my_list = [1, 2, 3]
print(id(my_list))

# Also point another_list to the same object
another_list = my_list

# Modify the list using one of its names
another_list.append(4)

# The change is visible through both names
print(my_list)  # Output: [1, 2, 3, 4]
print(id(my_list)) # ID is unchanged

Since my_list and another_list both point to the same list object, modifying it via one variable affects what you see through the other.

Writing Readable Code

How you write your code matters. Consistent style makes code easier to read, share, and maintain. In the Python community, the official style guide is PEP 8. It's a set of recommendations for writing clean, professional Python code.

ElementNaming ConventionExample
VariablesUse snake_case (all lowercase with underscores)user_name, total_items
FunctionsSame as variables: snake_casecalculate_tax(), send_email()
ClassesUse PascalCase (or CapWords)ShoppingCart, DatabaseConnection
ConstantsUse ALL_CAPS with underscoresMAX_CONNECTIONS, API_KEY

Beyond naming, PEP 8 suggests using 4 spaces for indentation (not tabs) and keeping lines under 79 characters long. Most code editors can be configured to help you follow these rules automatically.

A good mental model of Python's memory management and a commitment to clean code style will set you up for success as you tackle more complex problems.