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.
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().
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'>
Mixing types is the #1 beginner error. Convert explicitly:
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: 4x = 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
In conditions, Python treats some values as False automatically: 0, 0.0, "", [], {}, None. Everything else is truthy.
items = []
if items:
print("has items")
else:
print("empty!") # this runs — empty list is falsy
name = ""
print(bool(name)) # False
print(bool("hi")) # TrueFloats are stored in binary, so some decimals are approximate. Never compare floats with ==.
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
input() + 1 is a TypeError; wrap with int() firstint stores whole numbers with unlimited precision; float stores decimals in 64-bit binary, which makes some values approximate.
None is Python's 'no value' — the default return of functions that don't return anything, and the standard placeholder for missing data.
Floats are stored in binary and 0.1 can't be represented exactly, so tiny rounding errors appear. Use math.isclose() to compare.
Use int("42") for whole numbers or float("3.14") for decimals — it raises ValueError if the text isn't numeric.