Data Types
Learn how Python stores different kinds of information so you can write clearer programs and begin working with real data.
Skill Level: Beginner
Prerequisites: Variables
Estimated Time: 20 minutes
Story Time
In the previous lesson, you learned that a variable can store information. Now the next question is: what kind of information is being stored?
Not all values are the same. A student's name is text, an exam score might be a whole number, an average could include decimals, and whether a student submitted work on time may be either true or false.
Python needs to recognize these differences so it can work with data correctly. Understanding data types is one of the first steps toward writing useful programs and analyzing real datasets.
What You’ll Learn
By the end of this lesson, you will be able to:
- Explain what a data type is.
- Identify four common Python data types.
- Create variables using different data types.
- Use the
type()function to inspect a value. - Recognize common beginner mistakes when working with data types.
What Is a Data Type?
A data type tells Python what kind of value is stored in a variable.
Look at this example:
name = "Emma"
age = 22
height = 165.5
is_student = True
These are all variables, but they do not store the same kind of information. One stores text, one stores a whole number, one stores a decimal number, and one stores a true-or-false value.
Four Common Data Types
In this course, you will frequently use these four Python data types:
| Data Type | Meaning | Example |
|---|---|---|
str | Text | "Emma" |
int | Whole number | 22 |
float | Decimal number | 165.5 |
bool | True or False | True |
These are enough to begin writing meaningful beginner programs and understanding how values behave in Python.
Strings
A string, written as str, stores text.
course = "Data Visualization"
print(course)
Output:
Data Visualization
Strings must be written inside quotation marks. Without quotation marks, Python will not treat the value as text.
Integers
An integer, written as int, stores a whole number.
students = 45
print(students)
Output:
45
Integers do not include decimal points. They are often used for counts, ages, and quantities.
Floating-Point Numbers
A floating-point number, written as float, stores a number with a decimal point.
temperature = 98.6
print(temperature)
Output:
98.6
Floats are useful for measurements, averages, percentages, and calculations where decimal values matter.
Boolean Values
A Boolean, written as bool, stores one of only two values:
TrueFalse
Example:
is_registered = True
print(is_registered)
Output:
True
Booleans are useful when a program needs to make decisions, check conditions, or track whether something is true or false.
Using type()
Python provides the type() function to show the data type of a value or variable.
name = "Emma"
print(type(name))
Output:
<class 'str'>
Another example:
score = 95
print(type(score))
Output:
<class 'int'>
This is a helpful tool when you are unsure how Python is interpreting a value.
Worked Example
The following example checks the type of several variables at once:
name = "Emma"
age = 22
height = 165.5
is_student = True
print(type(name))
print(type(age))
print(type(height))
print(type(is_student))
Output:
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
This example shows that different kinds of values can be stored in different variables, and Python keeps track of each type automatically.
Quick Check
Before moving on, try identifying the data type of each value on your own.
| Value | Data Type |
|---|---|
"Python" | ? |
100 | ? |
3.14 | ? |
False | ? |
You can test your answers in Google Colab using type().
Practice in the Notebook
After completing the activity in Google Colab, mark it as complete below.
Create the following variables in Google Colab:
- Your name
- Your age
- Your GPA, or any decimal value
- Whether you enjoy programming
Then use type() to display the data type of each variable.
Here is a model to get started:
name = "Ava"
age = 20
gpa = 3.8
enjoys_programming = True
print(type(name))
print(type(age))
print(type(gpa))
print(type(enjoys_programming))
Challenge
A library stores information about books. Create variables for:
- Book title
- Number of pages
- Price
- Whether the book is available
Then display both the values and their data types.
This is good practice because it helps you think about how real information is represented in a program.
Common Mistakes
Forgetting quotation marks
Incorrect:
city = Gainesville
Correct:
city = "Gainesville"
Without quotation marks, Python does not recognize Gainesville as a text value.
Writing numbers as text
age = "21"
This looks like a number, but Python treats it as text because it is inside quotation marks.
Correct:
age = 21
Incorrect Boolean capitalization
Incorrect:
registered = true
Correct:
registered = True
Python is case-sensitive, so Boolean values must begin with a capital letter.
Key Takeaways
- A data type tells Python what kind of value is stored in a variable.
- Four common beginner data types are
str,int,float, andbool. - Strings store text, integers store whole numbers, floats store decimals, and Booleans store
TrueorFalse. - The
type()function helps you check how Python is interpreting a value. - Understanding data types will help you write more accurate programs and prepare you for working with real datasets.
Self Evaluation
Check Your Understanding
1. What does a data type describe?
2. Which data type is used to store text in Python?
3. Which of the following is a floating-point number?
4. What does the `type()` function do?
5. Which Boolean value is written correctly in Python?
Continue Your Journey
In the next lesson, you will learn about type conversion, which allows you to change one data type into another when needed.