Variables in Python
A variable is a named reference to a value stored in memory. In Python, variables are dynamically typed — you don't need to declare their type. This chapter covers how to create, name, and use variables effectively with real examples.
What is a Variable?
Think of a variable as a container that holds data. When you write name = "Ankit", Python creates a string object and binds it to the name. You can reassign variables freely.
Creating a Variable
name = "Ankit"
age = 22
is_student = True
percentage = 89.5
# Multiple assignment
x, y, z = 10, 20, 30
# Chained assignment
a = b = c = 100
Variable Naming Rules
- Names can contain letters, digits, and underscores.
- Must begin with a letter or underscore.
- Case-sensitive (
age≠Age). - Cannot use reserved keywords like
if,for. - Use descriptive names —
student_nameis better thansn.
Python is dynamically typed — the same variable can hold an integer and later a string. Avoid reassigning different types to the same variable in production code.
Create three variables: your name, your age, and your city. Print them in a single sentence using an f-string. Then calculate your age after 5 years.
Quick Revision
- Variables are named references to objects.
- Use
=for assignment. - Case-sensitive, cannot start with digit.
- Python is dynamically typed.
- Descriptive names improve readability.
Interview Questions
Q: Difference between a variable and constant in Python?
A: Python doesn't have true constants. By convention, names in ALL_CAPS (MAX_SIZE = 100) are treated as constants.
Q: Can a Python variable name start with a number?
A: No. It must begin with a letter or underscore. Starting with a digit causes a SyntaxError.
Want to Master Python?
Join our Python course in Varanasi — practical classes and career support at our Sarnath centre.