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.

  • literals
  • variables
  • constants
  • input-output

Use these short, runnable examples as demonstration checkpoints. Ask students to predict the output before you run each one.

  1. 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”.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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:

  1. Start with the constant and a hard-coded value for hours.
  2. Replace the hard-coded value with input.
  3. Add the conversion to int().
  4. Add a meaningful output sentence.

Exit-ticket prompts

  1. Identify the literal in: campus = “Booneville”
  2. Write a snake-case variable name for “number of absences.”
  3. Write a constant for the number of days in a week.
  4. Trace: total = 3; total = total + 2; print(total).
  5. Explain why an area code should usually be stored as text.

Answer key

  1. “Booneville”
  2. number_of_absences
  3. DAYS_PER_WEEK = 7
  4. 5
  5. It is an identifier and may need leading zeros; it is not normally used in calculations.