In Python, data types are classifications that specify the type of value a variable holds. Understanding them is essential for writing correct and efficient code.
🔹 1. Basic Built-in Data Types
Numeric Types
Type |
Description |
Example |
int |
Integer (whole number) |
x = 10 |
float |
Floating-point number (decimal) |
y = 3.14 |
complex |
Complex number (real + imag part) |
z = 2 + 3j |
Text Type
Type |
Description |
Example |
str |
String (text) |
name = "Abhi" |
Boolean Type
Type |
Description |
Example |
bool |
Logical True or False |
is_active = True |
Sequence Types
Type |
Description |
Example |
list |
Ordered, mutable collection |
fruits = ["apple", "banana"] |
tuple |
Ordered, immutable collection |
coords = (10, 20) |
range |
Sequence of numbers |
range(0, 5) |
Set Types
Type |
Description |
Example |
set |
Unordered, unique items |
colors = {"red", "blue"} |
frozenset |
Immutable set |
fs = frozenset([1, 2, 3]) |
Mapping Type
Type |
Description |
Example |
dict |
Key-value pairs |
person = {"name": "Abhi", "age": 30} |
🔹 2. Binary Types
Type |
Description |
Example |
bytes |
Immutable byte sequences |
b = b"hello" |
bytearray |
Mutable byte sequences |
ba = bytearray([65, 66]) |
memoryview |
Memory view of bytes |
mv = memoryview(b'abc') |
🔹 3. None Type
Type |
Description |
Example |
NoneType |
Represents null/empty |
x = None |
🔍 Use type()
to Check Data Type
x = 10
print(type(x)) # Output: <class 'int'>
🛠️ Type Conversion Examples
# Convert int to float
a = float(5) # 5.0
# Convert str to int
b = int("10") # 10
# Convert list to set
c = set([1, 2, 2]) # {1, 2}