Python Progamming/Basics of Python Language

Python Variables and Constants

Updated on January 12, 2026
3 min read

Variables in Pythons

A variable in Python is a name that refers to a value stored in memory. Just like in mathematics, a variable represents something that can change. In programming, it allows a program to store information, remember it, and use it later.

When you create a variable, you are giving a value a name. That name makes the data easy to access, update, and work with. The value can be a number, text, a list, or any other kind of data. As the program runs, the value connected to a variable can change.

For example:

age = 18

Here, age is the variable and 18 is the value.

Later, the value can change:

age = 20

The variable age still exists, but now it refers to a different value.

Many beginners think of a variable as a container that stores data. That idea works at a basic level. But in Python, a variable is actually a reference to an object in memory. The object holds the data. The variable simply points to it. When you assign a new value to a variable, Python makes it point to a new object.

This is why you can do things like:

x = 5
x = 10

The name x first points to the object 5, then it points to the object 10. The old value is not changed—x is just redirected to something new.

Variables are used everywhere in real programs. A variable can store a user’s name, the number of logged-in users, a player’s score in a game, the temperature from a sensor, or the total price in a shopping cart. Without variables, programs would not be able to remember or process changing information.

There are two main things you do with variables. You read their value, and you assign them a new value. When you use a variable’s name in your code, you are accessing its current value. When you use the assignment operator (=), you are telling Python to make that variable refer to a new value.

In short, variables are the memory of a program. They allow Python to track data, modify it, and use it to make decisions. Mastering variables is not optional—everything else in Python depends on them.

Constants in Python

A constant is a value that is meant to stay the same throughout a program. While variables are used to store data that can change, constants are used to store values that should remain fixed. These are things like the value of pi, a maximum limit, a tax rate, or a configuration setting that must not be modified.

In Python, constants are not enforced by the language. Python does not have a special keyword like const. Instead, constants are created by convention. When programmers want to mark a value as a constant, they write the variable name in uppercase.

Example:

PI = 3.14
MAX_USERS = 100
BIRTH_YEAR = 2010

These names tell anyone reading the code that these values should not be changed.

At a beginner level, you can think of a constant as a locked container. You can look at what is inside, but you are not supposed to change it. The program depends on this value staying the same.

At a deeper level, constants in Python are just variables. They still point to objects in memory, and Python will allow them to be reassigned. But changing a constant breaks the rules of clean programming and can cause bugs that are hard to track.

For example:

MAX_USERS = 100
MAX_USERS = 200   # This is allowed, but it is wrong design

The interpreter will not stop this, but a good programmer will not do it.

Constants are used to make programs safer and more readable. When a value is written in one place and used everywhere, changing it becomes easy and controlled. If the tax rate changes, you update it in one place instead of hunting through the entire program.

Constants also prevent accidental mistakes. If a value is meant to be fixed, using a constant tells both the programmer and anyone else reading the code not to modify it.

In real systems, constants are used for configuration values, limits, mathematical constants, error codes, and system settings. They act as the stable backbone of a program.

In short, constants give structure and safety to your code. Variables hold changing data. Constants protect important values. If you don’t respect that difference, your software becomes fragile.

Difference Between Variables and Constants in Python

In every Python program, data must be stored somewhere. That data is stored using variables and constants. While both are used to hold values, they serve very different purposes in how a program is designed and maintained.

variable is used to store data that can change while the program is running. For example, a user’s age, a player’s score, or the total price in a shopping cart all change as the program executes. Variables exist because programs deal with moving, changing information.

constant, on the other hand, is used to store data that should remain fixed. These are values that the logic of the program depends on staying the same. Examples include the value of π, the maximum number of users allowed, or a tax rate. In Python, constants are written in uppercase to signal that they must not be changed.

Even though Python technically allows both variables and constants to be reassigned, their meaning is different. Changing a variable is expected. Changing a constant is a design mistake.

Here is a simple comparison:

FeatureVariableConstant
PurposeStores changing dataStores fixed values
Can change?YesShould not
Naming styleLowercase or mixed caseUppercase
Used forUser input, calculations, resultsLimits, configuration, formulas

Example:

age = 18        # variable
PI = 3.14       # constant

Later in the program:

age = 19        # correct
PI = 3.15       # allowed by Python, but wrong practice

The difference is not enforced by the interpreter—it is enforced by good engineering discipline. Variables represent the moving parts of your program. Constants represent the rules that must not change.

Python Variables and Constants | Python Progamming | Learn Syntax