Home / Topic Library / Python / Data Types
🐍 Python Topic Library

Python Data Types — The Complete Guide

Every value in Python has a type, and most beginner bugs come from mixing them up. Master the five core types and conversions between them, and half your error messages disappear.

⚡ Quick Answer

Python's core types: int (whole numbers), float (decimals), str (text), bool (True/False) and None (no value). Convert between them with int(), float(), str() and bool().

§The five core types

core_types.py
whole   = 42          # int  — unlimited size!
decimal = 3.14        # float
text    = "hello"     # str
flag    = True        # bool (capital T!)
nothing = None        # NoneType — "no value here"

print(type(whole))    # <class 'int'>

§Type conversion (casting)

Mixing types is the #1 beginner error. Convert explicitly:

conversion.py
age = 12
msg = "I am " + str(age)     # int → str before joining text

answer = input("Number: ")    # input() ALWAYS returns str
num = int(answer)             # str → int before math

price = float("3.99")         # str → float
whole = int(3.99)             # float → int TRUNCATES: 3, not 4!
rounded = round(3.99)         # proper rounding: 4

§Checking types

checking.py
x = 3.14
print(type(x))               # <class 'float'>
print(isinstance(x, float))  # True — preferred in real code
print(isinstance(x, (int, float)))  # True — check several at once

§Truthiness — what counts as False

In conditions, Python treats some values as False automatically: 0, 0.0, "", [], {}, None. Everything else is truthy.

truthiness.py
items = []
if items:
    print("has items")
else:
    print("empty!")          # this runs — empty list is falsy

name = ""
print(bool(name))            # False
print(bool("hi"))            # True

§Float precision — the classic surprise

Floats are stored in binary, so some decimals are approximate. Never compare floats with ==.

float_trap.py
print(0.1 + 0.2)             # 0.30000000000000004 (!)
print(0.1 + 0.2 == 0.3)      # False!

import math
print(math.isclose(0.1 + 0.2, 0.3))   # True — the right way

§Common Mistakes to Avoid

🚨 Watch out for these
  • Doing math on input() results — input() always returns a string — input() + 1 is a TypeError; wrap with int() first
  • Comparing floats with == — 0.1 + 0.2 != 0.3 in floating point; use math.isclose() instead
  • Expecting int() to round — int(3.99) is 3 — it truncates. Use round() for rounding

§Frequently Asked Questions

What is the difference between int and float?

int stores whole numbers with unlimited precision; float stores decimals in 64-bit binary, which makes some values approximate.

What does None mean in Python?

None is Python's 'no value' — the default return of functions that don't return anything, and the standard placeholder for missing data.

Why does 0.1 + 0.2 not equal 0.3 in Python?

Floats are stored in binary and 0.1 can't be represented exactly, so tiny rounding errors appear. Use math.isclose() to compare.

How do I convert a string to a number?

Use int("42") for whole numbers or float("3.14") for decimals — it raises ValueError if the text isn't numeric.

🎓 Want to master Python properly?This reference covers one topic — the full free course takes you from zero to projects, with quizzes and a certificate.
Start the Free Python Course →

§Related Python Topics

← VariablesStrings →