Python Data Types: Knowing Your Numbers from Your Text
Introduction
In Python, you can't treat every piece of data the same way. You can do math with numbers, but you can't do math with words.
Python uses data types to know exactly what kind of information it is dealing with.
The Big Four Data Types
Here are the four most important types you will use every day:
- Integer
int: Whole numbers with no decimals. - Float
float: Numbers with decimals. - String
str: Text that must be inside quotes. - Boolean
bool: True or False logic.
Let's Look at the Code
You can see these in action by using the type() function. This function tells you what type of data you are using.
age = 25 # Integerprice = 19.99 # Floatname = "Alex" # Stringis_active = True # Booleanprint(type(age))print(type(price))print(type(name))print(type(is_active))
When you run this code, Python checks each value and prints its data type.
Why Does This Matter?
If you try to combine the wrong types, Python will stop you.
# This will cause an error!print("I am " + 25)
Python sees "I am " as a string and 25 as an integer.
You cannot add them directly because they are not the same type. You have to convert them first, which we will cover in a future lesson.
Summary Table
| Type | Python Name | Example |
|---|---|---|
| Text | str | "Hello" |
| Whole Number | int | 10 |
| Decimal | float | 10.5 |
| Logic | bool | True |
Conclusion
Data types help Python understand what kind of value it is working with.
Once you understand integers, floats, strings, and booleans, you can write programs that handle information more correctly and safely.
