Search⌘ K
AI Features

Variables

Explore how Python variables serve as references to objects rather than containers. Learn to create variables, understand dynamic typing, handle reassignment, and follow naming conventions to write clear, maintainable Python code.

We are now ready to move beyond printing static text and begin working with data. In many programming languages, variables are commonly described as containers or boxes: you create a box and then store a value inside it. Python, however, follows a different and more flexible model. Rather than acting as containers, variables in Python function more like labels or name tags. An object is created in memory, and a variable simply refers to that object by attaching a name to it.

Although this distinction may seem subtle at first, it underpins Python’s dynamic nature and contributes significantly to its simplicity and ease of use.

How to create a variable

In Python, a variable is created by assigning a value to a name using the assignment operator (=). Here's the syntax:

<name> = <value>

This is not a mathematical equality statement; it is a command. It tells Python to evaluate the value on the right side and bind it to the name on the left. Because Python is dynamically typed, we do ...