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 = 100When 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
EasyCreate 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 = 100This 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 hereThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Variables Exploration
ConstantsMultiple Variable AssignmentsSwapping VariablesPlaceholder VariablesRound NumbersList Casting4Contact Book Application
Display MenuAdd Contact7Sets Part 2
Mathematical Operations Part 1Mathematical Operations Part 2Recap - Treasure HuntSubsets and SupersetsIterating Over SetsRecap - Tournament Tracker2Dictionaries Part 1
What is a Dictionary?Creating a DictionaryAccessing ValuesModifying DictionariesRecap - Recipe Manager5Advanced Decision Making
Ternary OperatorMembership ChecksIdentity ChecksIndentation ErrorsRecap - Vacation Filter8Student Records Manager
Project OverviewAdd Student11Advanced Functions
Returning Multiple ValuesLambda Functions Part 1Lambda Functions Part 2Recap Challenge - Lambda SortRecursive Functions Part 1Recursive Functions Part 2Recap - Sum Nested List14Higher-Order Functions
The Map FunctionThe Filter FunctionRecap - Email ValidatorRecap - Number Processor3Dictionaries Part 2
Dictionary MethodsNested DictionariesChecking for KeysLooping Through DictionariesRecap - Frequency Counter9Advanced Data Aggregation
Using SumFinding Minimum and MaximumSorting Data EfficientlyRecap - Dictionary Sorter