What Are Constants in Programming?

Hello everyone! 😊

Today, we’re going to talk about another essential programming concept that pairs perfectly with what we learned last time — the variable. This time, let’s dive into its opposite: the constant.

Illustration representing constants in programming

What Is a Constant?

In programming, some values change over time, while others remain fixed throughout the entire execution of a program.

For example:

  • A year always has 12 months.
  • The mathematical constant π (pi) is always 3.14159.

These are values that never change, no matter when or where they are used. In programming, such a value is called a constant.


Variable vs Constant

If a variable is like a mailbox where you can replace the contents anytime, then a constant is like a signpost with a big “Do Not Change” sticker on it.

Once it’s created, its value can’t be modified.

Signpost analogy for constants

Why Use a Constant?

Sometimes, when writing a program, you want to give a name to a value — but make sure that value can never be altered.

PI = 3.14159

You can now use PI anywhere in your code, and it will always represent 3.14159. But if you try to assign it a new value like PI = 3.2, that’s not allowed — because it’s meant to stay the same. It’s like having a signpost labeled “Permanent Value – Do Not Edit”.

By contrast:

score = 90 
score = score + 10

Here, score can be updated — which makes it a variable.

  • A variable can hold changing data.
  • A constant holds a fixed value.

Why Constants Matter

You might wonder:

“Why not just write the number directly instead of naming it?”

Good question! The biggest reasons are meaning and safety.

area = radius * radius * 3.14159

At first glance, 3.14159 looks random — what does it mean? If we replace it with a constant:

PI = 3.14159
area = radius * radius * PI

Now the code instantly makes sense. It’s more readable and self-explanatory.

Additionally, using constants prevents accidental changes. If someone mistakenly modifies the value of a constant, the program will raise an error — keeping your logic safe.

That’s why constants are commonly used for mathematical formulas, reference values, and fixed configuration settings.


A Simple Example

Let’s say you’re building a program to calculate student grades:

MAX_SCORE = 100
score = 85
print(score / MAX_SCORE * 100)

Here, MAX_SCORE is a constant. The maximum possible score should always be 100 — if it suddenly changes to 200, every result will be wrong.

By defining it as a constant, you lock that value, protecting your code from accidental errors.


Tips for Using Constants

  1. Declare constants for all unchanging values.
    Whenever you notice a number or setting that should never be modified, make it a constant.
  2. Use uppercase names.
    It’s a common convention — names like PI, MAX_SCORE, or TAX_RATE tell readers right away: “This value is fixed.”
  3. Give meaningful names.
    Instead of writing A = 10, use MAX_USER = 10 or DEFAULT_SIZE = 10. Clear names make your code self-documenting.

Summary

A constant is a value that never changes once defined. While it doesn’t offer the flexibility of variables, it provides stability and clarity — serving as a reliable reference point in your program.

  • Use variables for values that can change.
  • Use constants for values that must stay the same.

This distinction helps prevent bugs and makes your code easier to understand.


That’s it for today’s lesson on constants! Once you start writing code, you’ll naturally develop a sense for when to use variables and when to define constants.

In the next post, we’ll explore data types — the kinds of values that variables and constants can store.

Thanks for reading, and happy coding! 😊

You can view the original blog post in Korean at the links below:

View in Korean