Programming in Python · Lesson 3
Literals, Variables, and Constants — Programming Examples
Eight runnable Python examples covering literals, variable naming, constants, initialization, dynamic typing, and reading input.
Use these short, runnable examples as demonstration checkpoints. Ask students to predict the output before you run each one.
- Literals: values written directly in code
whole_number = 7
price = 2.75
initial = "A"
course_name = "Programming in Python"
is_online = True
print(whole_number)
print(price)
print(initial)
print(course_name)
print(is_online)
Teaching point: Quotation marks make text a string; True is Boolean, not the string “True”.
- Number-looking strings
zip_code = "38843"
pin = "0042"
age = 20
print(zip_code)
print(pin)
print(age + 1)
Teaching point: ZIP codes and PINs are identifiers, not quantities to calculate. Keeping leading zeros requires strings.
- Variable names communicate purpose
student_name = "Jordan"
current_balance = 125.50
number_of_courses = 4
print(student_name)
print(current_balance)
print(number_of_courses)
Naming reminder: start with a letter or underscore; use descriptive names. student_name is snake case, and studentName is camel case.
- Variables change; constants should not
MINUTES_PER_HOUR = 60
hours_studied = 2
minutes_studied = hours_studied * MINUTES_PER_HOUR
print(minutes_studied)
hours_studied = 3
minutes_studied = hours_studied * MINUTES_PER_HOUR
print(minutes_studied)
Teaching point: hours_studied changes. MINUTES_PER_HOUR represents a value the program intends to keep fixed.
- Initialization versus later assignment
score = 0 # initialize
score = 10 # assign a new value
score = score + 5 # use the old value, then store the new value
print(score)
Trace it aloud: score starts as 0, becomes 10, and then becomes 15.
- Dynamic typing and None
response = None
print(response)
response = 42
print(response)
response = "forty-two"
print(response)
Teaching point: None represents no value yet. Python infers a type from the value assigned, and a variable can later hold a different type.
- Input is text by default
name = input("What is your name? ")
print("Welcome,", name)
age_text = input("How old are you? ")
print("Next year you will be", int(age_text) + 1)
Teaching point: input() returns a string. Convert it with int() only when the response needs to be used as a whole number.
- Mini live-coding challenge: study-time calculator
MINUTES_PER_HOUR = 60
student_name = input("Student name: ")
hours = int(input("Hours studied today: "))
minutes = hours * MINUTES_PER_HOUR
print(student_name, "studied for", minutes, "minutes.")
Build sequence:
- Start with the constant and a hard-coded value for hours.
- Replace the hard-coded value with input.
- Add the conversion to int().
- Add a meaningful output sentence.
Exit-ticket prompts
- Identify the literal in: campus = “Booneville”
- Write a snake-case variable name for “number of absences.”
- Write a constant for the number of days in a week.
- Trace: total = 3; total = total + 2; print(total).
- Explain why an area code should usually be stored as text.
Answer key
- “Booneville”
- number_of_absences
- DAYS_PER_WEEK = 7
- 5
- It is an identifier and may need leading zeros; it is not normally used in calculations.
Course Notes