Variables and assignment

Variables are one of the basic mechanisms that allow Python programs to remember information as they run. Any non-trivial program—whether it generates HTML pages, processes data files, or coordinates AI components—relies on variables to hold intermediate results and evolving state. This lesson exists to orient us to how Python represents and updates named values in a running program.

What a variable represents in Python

A variable in Python is a name that refers to a value held in memory. Rather than thinking of a variable as a fixed container, it is more useful to think of it as a label that can point to different values over time. This model becomes important as programs grow and values are reused, transformed, and replaced.

Assigning values using the equals sign

Assignment in Python is done with the = operator. This statement binds a name to a value, making that value accessible later in the program by using the name.

planet_name = "Mars"

After this line runs, the name planet_name refers to the string "Mars".

Variable names and naming rules

Variable names in Python must start with a letter or an underscore and may contain letters, digits, and underscores. By convention, names use lowercase letters with underscores to separate words. Clear names make programs easier to read and reason about, especially when variables represent meaningful concepts rather than temporary values.

Reassigning variables to new values

Variables can be reassigned at any point by using another assignment statement with the same name. The name stays the same, but the value it refers to changes.

planet_name = "Mars"
planet_name = "Jupiter"

After the second assignment, planet_name now refers to "Jupiter", and the previous value is no longer relevant to the program.

Using variables in simple expressions

Once a variable is assigned, it can be used as part of expressions. Python evaluates the expression using the variable’s current value and produces a result.

radius_km = 3390
diameter_km = radius_km * 2

Here, the value of radius_km is used to compute a new value, which is then assigned to diameter_km.

Conclusion

We have seen how Python uses variable names to refer to values, how assignment works, and how variables participate in simple expressions. This is enough to recognize and use variables as the basic building blocks of state in Python programs. With this mental model in place, we are ready to work with richer data and more complex computations.