Advertisement
❮ Previous: Python Variables & Scope Next: Python Type Casting & Type Hinting ❯

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

Try this code ❯


Advertisement

Deep Dive into Core Data Types

Numeric Types

a = 10          # int
b = 3.14        # float
c = 2 + 3j      # complex

Try this code ❯

Text & Sequence Types

s = "Hello, Python"      # str
lst = [1, "apple", True]  # list
tpl = (1, 2, 3)          # tuple
r = range(0, 5)          # range (0, 1, 2, 3, 4)

Try this code ❯

Mapping & Set Types

user = {"name": "Alice", "age": 25}  # dict
unique_ids = {101, 102, 103}       # set
frozen = frozenset([1, 2, 3])      # frozenset

Try this code ❯

Boolean Type


Advertisement

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

  1. It is not 0, False, or an empty string (""): None is its own distinct object and data type.
  2. Evaluates to False in boolean contexts: While not equal to False, it is treated as falsy.
  3. Singleton: Every reference to None points 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")

Try this code ❯

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

Try this code ❯

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

Try this code ❯

3. Variable Initialization

Use None as a placeholder before assigning a meaningful value later:

user_input = None

if condition:
    user_input = "Active"

Try this code ❯


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
❮ Previous: Python Variables & Scope Next: Python Type Casting & Type Hinting ❯
Advertisement