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
intis a whole number, like 50 or 7.floatis a number with a decimal point, like 2.5.stris text, like "Asha". It is short for string.
Use it
You can ask Python for the type of any value with type().
print(type(50))
print(type(2.5))
print(type("Asha"))
<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.
count = 3
price = 2.5
name = "Asha"
print(type(count))
print(type(price))
print(type(name))
<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?
- A whole number has no decimal point.
- 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?
- A float has a decimal point.
- 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?
- Text goes inside quotes.
- 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.
print(50 + 1)
print("50" + "1")
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.