Python Data Types
In Python, every value has a data type. Since Python is dynamically typed, you do not need to explicitly declare types—Python infers them at runtime.
Built-in Data Types Overview
Python has the following built-in data types categorized by their structural behavior:
| Category | Data Types |
|---|---|
| Numeric | int, float, complex |
| Text | str |
| Sequence | list, tuple, range |
| Mapping | dict |
| Set | set, frozenset |
| Boolean | bool |
| Binary | bytes, bytearray, memoryview |
| None Type | NoneType |
To check the data type of any object, use the built-in type() function:
x = 42
print(type(x)) # int
Deep Dive into Core Data Types
Numeric Types
int: Whole numbers without decimals (unlimited precision in Python 3).float: Floating-point numbers containing decimals or exponential notation (e).complex: Complex numbers written with ajrepresenting the imaginary unit.
a = 10 # int
b = 3.14 # float
c = 2 + 3j # complex
Text & Sequence Types
str: Text strings enclosed in single, double, or triple quotes.list: Ordered, mutable (changeable) collection. Allows duplicates.tuple: Ordered, immutable (unchangeable) collection. Allows duplicates.range: Immutable sequence of numbers generated dynamically.
s = "Hello, Python" # str
lst = [1, "apple", True] # list
tpl = (1, 2, 3) # tuple
r = range(0, 5) # range (0, 1, 2, 3, 4)
Mapping & Set Types
dict: Key-value pairs. Ordered (Python 3.7+), mutable, no duplicate keys allowed.set: Unordered, mutable collection of unique elements.frozenset: Immutable version of a set.
user = {"name": "Alice", "age": 25} # dict
unique_ids = {101, 102, 103} # set
frozen = frozenset([1, 2, 3]) # frozenset
Boolean Type
bool: Represents one of two truth values:TrueorFalse.- Booleans inherit from integers (
Truebehaves as1,Falsebehaves as0).
The None Type (NoneType)
None is a unique keyword in Python used to represent the absence of a value, a null pointer, or a default return state. It is the sole instance of the NoneType object.
Key Characteristics of None
- It is not
0,False, or an empty string (""):Noneis its own distinct object and data type. - Evaluates to
Falsein boolean contexts: While not equal toFalse, it is treated as falsy. - Singleton: Every reference to
Nonepoints to the exact same object in memory.
How to Correctly Check for None
Always use identity operators (is or is not) rather than equality operators (==) when checking if a variable is None:
x = None
# Recommended / Pythonic
if x is None:
print("x is None")
# Avoid
if x == None:
print("x is None")
Common Use Cases for None
1. Default Function Arguments
Avoid using mutable objects (like lists or dictionaries) as default arguments. Use None instead:
def append_item(item, target_list=None):
if target_list is None:
target_list = []
target_list.append(item)
return target_list
2. Default Return Value of Functions
Functions that do not explicitly execute a return statement automatically return None:
def greet(name):
print(f"Hello, {name}")
result = greet("Alice")
print(result) # Output: None
3. Variable Initialization
Use None as a placeholder before assigning a meaningful value later:
user_input = None
if condition:
user_input = "Active"
4. Mutability vs. Immutability
Understanding whether a data type is mutable (can be altered in place) or immutable (cannot be altered after creation) is critical for managing memory and avoiding unexpected side effects:
| Mutable (Modifiable) | Immutable (Read-Only) |
|---|---|
list |
int, float, complex |
dict |
str |
set |
tuple |
bytearray |
frozenset, bytes |