Skip to main content
Anime study buddy

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

What is a variable?

A variable is a name that refers to a value stored in memory.
age = 19
name = "Dhruv"
Here, age and name are variable names.

Why do we need variables?

Without variables, you would repeat raw values many times. Variables make programs readable and maintainable.

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

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.

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