Lesson 5: Variables and data types in Python
A variable is a name for a value. This first lesson stores one value of each type — text, whole number, decimal, True/False and None — and prints what Python calls each one.
In short
- A variable is a name that points at a value; you create it by assigning with `=`.
- Python has five types you will use every day: str, int, float, bool and None.
- You never declare a type. Python decides from the value you give it.
- `type()` tells you which type a value is, which is the fastest way to fix a confusing bug.
5
data types covered: str, int, float, bool, None
Python docs
1
line to check any value: type(value)
Python docs
What a variable is
A variable is a name you give to a value so that you can use that value again later. In Python you create one by writing a name, an equals sign, and the value: age = 44. That is the whole rule.
You do not tell Python what kind of value it is. It looks at the right-hand side and remembers. This is why people call Python a dynamically typed language — the type belongs to the value, not to the name.
The five types you will use
| Type | Example | What it holds |
|---|---|---|
str | "Patiala" | Text, always inside quotes |
int | 44 | A whole number, no decimal point |
float | 1.78 | A number with a decimal point |
bool | True, False | A yes/no answer, capital first letter |
None | None | No value yet — an empty placeholder |
The program
# variables.py — one value of each type, then what Python calls it
name = "Vinay" # str: text in quotes
age = 44 # int: whole number
height = 1.78 # float: has a decimal point
is_teacher = True # bool: True or False
nickname = None # None: nothing stored yet
print(name, type(name))
print(age, type(age))
print(height, type(height))
print(is_teacher, type(is_teacher))
print(nickname, type(nickname))
How it works, step by step
- The first line starts with
#. That is a comment — Python ignores it. It is for you. name = "Vinay"creates a name callednameand stores text in it. Without the quotes, Python would look for a variable called Vinay and complain.age = 44stores a whole number. No quotes, so it is a number you can do maths with.height = 1.78stores a decimal number. Any number with a dot is a float.is_teacher = Truestores a boolean.TrueandFalsemust start with a capital letter.nickname = Nonestores nothing on purpose. It is useful when a value will arrive later.print(name, type(name))prints two things separated by a space: the value, then the type.
What it prints
Vinay <class 'str'> 44 <class 'int'> 1.78 <class 'float'> True <class 'bool'> None <class 'NoneType'>
Names you are allowed to use
- Letters, digits and underscores only:
student_name,roll2,_temp. - Never start with a digit.
2marksis an error. - Case matters:
Ageandageare two different variables. - Reserved words such as
if,for,classandTruecannot be names.
Common mistakes
age = "44"stores text.age + 1then fails, because you cannot add a number to text. Leave the quotes off for numbers.- Writing
truein small letters givesNameError. Python only knowsTrueandFalse. - Using a name before assigning it also gives
NameError. Create the variable first, then use it.
Practice
Try these before moving on. Each one uses only what this lesson covered.
- Store your name, your age and your city in three variables, then print them on one line.
- Store the price of a book as a float and the number of copies as an int. Print both with their types.
- Predict what `type(5 / 2)` will be, then print it and check.
- Create a variable `x` with the value 10, then change it to "ten" and print `type(x)` before and after.
Sources
Frequently asked questions
What is a variable in Python?
A name that points at a value. You create it with name = value, and from then on the name stands for that value until you assign something else to it.
Do I have to declare a data type in Python?
No. Python reads the value you assign and gives the variable that type. age = 44 makes an int, and the same name can later hold text without any error.
What is the difference between int and float?
An int is a whole number such as 44 or -7. A float has a decimal point, such as 1.78 or 0.5. Division with / always produces a float, even when the answer looks whole.
What does None mean?
It means there is no value yet. It is different from 0, from an empty string, and from False. Use it when a value will be filled in later.