Python Variables

Used to store or assign a value to a variable name

Over 1.8 million professionals use CFI to learn accounting, financial analysis, modeling and more. Start with a free account to explore 20+ always-free courses and hundreds of finance templates and cheat sheets.

What are Python Variables?

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.

Python Variables

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

Python is a dynamically typed language, meaning Python recognizes the type of variable automatically. Therefore, the programmer does not need to define the data type when defining the variable. Python discerns the type of the variable from the value it is assigned. It is opposed to statically typed languages like C++ or JavaScript, which require the programmer to declare the type of the variable while defining it.

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 Variables - Assignment Operator

Variable Names

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:

  • _variable_name_
  • variablename
  • variable_name
  • variableName
  • VariableName
  • variablename1
  • variable_name_1
  • variablename_1

Scope

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:

Scope

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.

Type Function

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:

Inside Python Variable Definition

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:

Sample Test 1

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?

Sample Test 2

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.

Test Output

Related Readings

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:

0 search results for ‘