Like other programming languages, Python variables allow us to store or assign a value to a variable name. It helps in applying programming logic to a variety of inputs.
The article below will review how to define variables in Python, the correct syntax, and run simple experiments. By doing so, we can study their behavior and gain some insight into Python’s internal workings.
Defining Variables in Python
One advantage of a dynamically typed language like Python is that a single variable can be used for multiple data types. However, such a practice is not recommended.
A variable can be easily created in Python using the assignment (“=”) operator as shown below:
Python syntax allows for a variety of variable names. Variable names are permitted to contain letters, numbers, and underscores. A variable cannot start with a number but can start with a letter or an underscore.
Python also uses a list of reserved keywords that hold a special meaning. For example, the word “def” is used to define functions. It is possible for variables to use keywords as names, but it is not good practice. Finally, a variable name should not include any spaces or tabs.
It is important to note that variable names are case-sensitive, which means that a variable named “x” is different from a variable named “X.”
Legal naming conventions are listed below:
The scope of a variable can be thought of as the context within which the variable exists. It is an important concept because the value of a Python variable will depend on the scope of that variable.
When a variable is defined inside a function, it is possible for a variable with the same name to refer to two different values depending on the active scope of the program.
For example, the variable x contains the value 5 inside of the function. However, outside of the function, it takes the value 4. It’s important to know the scope in which the variable is defined. It is advisable to use unique variable names regardless of the scope. It can help to avoid confusion and unexpected errors.
The concept of scope is illustrated in the code snippet below:
How Python Variables Work
Python variables are not what they seem. They do not hold values; instead, they hold references to objects. In Python, everything is an object, which means even the most basic data types like integers and strings are objects. It is different from C++ and other programming languages, where integers are standalone data types and not complex objects with associated properties and methods.
The fact that integer data types are objects can be verified using the type() function. Running the following command yields the type of variable x, which has been assigned the integer 4.
When the variable x is defined and assigned a value of 4, an integer object is created in the memory with the value 4. Then, the variable x is given the address of where the object is in the memory. Each time the variable is accessed, it accesses the object stored in the memory location and returns the associated value. The said concept is illustrated in the figure below:
The table represents computer memory, and the variable “x” points to the integer object stored in memory.
The above process can be verified using the id() method in Python. The id() method returns the address of the stored object that a variable points to. If the variable is assigned a different value, it will change the address to update the current value. It illustrates the concept of Python storing the integer and the variable acting more like a label. To demonstrate the concept, try the experiment below on your computer:
We can see that even though x and y are different variables, they share the same address because they point to, or are labels for, the same integer value 4. What would happen when we assign a different value to the variable x?
The address changes to reflect that the variable x now points to a different object with the value 5. The address displayed is that of the integer object with the value 5.
Variables can also be defined by referencing other variables. The following statement defines the variable x2 as being equal to x2. If we compare the id of both x1 and x2, we see that they are identical. It can be verified by using the “is” operator.
The test will check if two variables point to the same object. The test’s output can be seen below. The variable x2 can be thought of as just another label for the object that the variable x1 refers to.
Learn more about Python variables and other basic Python concepts through CFI’s Python Fundamentals course. To keep learning, the following resources will be helpful:
Take your learning and productivity to the next level with our Premium Templates.
Upgrading to a paid membership gives you access to our extensive collection of plug-and-play Templates designed to power your performance—as well as CFI's full course catalog and accredited Certification Programs.
Already have a Self-Study or Full-Immersion membership? Log in
Access Exclusive Templates
Gain unlimited access to more than 250 productivity Templates, CFI's full course catalog and accredited Certification Programs, hundreds of resources, expert reviews and support, the chance to work with real-world finance and research tools, and more.