Skip to main content

Learning outcomes

By the end of this chapter, you should be able to:
  • define a variable in simple terms
  • write valid variable names
  • assign and reassign values
  • explain dynamic typing in Python

30-minute recording plan

  • 0-6 min: what variables are and why needed
  • 6-12 min: assignment statement and reassignment
  • 12-18 min: variable naming rules and style
  • 18-24 min: dynamic typing basics
  • 24-30 min: exam-focused recap and practice

Chapter snapshot

  • Think of a variable as a label attached to a value.
  • Labels make code readable: total_marks is clearer than raw numbers repeated everywhere.
  • In Python, a variable can point to different value types over time.

What is a variable?

A variable is a name that refers to a value stored in memory. You can read it as: “store this value in this name.”
age = 19
name = "Dhruv"
Here, age and name are variable names.

Why do we need variables?

  • avoid repeating fixed values
  • update a value once and reuse it everywhere
  • improve readability and debugging
  • make formulas and logic easier to understand

Assignment statement

Python uses = for assignment.
x = 10
x = x + 5
print(x)  # 15
= means “store value in variable”. It does not mean “equals” in mathematics.

Variable naming rules

  • use letters, digits, and _
  • must not start with a digit
  • no spaces in names
  • names are case-sensitive (marks and Marks are different)
  • do not use keywords (if, for, class, …)
Good examples: total_marks, student_name, is_passed

Naming style (best practice)

  • use snake_case in Python: final_score, unit_price
  • avoid one-letter names except short math loops (i, j)
  • choose names that show meaning, not just type

Dynamic typing in Python

In Python, variable type is determined at runtime.
x = 10        # int
x = "ten"     # str
This is allowed, but in exams and real code, avoid unnecessary type changes.

Mini showcase: monthly expense update

allowance = 5000
food = 1450
travel = 900
notes = 350

spent = food + travel + notes
left = allowance - spent

print(f"Spent: {spent}")
print(f"Left: {left}")
Output idea:
  • Spent: 2700
  • Left: 2300

Exam-focused points

  • variable: named memory location/reference
  • assignment operator is =
  • equality comparison uses ==
  • Python is dynamically typed

Practice questions

  1. Write 5 valid and 5 invalid variable names.
  2. Predict output:
x = 7
x = x * 2
print(x)
  1. Correct the errors:
2name = "Aman"
my marks = 90
if = 3