Click "Run Code" to see output here...
Try Examples:
Python Data Types
Click any card to explore more details about each data type. Python has 14 built-in data types organized into categories.
Numeric Types
Whole numbers, positive or negative, without decimals
x = 42
y = -17
big = 10000000000
Key Features:
Unlimited precision in Python 3. Can be positive, negative, or zero. Supports all arithmetic operations.
Common Operations:
+, -, *, /, //, %, ** (power), abs(), int()
Numbers with decimal points or scientific notation
pi = 3.14159
price = 19.99
sci = 1.5e-10
Key Features:
64-bit double precision. Can represent very large or very small numbers using scientific notation (e.g., 1.5e10).
Watch Out:
Floating point arithmetic can have precision issues: 0.1 + 0.2 = 0.30000000000000004
Numbers with real and imaginary parts
z = 3 + 4j
print(z.real) # 3.0
print(z.imag) # 4.0
Key Features:
Written as a + bj where j is the imaginary unit. Used in scientific computing, signal processing, and mathematics.
Attributes:
.real returns real part, .imag returns imaginary part, .conjugate() returns complex conjugate
Text Type
Sequences of characters (text data)
name = "Python"
msg = 'Hello, World!'
multi = """Line 1
Line 2"""
Key Features:
Immutable (cannot be changed after creation). Can use single, double, or triple quotes. Supports indexing and slicing.
Common Methods:
.upper(), .lower(), .strip(), .split(), .join(), .replace(), .find(), .format(), f-strings
Boolean Type
Represents True or False values
is_valid = True
is_empty = False
result = 10 > 5 # True
Key Features:
Only two values: True and False (capitalized). Used in conditional statements and logical operations.
Falsy Values:
False, None, 0, 0.0, "", [], {}, set() all evaluate to False in boolean context
Sequence Types
Ordered, mutable collection of items
nums = [1, 2, 3, 4]
mixed = [1, "two", 3.0]
nums.append(5)
Key Features:
Mutable (can be changed). Ordered (maintains insertion order). Allows duplicates. Can contain mixed types.
Common Methods:
.append(), .insert(), .remove(), .pop(), .sort(), .reverse(), .extend(), .index()
Ordered, immutable collection of items
coords = (10, 20)
rgb = (255, 128, 0)
single = (42,) # Note comma
Key Features:
Immutable (cannot be changed). Faster than lists. Often used for fixed collections like coordinates or RGB values.
Use Cases:
Function return values, dictionary keys (since they're hashable), data that shouldn't change
Immutable sequence of numbers
r = range(5) # 0,1,2,3,4
r = range(2, 8) # 2,3,4,5,6,7
r = range(0,10,2) # 0,2,4,6,8
Key Features:
Memory efficient - doesn't store all values. Parameters: start (optional), stop, step (optional).
Common Use:
for i in range(10): is the standard way to loop a specific number of times
Mapping Type
Key-value pairs (like a real dictionary)
person = {
"name": "Alice",
"age": 30
}
print(person["name"])
Key Features:
Mutable. Keys must be immutable (strings, numbers, tuples). Values can be any type. Fast lookup by key.
Common Methods:
.keys(), .values(), .items(), .get(), .update(), .pop(), .setdefault()
Set Types
Unordered collection of unique items
colors = {"red", "green", "blue"}
nums = set([1, 2, 2, 3]) # {1,2,3}
colors.add("yellow")
Key Features:
Mutable. No duplicates allowed. Unordered. Great for membership testing and removing duplicates.
Set Operations:
Union (|), Intersection (&), Difference (-), Symmetric Difference (^)
Immutable version of set
fs = frozenset([1, 2, 3])
# fs.add(4) # Error!
print(2 in fs) # True
Key Features:
Immutable (cannot add or remove items). Can be used as dictionary keys. Supports all set operations that don't modify.
Use Cases:
When you need a set that won't change, or need to use a set as a dictionary key
Binary Types
Immutable sequence of bytes
b = b"Hello"
b = bytes([72, 101, 108, 108, 111])
print(b[0]) # 72
Key Features:
Immutable. Each element is an integer 0-255. Used for binary data like files, network communication.
Encoding:
"Hello".encode('utf-8') converts string to bytes, b.decode('utf-8') converts back
Mutable sequence of bytes
ba = bytearray(b"Hello")
ba[0] = 74 # Change 'H' to 'J'
print(ba) # bytearray(b'Jello')
Key Features:
Mutable version of bytes. Can modify individual bytes. Same methods as bytes plus modification methods.
Use Cases:
When you need to modify binary data in place, like editing file contents or network buffers
Memory view of binary data without copying
ba = bytearray(b"Hello")
mv = memoryview(ba)
mv[0] = 74 # Modifies ba!
Key Features:
Provides a view into binary data without copying. Efficient for large data manipulation.
Use Cases:
Advanced: Working with large binary files, image processing, or when memory efficiency is critical
None Type
Represents the absence of a value
x = None
result = some_function() # might return None
if x is None:
print("No value")
Key Features:
Only one value: None. Used for "no value" or "unknown". Functions without return statement return None.
Best Practice:
Use "is None" or "is not None" for comparison, not "== None"