Variables
A variable is a name that points to a value. Once a value has a name, you can use the name again and again, and you can change the value in one place. Almost every program is built from variables.
Making a variable
Write the name, an equals sign, and the value.
name = "Asha"
age = 14
height = 1.52
print(name)
print(age)
print(height)Output
Asha
14
1.52The equals sign here does not mean "is equal to" as in maths. It means "take the value on the right and store it under the name on the left". Programmers call this assignment.
Notice that print(name) has no quotation marks. With quotation marks, Python would print the word name itself. Without them, it looks up what the name points to.
Changing a variable
A variable can be given a new value at any time. The old value is forgotten.
score = 10
print(score)
score = 25
print(score)
score = score + 5
print(score)Output
10
25
30The line score = score + 5 looks odd at first. Python works out the right side first, using the current value 25, gets 30, and then stores 30 back under the same name. This pattern is so common that there is a short form:
score = 30
score += 5
print(score)
score -= 10
print(score)Output
35
25Rules for names
- A name can use letters, digits and the underscore
_. - It cannot start with a digit.
class8is fine.8classis not. - It cannot contain spaces. Use an underscore:
total_marks. - Capital letters matter.
Marksandmarksare two different variables. - A few words belong to Python and cannot be used, for example
if,for,classandTrue.
Beyond the rules, choose names that say what the value is. price_per_kg is longer than p, but a reader understands it at once. The usual Python style is small letters with underscores between words.
Types of value
Every value has a type. The four you need first are:
| Type | Meaning | Example |
|---|---|---|
int | whole number | 14 |
float | number with a decimal point | 1.52 |
str | text, called a string | "Asha" |
bool | true or false | True |
The type function tells you what a value is.
print(type(14))
print(type(1.52))
print(type("Asha"))
print(type(True))Output
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>A variable is not fixed to one type. It simply points to whatever you last gave it. Still, changing the kind of thing a name holds halfway through a program confuses readers, so avoid it.
Several variables in one line
You can assign more than one variable at a time. This also gives a neat way to swap two values.
x, y = 5, 9
print(x, y)
x, y = y, x
print(x, y)Output
5 9
9 5Using a name before it exists
Python reads from top to bottom. If you use a name before giving it a value, or you misspell it, you get a NameError.
total = 50
print(totl)What Python says
Traceback (most recent call last):
File "example.py", line 2, in <module>
print(totl)
^^^^
NameError: name 'totl' is not defined. Did you mean: 'total'?The message even suggests the name you probably meant. See NameError explained for more.
Try it yourself
Exercise 1. Make variables for a product name, its price and the quantity bought. Print the total cost.
Show one answer
product = "Notebook"
price = 45
quantity = 6
print(product, "total:", price * quantity)Output
Notebook total: 270Exercise 2. Start with runs = 0. Add 4, then add 6, then print the result. Use the short form +=.
Show one answer
runs = 0
runs += 4
runs += 6
print(runs)Output
10Exercise 3. Two variables hold a = "tea" and b = "coffee". Swap them in one line and print both.
Show one answer
a = "tea"
b = "coffee"
a, b = b, a
print(a, b)Output
coffee teaExamples on this page were run with Python 3.14 on 12 November 2026. The output shown is the real output.
This is an independent learning site. It is not connected with or approved by the Python Software Foundation. "Python" is a trademark of the Python Software Foundation and is used here only to name the language.