Programming in Python · Lesson 3

Literals, Variables, and Constants

Represent data with literals, name memory locations with variables, assign values, and collect basic input and output.

  • literals
  • variables
  • constants
  • assignment
  • input-output

Lesson purpose

Programs need a way to represent values, remember them, change them, and communicate with users. Python uses literals, variables, constants by convention, assignment statements, and input()/print() for these jobs.

Learning objectives

  • Identify integer, floating-point, character/string, and Boolean literals.
  • Create descriptive variable names and conventional constants.
  • Explain initialization, assignment, dynamic typing, type inference, and None.
  • Collect user input and display a variable’s value.

1. Literals

A literal is a data value written directly in program code. Numeric literals include integers and floating-point values. Character and string literals are enclosed in quotes. Boolean literals are True and False and are not strings.

ring_count = 7          # integer literal
thickness = 2.7         # floating-point literal
symbol = '@'            # character/string literal
location = "Mordor"    # string literal
is_ready = True         # Boolean literal

Digits can represent either a quantity or an identifier. Store a ZIP code, PIN, or area code as a string when it is not used mathematically, especially when leading zeros matter.

2. Variables and constants

A variable is a named memory location whose stored value can change. Use descriptive names that start with a letter or underscore. Python code commonly uses snake case (current_balance) or camel case (currentBalance); this course uses descriptive snake case.

A constant is a named value that the program intends not to change. Python uses uppercase names with underscores, but the convention is not enforced by the language.

MINUTES_PER_HOUR = 60
hours_studied = 2
minutes_studied = hours_studied * MINUTES_PER_HOUR
print(minutes_studied)

The name connects the program to a memory location. A literal is the value itself; a variable or constant is a name that refers to stored data.

3. Assignment and dynamic typing

Assignment stores a value in a name. Python initializes a variable when its first assignment gives it a value; a later assignment changes the stored value. Python is dynamically typed: it infers a value’s type and permits reassignment to a different type.

score = 0          # initialize
score = 10         # assign a new value
score = score + 5  # read old value, store 15

response = None    # no value yet
response = 42      # integer
response = "forty-two"  # string

None is useful as an explicit “not available yet” value. Using a name before it is assigned produces an error.

4. Input and output

The input algorithm is: prepare a variable, display a clear prompt, collect the user’s response, and store it. input() returns text, so convert it when a numeric calculation is required. print() displays values, not the variable names.

name = input("What is your name? ")
age_text = input("How old are you? ")
age = int(age_text)

print("Welcome,", name)
print("Next year you will be", age + 1)

Classroom application

  1. Ask students to label every literal in a short program.
  2. Rewrite vague names such as x and thing as descriptive snake-case names.
  3. Trace a variable’s value after each assignment.
  4. Extend the example into a study-time calculator using MINUTES_PER_HOUR.

Common misconceptions

  • = assigns; it does not test mathematical equality.
  • "True" is a string; True is Boolean.
  • input() returns a string even when the user types digits.
  • Uppercase constants are a communication convention, not a read-only feature.

Lesson summary

Literals supply values, variables name changeable storage, constants document fixed assumptions, assignment updates state, and input/output connects a program to its user.