Vinay Garg Assistant Professor

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.

A dark code window showing name = "Vinay" and print(name) with the output Vinay

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

TypeExampleWhat it holds
str"Patiala"Text, always inside quotes
int44A whole number, no decimal point
float1.78A number with a decimal point
boolTrue, FalseA yes/no answer, capital first letter
NoneNoneNo 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

  1. The first line starts with #. That is a comment — Python ignores it. It is for you.
  2. name = "Vinay" creates a name called name and stores text in it. Without the quotes, Python would look for a variable called Vinay and complain.
  3. age = 44 stores a whole number. No quotes, so it is a number you can do maths with.
  4. height = 1.78 stores a decimal number. Any number with a dot is a float.
  5. is_teacher = True stores a boolean. True and False must start with a capital letter.
  6. nickname = None stores nothing on purpose. It is useful when a value will arrive later.
  7. 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. 2marks is an error.
  • Case matters: Age and age are two different variables.
  • Reserved words such as if, for, class and True cannot be names.

Common mistakes

  • age = "44" stores text. age + 1 then fails, because you cannot add a number to text. Leave the quotes off for numbers.
  • Writing true in small letters gives NameError. Python only knows True and False.
  • 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.

  1. Store your name, your age and your city in three variables, then print them on one line.
  2. Store the price of a book as a float and the number of copies as an int. Print both with their types.
  3. Predict what `type(5 / 2)` will be, then print it and check.
  4. 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.

Vinay Garg

Vinay Garg

Assistant Professor of Computer Science at Multani Mal Modi College, Patiala. Twenty-five years of teaching web programming, and a habit of building the things I teach about.