Python — Data Types
Python's built-in types
Python has a rich set of built-in types. Unlike statically typed languages, you don't declare types — Python figures them out:
x = 42 # int
y = 3.14 # float
name = "Ada" # str
active = True # bool
Use type() to check any value:
print(type(x)) # <class 'int'>
print(type(y)) # <class 'float'>
print(type(name)) # <class 'str'>
print(type(active)) # <class 'bool'>
Integers
Python integers have unlimited precision — no overflow:
x = 10 ** 100 # a googol
print(x) # works fine — Python handles arbitrarily large numbers
No long type needed — Python's int grows as big as memory allows.
a = 42
b = -100
c = 0
Use underscores for readability in large numbers:
population = 7_800_000_000 # 7.8 billion — underscores are ignored
Floats
pi = 3.14159
e = 2.71828
scientific = 1.5e10 # 1.5 × 10^10
Floats have limited precision — about 15-17 significant digits:
print(0.1 + 0.2) # 0.30000000000000004
This is IEEE 754, not a Python bug. For precise decimals (money), use the decimal module.
Complex numbers
z = 3 + 4j
print(z.real) # 3.0
print(z.imag) # 4.0
print(abs(z)) # 5.0 — magnitude
Python natively supports complex numbers with j as the imaginary unit. Used in scientific computing and signal processing.
Strings
name = "Ada"
greeting = 'Hello, world!'
multiline = """
This string
spans multiple
lines.
"""
Strings are immutable — once created, they can't be changed:
name = "Ada"
# name[0] = "B" # TypeError: 'str' does not support item assignment
name = "Bob" # creates a new string
String operations
first = "Hello"
second = "World"
combined = first + " " + second # "Hello World"
repeated = "Ha" * 3 # "HaHaHa"
length = len(combined) # 11
String methods
text = " Hello, World! "
print(text.strip()) # "Hello, World!" — remove whitespace
print(text.lower()) # " hello, world! "
print(text.upper()) # " HELLO, WORLD! "
print(text.replace("World", "Python")) # " Hello, Python! "
print(text.split(",")) # [' Hello', ' World! ']
print("hello".startswith("he")) # True
print("hello".endswith("lo")) # True
f-strings (formatted strings)
name = "Ada"
age = 36
print(f"Hello, {name}! You are {age} years old.")
print(f"Next year you'll be {age + 1}.")
print(f"Pi is approximately {3.14159:.2f}.")
f-strings are the modern way to embed expressions in strings. The :.2f formats to 2 decimal places.
Booleans
is_active = True
has_error = False
Booleans are a subclass of integers — True is 1 and False is 0:
print(True + True) # 2
print(True * 10) # 10
print(False + 1) # 1
Truthy and falsy values — Python treats certain values as true or false in conditions:
# Falsy values
bool(0) # False
bool(0.0) # False
bool("") # False
bool([]) # False
bool(None) # False
# Truthy values
bool(1) # True
bool("hello") # True
bool([1, 2]) # True
bool(3.14) # True
None — Python's null
result = None
print(result) # None
print(type(result)) # <class 'NoneType'>
# Check for None with 'is'
if result is None:
print("No value")
None represents the absence of a value. Always compare with is None, not == None.
Type conversion
# String to number
num = int("42") # 42
decimal = float("3.14") # 3.14
# Number to string
text = str(42) # "42"
pi_text = str(3.14) # "3.14"
# Float to int (truncates, doesn't round)
x = int(3.9) # 3
y = int(-3.9) # -3
# Round instead
z = round(3.9) # 4
The type() and isinstance() functions
x = 42
print(type(x) == int) # True
print(isinstance(x, (int, float))) # True — checks multiple types
isinstance() is preferred for type checking because it works with inheritance.
Mutable vs immutable
| Immutable | Mutable |
|---|---|
int, float, complex | list |
str | dict |
tuple | set |
frozenset | Custom objects |
Immutable types can't be changed after creation. This makes them safe to use as dictionary keys and in sets.
# Immutable — creates new value
x = 5
y = x
y += 1
print(x, y) # 5 6 — x unchanged
# Mutable — modifies in place
a = [1, 2, 3]
b = a
b.append(4)
print(a, b) # [1, 2, 3, 4] [1, 2, 3, 4] — both changed!
Mini Practice
- Create variables of each built-in numeric type and print their types
- Calculate
0.1 + 0.2— observe the floating-point imprecision - Use an f-string to format a number to 3 decimal places
- Test which values are truthy and falsy using
bool() - Create a list and a string — try to modify each and see which one fails
Next: working with strings in depth →
Related Topics
Frequently Asked Questions about Data Types
What is Data Types in Python?
Data Types is a fundamental concept in Python. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Data Types?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Data Types.
Why is Data Types important in Python?
Data Types is essential for Python development. Understanding this concept will help you write better code and solve real-world problems more effectively.