Python Data Types Explained for Beginners
You already know you can print numbers and text in Python. Now it's time to understand what those values actually are. In this guide, you'll learn the four basic data types (integers, floats, strings and booleans), what makes each one different, and how to check a value's type with type(). Every example comes with its output, so open a Python editor and type along as you read.
What Are Data Types in Python?
Python has four basic data types you'll use all the time.
| Data type | Short name | What it holds | Examples |
|---|---|---|---|
| Integer | int |
Whole numbers | 5, 0, -3 |
| Float | float |
Numbers with a decimal point | 3.09, 1.2 |
| String | str |
Text | "Ashley", 'hello' |
| Boolean | bool |
True or False | True, False |
Let's look at each one.
Integers (int)
You know you can print a number in Python like this.
print(5)
Output
Here, 5 is a number. More specifically, it's a whole number with no decimal point. In Python, numbers like this are called integers. Integers include positive whole numbers, zero and negative numbers.
print(1)
print(0)
print(-8)
Output
Floating-Point Numbers (float)
Numbers with a decimal point are called floating-point numbers, or floats for short. Any number with a decimal point is a float in Python.
print(3.09)
print(1.2)
Output
Integers and floats are both numeric data types, which means you can do maths with them.
Strings (str)
Text in Python is called a string. To create a string, wrap the text in quotes. Double quotes and single quotes work exactly the same way.
print("Ashley")
print('Ashley')
Output
Why "5" Is Not the Same as 5
When you put a number inside quotes, it's no longer a number. It becomes text. It looks the same when printed, but you can't do calculations with it.
print(5 + 5)
print("5 + 5")
Output
Without quotes, Python does the maths. With quotes, Python treats it as text and prints it exactly as written.
Booleans (bool)
A boolean is used when the answer is a simple yes or no. For example, is he a student? It's either yes or no, nothing in between.
A boolean can only have two values, True (yes) and False (no).
print(True)
print(False)
Output
Watch out for two things.
1. No quotes. If you put quotes around True or False, Python treats them as plain text, not booleans.
2. Capital letters. Python is case-sensitive, so it must be True and False, not true or false.
print("True")
print("False")
Output
The output looks identical, but behind the scenes it's completely different. "True" is just a word, the same way "5 + 5" was just text. Right now that doesn't seem to matter. But when you start using booleans to make decisions in your code, Python treats a real boolean and a piece of text very differently.
How to Check the Data Type with type()
Python has a handy built-in function called type() that tells you the data type of any value. Put the value inside type(), then print it.
print(type(5))
Output
int is short for integer, so 5 is an integer. Don't worry about the word "class" for now. You'll learn about it later.
Now let's check the other types.
print(type(3.09))
print(type("5"))
print(type(True))
print(type("True"))
Output
floatmeans3.09is a floating-point number.stris short for string. Even though5is a number, the quotes make Python treat"5"as text.boolis short for boolean, soTruewithout quotes is a real boolean."True"with quotes is a string, not a boolean.
Whenever you're not sure what type a value is, type() will tell you.
Quick Recap
- Python has four basic data types: integer (
int), float (float), string (str) and boolean (bool). - Integers are whole numbers, and floats are numbers with a decimal point.
- Strings are text inside quotes. Quotes turn any value into a string, so
"5"and"True"are text. - Booleans are
TrueorFalse, with a capital letter and no quotes. type()tells you the data type of any value.
Practice Exercises
Reading isn't enough. Try these yourself before checking the answers.
- Print a whole number, a decimal number, your name and
True. - Use
type()to check the data type of each one. - Print
10 + 5without quotes and with quotes. Notice the difference. - Check the data type of
"False".
Answers
print(7)
print(2.5)
print("Ashley")
print(True)
print(type(7))
print(type(2.5))
print(type("Ashley"))
print(type(True))
print(10 + 5)
print("10 + 5")
print(type("False"))
Output
FAQs
What are the 4 basic data types in Python?
The four basic data types are integers (whole numbers), floats (decimal numbers), strings (text) and booleans (True or False).
How do I check the data type of a variable in Python?
Use the type() function, for example print(type(age)). It shows the type, such as <class 'int'>.
What's the difference between True and "True" in Python?
True without quotes is a boolean. "True" with quotes is a string, just a piece of text that happens to spell the word.
Conclusion
You now know the four basic Python data types, what makes each one different, and how to check any value's type with type(). Every value you'll ever use in Python has a data type, so this is one of the most important foundations to get right.
Work through the practice exercises to make it stick. Next, you'll learn about variables, so you can store these values and use them again later.
Have anything to say about this lesson?
Your feedback helps improve these tutorials. If something was confusing or missing, let us know.