Look at this code:
name = "Sam"
age = 20
height = 5.8
is_student = True
We just created four different values and stored them in four variables. Here:
"Sam"is text.20is a whole number.5.8is a decimal number.Trueis a true or false value (Boolean).
These are different data types.
What is a Data Type?
Definition: A data type tells Python what kind of data a value is, such as text, a decimal number, or a list.
In many programming languages, you must declare a variable's data type when you create it. For example, whether it stores text, an integer, or some other type.
But in Python, you don't have to.
Python automatically determines the data type from the value you assign.
Python Built-in Data Types
Python has 14 built-in data types . To make them easier to understand, they are grouped into categories based on what they do.
| Category | Data Type | Description | Level |
|---|---|---|---|
| Text | str |
Text / strings (e.g., "Hello") |
Core |
| Numeric | int |
Whole numbers (e.g., 10) |
Core |
float |
Decimal numbers (e.g., 3.14) |
Core | |
complex |
Complex numbers for advanced math | Advanced | |
| Boolean | bool |
True or False values | Core |
| Sequence | list |
Ordered, changeable collection | Core |
tuple |
Ordered, unchangeable collection | Core | |
range |
A sequence of numbers | Core | |
| Mapping | dict |
Key-value pairs | Core |
| Set | set |
Unordered collection of unique values | Core |
frozenset |
Unchangeable version of a set | Advanced | |
| Binary | bytes |
Sequence of bytes | Advanced |
bytearray |
Changeable sequence of bytes | Advanced | |
memoryview |
Memory access for bytes | Advanced | |
| None | NoneType |
Represents the absence of a value | Core |
Note: As you can see, there are a few advanced data types (
complex,frozenset,bytes,bytearray, andmemoryview). If you are a beginner, you do not need to worry about these right now, focus only on the "Core" types.
Do not try to memorize this table. You will learn each of these in detail as you build more Python programs.
Why Data Types Matter
Look at these two print statements:
The first outputs 30. The second outputs Hello World. The + operator adds numbers, but it joins text together.
Now, predict the output of this code:
print("10" + "20")
If you guessed 30, you made a common beginner mistake. The output is:
1020
Why?
Because "10" and "20" are inside quotes, Python treats them as strings, not numbers. It joins them together instead of adding them.
This is why data types matter. The data type determines what operations Python performs on a value.
Checking Data Types
To check the data type of a Python variable, use the type() function.
Dynamic Typing
You may have heard that Python is a dynamically typed language. What does that mean?
In many programming languages, a variable is like a rigid box. if you create it to hold text, it can never hold numbers. Python is different.
Look at this code:
The same variable x is first used for an integer and then used for a string.
Why does this work? In Python, variables don't have fixed data types; only the values do. Think of a variable as a sticky note or name tag. First, Python sticks the x name tag on the integer 10. Then, it moves the x name tag to the string "Sam". Because the variable is just a label pointing to data, it can point to a new data type at any time. This behavior is called dynamic typing.