Menu
Coddy logo textTech

Constants

Part of the Logic & Flow section of Coddy's Python journey — lesson 1 of 78.

In programming, a constant is a type of variable whose value cannot be changed during the execution of a program. Constants are useful for defining values that are used multiple times throughout a program, such as mathematical constants like PI or the number of hours in a day.

In Python, there is no strict enforcement of constants like in some other languages (such as c++). However, there is a widely adopted convention to indicate that a variable should be treated as a constant. This is done by writing the variable name in all uppercase letters, often with underscores to separate words.

For example:

PI = 3.14159
HOURS_IN_A_DAY = 24
MAX_USERS = 100

When you see variables named this way in Python code, it's a signal that these values should not be altered. This convention helps make the code more readable and maintainable. It's important to note that this is just a naming convention; the Python interpreter itself does not prevent you from changing the value of these variables.

challenge icon

Challenge

Easy

Create a Python program that calculates the area of a circle using a constant for the value of PI. Define a constant named PI and set its value to 3.14159. Then, prompt the user to enter the radius of the circle. Calculate the area of the circle using the formula (area = PI * radius**2) and print the result.

Remember to follow the naming convention for constants by using all uppercase letters for the variable PI.

Cheat sheet

In Python, constants are variables whose values should not be changed during program execution. Python uses a naming convention to indicate constants:

Write constant names in all uppercase letters, using underscores to separate words:

PI = 3.14159
HOURS_IN_A_DAY = 24
MAX_USERS = 100

This is a convention only - Python doesn't enforce immutability, but it signals to other developers that these values should remain unchanged.

Try it yourself

# Write code here
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow