Understanding data types is essential in Python programming. Every value in Python has a type, and knowing how to work with these types helps you write clean, efficient code.
What Are Data Types?
A data type defines the kind of value a variable holds. Python is dynamically typed, meaning you don’t need to declare a type explicitly—Python infers it when you assign a value.
Built-in Data Types in Python
Python includes several built-in data types, grouped into categories:
Here’s the Core Data Types
Category | Type(s) | Example |
---|---|---|
Text | str | x = "Hello" |
Numeric | int , float , complex | x = 10 , x = 3.14 , x = 1j |
Sequence | list , tuple , range | x = [1, 2] , x = (1, 2) , x = range(5) |
Mapping | dict | x = {"a": 1} |
Set | set , frozenset | x = {"a", "b"} , x = frozenset(...) |
Boolean | bool | x = True |
Binary | bytes , bytearray , memoryview | x = bytes(5) |
None | NoneType | x = None |
Some more advanced Runtime Types
Category | Type(s) | Example |
---|---|---|
Callable | function , lambda , builtin_function_or_method | def f(): pass , lambda x: x+1 |
Module | module | import math → type(math) |
File | TextIOWrapper (from open() ) | f = open("file.txt") |
Class/Object | type , object | type(int) → <class 'type'> |
Checking a Variable’s Data Type
Use the built-in type()
function to inspect the type of any object:
x = 5
print(type(x)) # Output: <class 'int'>
Assigning Data Types by Value
Python automatically assigns the correct data type based on the value:
x = "Hello World" # str
x = 20 # int
x = 20.5 # float
x = 1j # complex
x = ["apple", "banana", "cherry"] # list
x = ("apple", "banana", "cherry") # tuple
x = range(6) # range
x = {"name": "John", "age": 36} # dict
x = {"apple", "banana", "cherry"} # set
x = frozenset({"apple", "banana"})# frozenset
x = True # bool
x = b"Hello" # bytes
x = bytearray(5) # bytearray
x = memoryview(bytes(5)) # memoryview
x = None # NoneType
Explicitly Setting Data Types
You can also use constructor functions to define a specific type:
x = str("Hello World")
x = int(20)
x = float(20.5)
x = complex(1j)
x = list(("apple", "banana"))
x = tuple(("apple", "banana"))
x = range(6)
x = dict(name="John", age=36)
x = set(("apple", "banana"))
x = frozenset(("apple", "banana"))
x = bool(5)
x = bytes(5)
x = bytearray(5)
x = memoryview(bytes(5))
Type Hints for Clarity
Python is dynamically typed, meaning variables don’t need declared types. But for larger projects, tutorials, or team collaboration, type hints can make your code easier to read, understand, and maintain.
What Are Type Hints?
Type hints are optional annotations that show what type a variable, function argument, or return value is expected to be. They don’t change how Python runs your code — they’re just guides for humans and tools.
Example
def greet(name: str) -> str:
return f"Hello, {name}"
name: str
means the function expects a string as input.-> str
means the function will return a string.
This doesn’t enforce anything at runtime — you could still pass an integer and Python wouldn’t complain. But tools like IDEs, linters, and type checkers (e.g. mypy) can use these hints to catch bugs early.
Why Use Type Hints?
Clarifies intent — especially useful in educational content and APIs.
Improves readability for learners and collaborators.
Helps autocomplete and documentation in editors like VS Code or PyCharm.
Supports static analysis to catch type mismatches before runtime.
Mastering Python’s data types is the foundation of writing clean, bug-free code. Whether you’re building a web app, crunching data, or scripting automation, understanding types will make your journey smoother.
