Lesson 2 of 5 · Variables · 2 min read · Updated

Data types in Python

Every value in Python has a type: whole number, decimal number or text. Learn to see the type of a value with type().

Understand

Every value in Python has a type. The type tells Python what kind of value it is.

Python has many types. A whole number, a number with a decimal point, and a piece of text are three of them. These are the ones to learn first.

Three types to know first

  • int is a whole number, like 50 or 7.
  • float is a number with a decimal point, like 2.5.
  • str is text, like "Asha". It is short for string.

Use it

You can ask Python for the type of any value with type().

python
print(type(50))
print(type(2.5))
print(type("Asha"))
Output
<class 'int'>
<class 'float'>
<class 'str'>

Python shows the type inside the message. Read the short name between the single quotes: int, float or str.

A variable has the type of the value it holds.

python
count = 3
price = 2.5
name = "Asha"
print(type(count))
print(type(price))
print(type(name))
Output
<class 'int'>
<class 'float'>
<class 'str'>

Practice

Fill in the blank with a whole number so that Python prints its type as int.

To try it, copy the code into a file on your computer and run it. Your output should match the expected output below.

Expected output
<class 'int'>
Need a hint?
  1. A whole number has no decimal point.
  2. Write print(type(7)).
Show a solution
print(type(7))

This code prints int. Change the value of price so that Python prints float.

To try it, copy the code into a file on your computer and run it. Your output should match the expected output below.

Expected output
<class 'float'>
Need a hint?
  1. A float has a decimal point.
  2. Write price = 50.5.
Show a solution
price = 50.5
print(type(price))

A PIN code is a code, not a number to calculate with. Change the value so that Python prints str.

To try it, copy the code into a file on your computer and run it. Your output should match the expected output below.

Expected output
<class 'str'>
Need a hint?
  1. Text goes inside quotes.
  2. Write pin = "625001".
Show a solution
pin = "625001"
print(type(pin))

What is the type of "50"?

Why it works

The type decides what a value can do. Two whole numbers are added as numbers. Two pieces of text are joined together.

python
print(50 + 1)
print("50" + "1")
Output
51
501

This is why a PIN code is better as text. You never add two PIN codes, and a PIN code can start with a zero. Text keeps the zero.

Create a free account to save your progress, or sign in.