Common Data Types
Not all data is the same. Text behaves differently than numbers, and numbers behave differently than true/false values. Understanding data types helps you write code that works correctly and avoid confusing errors.
The Four Basic Types
Python has four fundamental data types you'll use constantly:
Strings hold text – anything in quotes:
greeting = "Hello, World"
letter = 'A'
Integers are whole numbers without decimals:
count = 42
temperature = -5
Floats are numbers with decimal points:
price = 19.99
pi = 3.14159
Booleans are true/false values:
is_active = True
has_permission = False
Why Types Matter
Types determine what operations make sense. You can add numbers together, but adding a number to text doesn't work the way you might expect:
print(5 + 3) # 8 (math addition)
print("5" + "3") # "53" (string concatenation)
print(5 + "3") # Error! Can't add int and str
Python won't guess what you mean – it needs compatible types.
Dynamic Typing
Python figures out types automatically based on the value you assign. You don't declare types explicitly:
x = 42 # Python knows this is an integer
x = "hello" # Now x is a string
This flexibility is convenient but means you need to track what type each variable holds.
Checking Types
When you're unsure what type a value is, use type():
text = "Hello"
count = 42
price = 19.99
active = True
print(type(text)) # <class 'str'>
print(type(count)) # <class 'int'>
print(type(price)) # <class 'float'>
print(type(active)) # <class 'bool'>
This is especially useful when debugging – unexpected types often cause errors.
Converting Between Types
Sometimes you need to convert data from one type to another:
# String to integer
age_text = "25"
age_number = int(age_text)
# Integer to string
count = 42
count_text = str(count)
# String to float
price_text = "19.99"
price = float(price_text)
You'll use these conversions frequently, especially when handling user input.