DocsHub
Basics

Variables

Learn how to store and name data in Python using variables.

Variables

A variable is a name that points to a value stored in memory. Think of it like a labeled box — you put something inside, and whenever you need it, you use the label to get it back.

In Python, creating a variable is simple. You just write a name, an equals sign, and a value:

name = "Ali"
age = 22

That's it. No let, no var, no const — just the name and the value.


How assignment works

The = in Python is not "equals" in the mathematical sense. It means assign this value to this name. The right side is always evaluated first, then stored under the name on the left.

x = 10        # store the number 10 under the name x
y = x + 5     # evaluate x + 5 (which is 15), store it under y

print(x)      # 10
print(y)      # 15

You can also reassign a variable at any time. Python does not care what the previous value was:

score = 0
print(score)   # 0

score = 100
print(score)   # 100

score = score + 10
print(score)   # 110

Naming rules

Not every name is valid in Python. Here are the rules:

Must follow:

  • Can only contain letters, numbers, and underscores _
  • Cannot start with a number
  • Cannot be a Python keyword (like if, for, while, True, etc.)
# Valid names
name = "Ali"
age_1 = 25
_private = True
totalScore = 99

# Invalid names
1name = "Ali"       # starts with a number — SyntaxError
my-name = "Ali"     # hyphens not allowed — SyntaxError
for = 10            # 'for' is a keyword — SyntaxError

Best practice — use snake_case:

Python convention is to use lowercase words separated by underscores. This is called snake_case.

# Good — Pythonic style
first_name = "Ali"
total_score = 100
is_logged_in = True

# Works but not conventional
firstName = "Ali"     # this is camelCase — common in JavaScript
TotalScore = 100      # this looks like a class name in Python

Python is case-sensitive. name, Name, and NAME are three completely different variables.


Assigning multiple variables

Python lets you assign multiple variables on one line, which can make your code cleaner.

Same value to multiple variables:

x = y = z = 0
print(x, y, z)   # 0 0 0

Different values in one line:

name, age, city = "Ali", 22, "Lahore"
print(name)   # Ali
print(age)    # 22
print(city)   # Lahore

This is called unpacking. The number of names on the left must match the number of values on the right, otherwise Python raises an error.

a, b = 1, 2, 3   # ValueError: too many values to unpack

Variable types are dynamic

In Python, you do not declare what type a variable holds. Python figures it out automatically — and you can even change it:

x = 10        # x is an integer
x = "hello"   # now x is a string — this is fine in Python
x = True      # now x is a boolean

This is called dynamic typing. It gives you flexibility, but it also means you need to be careful — if you accidentally put the wrong type of value in a variable, Python will not warn you until something breaks.

Dynamic typing is powerful but can cause bugs. If you pass a string where a number is expected, Python will raise a TypeError at runtime — not before. As you grow, you will learn to use type hints to make your intentions clear.


Checking a variable's type

You can use the built-in type() function to see what type of value a variable holds:

name = "Ali"
age = 22
score = 98.5
is_active = True

print(type(name))       # <class 'str'>
print(type(age))        # <class 'int'>
print(type(score))      # <class 'float'>
print(type(is_active))  # <class 'bool'>

You do not need to memorize the output format right now — just know that type() exists and is useful for debugging.


Deleting a variable

You can delete a variable using del. After that, trying to use it raises a NameError:

x = 42
print(x)   # 42

del x
print(x)   # NameError: name 'x' is not defined

You will not need this often, but it is good to know it exists.


Constants

Python has no built-in way to make a variable constant (unchangeable). The convention is to write the name in ALL_CAPS to signal to other developers that this value should not be changed:

MAX_SCORE = 100
PI = 3.14159
DB_HOST = "localhost"

Python will not stop you from changing these — it is just a signal by convention.

If you need real constants that cannot be changed, Python's typing module has a Final type hint for this — but that is an advanced topic covered later.


Summary

ConceptExample
Create a variablex = 10
Reassignx = 20
Multiple assignmenta, b = 1, 2
Check typetype(x)
Deletedel x
Constant conventionMAX_SIZE = 100

On this page