Stewdent.eu
▾

Constants

A constant is a named value that never changes while a program is running. Using constants makes code easier to read, maintain, and debug.

Last updated: 25 December 2025

★☆☆☆☆ Level 1computingprogrammingVariablesconstants4 min read

Report an error

You already know that a variable is a labelled box that can hold a changing value. A constant is similar, but with one important difference: once you set its value, that value should never change during the whole program. Think of a constant like the speed of light — it is a fixed fact the program needs to know, not something that should be modified.

Why Use Constants?

Imagine you are writing a game where every level has a maximum of 100 points. You could just type 100 everywhere in your code — but what happens if you later decide to change it to 150? You would need to hunt through every single line and update each occurrence. If you miss one, your program will behave inconsistently and the bug will be very hard to find.

With a constant, you define the value once:

MAX_POINTS = 100

Now every part of your program uses MAX_POINTS. When you need to change it, you update one line and every calculation instantly uses the new value.

Note

Python does not have a built-in const keyword like some other languages. Instead, programmers follow a naming convention: write constant names in ALL_CAPS with underscores between words. This signals to anyone reading the code that the value should not be changed.

Declaring Constants in Python

PI = 3.14159
GRAVITY = 9.81
MAX_LIVES = 3
SCHOOL_NAME = "Greenfield Academy"

# Using constants in calculations
circumference = 2 * PI * radius

By placing all your constants at the top of a file, any reader immediately knows what fixed values the program relies on.

Constants vs Variables

FeatureVariableConstant
Value can changeYesNo (by convention)
Naming style (Python)snake_caseALL_CAPS
Examplescore = 0MAX_SCORE = 100
PurposeStores changing stateStores fixed configuration
Tip

If you are using a number more than once in your code and it represents a real-world rule or limit, it is almost always better to turn it into a constant. Numbers with no name are called magic numbers and are considered bad practice.

Constants in Other Languages

In languages like JavaScript, you use the const keyword to enforce that a value cannot be reassigned:

const MAX_SCORE = 100;
const PI = 3.14159;

If you try to change a const in JavaScript, the program will throw an error. Python relies on programmers following the ALL_CAPS convention rather than enforcing it automatically.

Warning

In Python, nothing technically stops you from reassigning an ALL_CAPS name — the language will not throw an error. The convention only works if everyone on the team agrees to respect it. In professional code, tools called linters can warn you if you accidentally change a constant.

Practical Example

# Constants defined at the top
SPEED_OF_SOUND = 343       # metres per second
SECONDS_IN_MINUTE = 60
MINUTES_IN_HOUR = 60

# Using them in the program
echo_time = 2.5            # seconds for sound to return
distance = (SPEED_OF_SOUND * echo_time) / 2
print(f"The object is {distance} metres away.")

This code is easy to understand and easy to update if you ever need to change any of the fixed values.

Practice Problems

A programmer writes SCREEN_WIDTH = 1920 at the top of their file. Later they write SCREEN_WIDTH = 800. What problem does this cause?

Show answer
They have accidentally overwritten a constant, which means any code that relied on SCREEN_WIDTH being 1920 will now behave incorrectly. Constants should never be reassigned after their initial definition.

Why is it better to write MAX_ATTEMPTS = 3 and use MAX_ATTEMPTS in your code rather than typing the number 3 directly each time?

Show answer
Using a named constant means you only need to update the value in one place if it ever changes. It also makes the code more readable because MAX_ATTEMPTS clearly describes what the number means, whereas a bare 3 gives no context.