Data types specify the kind of value a variable can hold. Python is a dynamically-typed language, meaning that you don’t need to declare a variable’s type explicitly; Python infers the type based on the assigned value.
To determine the type of a variable, you can use the built-in type() function.
Python’s Built-in Data Types
Python provides the following built-in data types:
-
Text type:
strThe string (
str) data type is used for textual data. You can create a string by enclosing characters within single or double quotes.a = "Hello World!" print(type(a)) #output: <class 'str'> -
Numeric type:
int,float,complexPython provides three types of numeric values:
int: Integer numbers (positive or negative).float: Floating-point numbers (decimal numbers).complex: Complex numbers, with a real and imaginary part.a = 5 #int b = 5.5 #float c = 5j #complex print(type(a)) # Output: <class 'int'> print(type(b)) # Output: <class 'float'> print(type(c)) # Output: <class 'complex'> -
Boolean type:
boolThe boolean (
bool) data type is used to represent one of two possible values:TrueorFalse. It is commonly used in conditions and loops.a = True print(type(a)) #output: <class 'bool'> -
Sequence type:
list,tuple,rangePython has three built-in sequence types:
list: Ordered, mutable collections of items (can hold different data types).tuple: Ordered, immutable collections of items.range: Represents a sequence of numbers, often used in for-loops.We will explore these data types in much more detail as we progress.
a = ["one", "two", "three"] #list b = ("one", "two", "three") #tuple c = range(5) #range print(type(a)) # Output: <class 'list'> print(type(b)) # Output: <class 'tuple'> print(type(c)) # Output: <class 'range'> -
Set type:
set,frozensetSets are unordered collections of unique items.
set: Mutable set.frozenset: Immutable set.a = {"one", "two", "three"} #set b = frozenset({"one", "two", "three"}) #frozenset print(type(a)) # Output: <class 'set'> print(type(b)) # Output: <class 'frozenset'> -
Mapping type:
dictA dictionary (
dict) is an unordered collection of key-value pairs. The keys must be unique, and the values can be any data type.a = {"name" : "John", "age" : 25} print(type(a)) # Output: <class 'dict'> -
None type:
NoneTypeThe
Nonetype represents the absence of a value or a null value. It is commonly used to signify that a variable has no value assigned.a = None print(type(a)) # Output: <class 'NoneType'> -
Binary types:
bytes,bytearray,memoryviewThese types are used for handling binary data.
bytes: Immutable sequence of bytes.bytearray: Mutable sequence of bytes.memoryview: A view object that references data in bytes or bytearray without copying.a = b"Hello" # bytes b = bytearray(b"Hello") # bytearray c = memoryview(b"Hello") # memoryview print(type(a)) # Output: <class 'bytes'> print(type(b)) # Output: <class 'bytearray'> print(type(c)) # Output: <class 'memoryview'>
Summary of Data Types in Python
| Data Type | Description | Example |
|---|---|---|
str |
String type for textual data | "Hello World!" |
int |
Integer type for whole numbers | 5 |
float |
Float type for decimal numbers | 5.5 |
complex |
Complex number type (real + imaginary part) | 5j |
bool |
Boolean type, represents True or False | True or False |
list |
Mutable sequence of items | ["one", "two", "three"] |
tuple |
Immutable sequence of items | ("one", "two", "three") |
range |
Sequence of numbers (usually used in loops) | range(5) |
set |
Unordered collection of unique items | {"one", "two", "three"} |
frozenset |
Immutable set | frozenset({"one", "two", "three"}) |
dict |
Key-value pair collection | {"name": "John", "age": 25} |
NoneType |
Represents no value (None) | None |
bytes |
Immutable binary data | b"hello" |
bytearray |
Mutable binary data | bytearray(b"hi") |
memoryview |
View of binary data | memoryview(b"hi") |
👉 Next tutorial: Typecasting in Python