Remember
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.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
DAYS_IN_WEEK = 7
weeks = 3
days = weeks * DAYS_IN_WEEK
print(days)Top-level scope
7Object 2 · int3Object 3 · int21Uppercase names express an intention to keep a value unchanged. Python still permits reassignment; the convention is for people reading the code.
(no output)
What do you expect this program to print?
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: 21Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
DAYS_IN_WEEK = 7
weeks = 3
days = weeks * DAYS_IN_WEEK
print(days)Top-level scope
7Object 2 · int3Object 3 · int21The example has finished. Compare the final output with the values and references above.
21
What do you expect this program to print?
DAYS_IN_WEEK = 7
weeks = 3
days = weeks * DAYS_IN_WEEK
print(days)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.
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.”
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.
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.
MAX_SPEED, API_KEY, PIConstants 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.
Connect what you noticed in the example with the idea below.
Uppercase names express an intention to keep a value unchanged. Python still permits reassignment; the convention is for people reading the code.
Uppercase names cannot be reassigned by Python.
This is the misconception to avoid.Reaching the recap and completing the quizzes are separate milestones.