Python Constants

Let’s look at constants

  1. Notice

    Uppercase names express an intention to keep a value unchanged. Python still permits reassignment; the convention is for people reading the code.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
DAYS_IN_WEEK = 7
weeks = 3
days = weeks * DAYS_IN_WEEK
print(days)
Live state

Top-level scope

DAYS_IN_WEEK→Object 1
weeks→Object 2
days→Object 3
Object 1 · int7Object 2 · int3Object 3 · int21
This step

Uppercase names express an intention to keep a value unchanged. Python still permits reassignment; the convention is for people reading the code.

Picture constants

Uppercase names express an intention to keep a value unchanged. Python still permits reassignment; the convention is for people reading the code.

Image brief

Alt text
A diagram for Python Constants. Instructions: DAYS_IN_WEEK = 7 weeks = 3 days = weeks * DAYS_IN_WEEK print(days). Displayed result: 21. The final example bindings are DAYS_IN_WEEK --> object 1 (int); weeks --> object 2 (int); days --> object 3 (int). object 1: 7; object 2: 3; object 3: 21
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python Constants". Teaching focus: Uppercase names express an intention to keep a value unchanged. Python still permits reassignment; the convention is for people reading the code. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS DAYS_IN_WEEK = 7 weeks = 3 days = weeks * DAYS_IN_WEEK print(days) | v DISPLAYED RESULT 21 FINAL BINDINGS DAYS_IN_WEEK --> object 1 (int) weeks --> object 2 (int) days --> object 3 (int) object 1: 7 object 2: 3 object 3: 21 Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python Constants. Instructions: DAYS_IN_WEEK = 7 weeks = 3 days = weeks * DAYS_IN_WEEK print(days). Displayed result: 21. The final example bindings are DAYS_IN_WEEK --> object 1 (int); weeks --> object 2 (int); days --> object 3 (int). object 1: 7; object 2: 3; object 3: 21

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
DAYS_IN_WEEK = 7
weeks = 3
days = weeks * DAYS_IN_WEEK
print(days)
Live state

Top-level scope

DAYS_IN_WEEK→Object 1
weeks→Object 2
days→Object 3
Object 1 · int7Object 2 · int3Object 3 · int21
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
21

What do you expect this program to print?

What does this example display?

DAYS_IN_WEEK = 7
weeks = 3
days = weeks * DAYS_IN_WEEK
print(days)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

21

Follow the printed values in order. The output is: 21

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

Uppercase names express an intention to keep a value unchanged. Python still permits reassignment; the convention is for people reading the code.

Uppercase names express an intention to keep a value unchanged. Python still permits reassignment; the convention is for people reading the code.

Constants

In Python, a constant is a value that should not change once it's been given. For example, if you have a value like the number of days in a week, it always stays the same: 7.

While Python doesn't have a special way to make real constants (like some other languages do), we can still follow a naming rule to show that something is meant to stay the same.

How to Create a Constant in Python

We usually use capital letters for constant names, like this:

DAYS_IN_WEEK = 7
PI = 3.14
APP_NAME = "MyFirstApp"

These names follow the constant naming convention. The capital letters tell us, “Hey! Don’t change this value.”

{ "array": ["7", "3.14", "MyFirstApp"], "showIndices": false, "highlightIndicesBlue": [0], "highlightIndices": [1], "highlightIndicesGreen": [2], "labels": { "0": "DAYS_IN_WEEK", "1": "PI", "2": "APP_NAME" } }

Using Constants

DAYS_IN_WEEK = 7
print("There are", DAYS_IN_WEEK, "days in a week.")
There are 7 days in a week.

Explanation: We created a constant DAYS_IN_WEEK and printed its value along with some text. The value 7 was shown because that’s what we stored in it.

What Happens If We Change a Constant?

PI = 3.14
print("Original PI:", PI)

PI = 3.14159  # changing the value
print("Changed PI:", PI)
Original PI: 3.14
Changed PI: 3.14159

Explanation: Python let us change the value, even though we wrote it as a constant. But this is not a good habit. Constants are called that because we should not change them.

So, it's our responsibility as programmers to treat them like fixed values and not update them later.

Tips for Using Constants

  • Use capital letters with underscores: MAX_SPEED, API_KEY, PI
  • Put constants at the top of your Python file so they are easy to find
  • Don’t change their values once they’re set

Conclusion

Constants are values that we decide should not change. Python doesn’t force them to stay constant, but we follow rules to treat them that way.

What to remember

Connect what you noticed in the example with the idea below.

Remember

Uppercase names express an intention to keep a value unchanged. Python still permits reassignment; the convention is for people reading the code.

Watch out for this idea

Uppercase names cannot be reassigned by Python.

This is the misconception to avoid.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Python Data Types →