What you'll learn
- Store a value under a name with a single
= - Tell the four basic types apart:
str,int,float, andbool - Understand what Python actually does when you assign
- Choose names that make code readable
Concept
A variable is a name attached to a value. You create one with =: the name goes on the left, the value on the right. From then on, using the name means using the value. There is no separate step where you announce a variable exists; the first assignment creates it.
Every value has a type, and the type decides what you can do with it. Text is a str, whole numbers are int, numbers with a decimal point are float, and True or False are bool. Python works out the type from the value you wrote, so you never declare it, but it is always there and it always matters.
The mental model that saves beginners the most confusion is this: a variable is a label, not a box. When you write age = 29, Python creates the number 29 somewhere in memory and sticks the label age on it. When you later write age = 30, the label moves to a new number. Nothing inside the old value changes.

Because the type belongs to the value and not to the name, the same name can hold an int now and a str later. Python allows it, and it is almost always a bad idea, because anyone reading the code has to track what the name currently means. Keep each name to one kind of value for its whole life; when the data really changes kind, give it a new name.
Names follow a few rules. They can contain letters, digits, and underscores, they cannot start with a digit, and they are case-sensitive, so total and Total are different names. The convention in Python is lowercase words joined with underscores: first_name, items_sold. Good names describe what the value is for, not what type it is.
Code
Start by creating variables of each basic type and printing them. Notice that the strings need quotes and the numbers do not, and that True and False are capitalized.
# basics.py
name = "Mahmud"
age = 29
height_m = 1.75
is_student = False
print(name)
print(age)
print(height_m)
print(is_student)
Mahmud
29
1.75
FalseThe type() function tells you what kind of value a name currently holds. Run it on each variable and Python answers with the class of the value.
# types.py
name = "Mahmud"
age = 29
height_m = 1.75
is_student = False
print(type(name))
print(type(age))
print(type(height_m))
print(type(is_student))
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>Reassigning a name moves the label. Here age points at 29, then at 30, and a second name previous keeps pointing at the old value, which proves the 29 itself never changed.
# reassign.py
age = 29
previous = age
age = 30
print(previous)
print(age)
29
30Try it yourself
Create a file called about_me.py. Store your city as a str, the current year as an int, your height as a float, and whether you have programmed before as a bool. Print each one, then print its type. Check that the four types come out as you expect.
Common mistakes
- Forgetting the quotes around text.
name = Mahmudmakes Python look for a variable calledMahmud, which does not exist, so you get aNameError. Text always needs quotes. - Using a name before assigning it. Python reads top to bottom;
print(city)fails ifcity = "Dhaka"comes later in the file. - Writing
trueorfalsein lowercase. Python's booleans areTrueandFalsewith a capital letter; the lowercase versions are undefined names.
What's next
- A variable is a label on a value, created by
=; reassigning moves the label rather than changing the value. - The four basic types are
str,int,float, andbool, andtype()shows which one you have. - Names are lowercase with underscores, describe purpose, and are case-sensitive.
You can now hold values under names. Next you will do arithmetic with the numeric ones and meet the places where integers and floats behave differently from what you expect.