Python Variables and Data Types Explained for Beginners (int, float, str, bool)
P2.2 Python Data Types: int, float, str and bool
Why. Python treats "500" (text) and 500 (number) very differently. Many data bugs happen because a number is stored as text. Knowing types helps you catch them.
What. Every value has a type.
- int — whole numbers:
42,-7. - float — numbers with a decimal point:
19.99. - str (string) — text inside quotes:
"London". - bool — only
TrueorFalse.
| Value | Type | Real example |
|---|---|---|
| 250 | int | parcels in a van |
| 72.5 | float | patient weight in kg |
| "Madrid" | str | branch city |
| True | bool | is the invoice paid? |
How to read this table: each row is one value. The Type column is what type() would print.
How.
Check the type of any value
- Put the value or variable inside
type(). - Print it.
- Read the word after
class.
Worked example A — Check four types.
Step 1 — What we do. We store one value of each type for a bank customer in Toronto and print the types.
Step 2 — Type and run this code.
age = 34
balance = 2500.75
name = "Emma"
is_active = True
print(type(age))
print(type(balance))
print(type(name))
print(type(is_active))
Step 3 — Output you should see.
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
Step 4 — What it means. Each line shows the type. In pandas you will see the same idea as column dtypes: int64, float64, object (text) and bool.
Worked example B — Number vs text that looks like a number.
Step 1 — What we do. We compare the number 500 with the text "500".
Step 2 — Type and run this code.
a = 500
b = "500"
print(a + a)
print(b + b)
Step 3 — Output you should see.
1000
500500
Step 4 — What it means. For numbers + adds. For text + joins. So "500" + "500" gives "500500". If a sales total looks huge and strange, check if the column is text.
Worked example C — True and False are numbers too.
Step 1 — What we do. Three invoices: paid, not paid, paid. We count the paid ones.
Step 2 — Type and run this code.
paid = [True, False, True]
print(sum(paid))
Step 3 — Output you should see.
2
Step 4 — What it means. True counts as 1 and False as 0. So sum counts the True values: 2 invoices are paid. pandas uses this trick all the time.
Ravindra Bagale's Tip
Always check types right after loading data. One type() (or later df.dtypes) saves you from wrong totals.
Ravindra Bagale's Tip – मराठी
Data load केल्यावर लगेच types तपासा. एक type() (पुढे df.dtypes) चुकीच्या totals पासून वाचवतो.
Ravindra Bagale's Tip – हिंदी
Data load करते ही types check करो. एक type() (आगे df.dtypes) गलत totals से बचाता है.