logo
03

Variables & Assignment

⏱️ 25 min

Python Variables: Name Your Data or Your Code Becomes a Mess

What You Might Be Wondering

"A variable is just a name, right? Why is this a big deal?"

Because without clear variable names, your code is a warehouse with no labels. It runs today. It's chaos next month.

One-Line Definition

A variable is a "name -> value" binding that lets you store and update data in your program.

Real-Life Analogy

Variables are like labels on storage boxes. The box holds the data, and the label is how you find and change it quickly.

Minimal Working Example

name = "Alice"
days = 0

print(name)
print(days)

days = days + 1
print(days)  # 1

Key Concepts

  • = is assignment, not comparison
  • Left side is the variable name, right side is the value
  • You can rebind the same name to a new value (updating state)

Quick Type Overview

username = "jr_student"  # str
score = 95                # int
price = 19.9              # float
is_vip = False            # bool

print(type(username))
print(type(score))

Naming Rules (Start with These 3)

  • Use lowercase + underscores: learning_days
  • Names should express business meaning: user_score beats x1
  • Don't use reserved keywords as variable names (class, for, etc.)

Quick Quiz (3 min)

  1. Define name, city, goal and print them.
  2. Increment learning_days three times in a row.
  3. Use type() to print each variable's type.

Quiz Rubric & Grading Criteria

  • Direction: write runnable code that covers the core requirements and edge cases from the prompt.
  • Criterion 1 (Correctness): main flow produces correct results, key branches execute.
  • Criterion 2 (Readability): clear variable names, no excessive nesting.
  • Criterion 3 (Robustness): basic protection against empty values, type errors, or unexpected input.

Take-Home Task

Build a "study check-in" script:

  • A variable for today's study hours
  • A variable for total days
  • Print "I've studied for X days, Y hours today"

Acceptance Criteria

You can independently:

  • Create, read, and update variables
  • Tell the difference between assignment = and comparison ==
  • Write variable names that are actually readable

Common Errors & Debugging Steps (Beginner Edition)

  • Error message looks like gibberish: read the last line for the error type (TypeError, NameError, etc.), then trace back to the offending line.
  • Not sure what a variable holds: drop a temporary print(variable, type(variable)) to check.
  • Changed code but nothing happened: make sure you saved the file, you're running the right file, and your terminal environment (venv) is correct.

Common Misconceptions

  • Misconception: variable names don't matter as long as the code runs.

  • Reality: naming quality directly determines long-term maintainability.

  • Misconception: = means "equals."

  • Reality: = is assignment. Comparison uses ==.