Data Types in Python
Below is a structured explanation of the primary built-in data types.
1. Numeric Types
1.1 int
Represents whole numbers (positive, negative, or zero).
x = 10
y = -5
-
No decimal point
-
Unlimited precision (only limited by memory)
1.2 float
Represents decimal (floating-point) numbers.
pi = 3.14
temp = -2.5
-
Uses double precision (64-bit IEEE 754)
1.3 complex
Represents complex numbers (real + imaginary part).
z = 3 + 4j
-
3→ real part -
4j→ imaginary part
2. Sequence Types
2.1 str (String)
Represents textual data.
name = "Prasath"
msg = 'Hello'
-
Immutable (cannot be changed after creation)
-
Supports indexing and slicing
2.2 list
Ordered, mutable collection of elements.
marks = [80, 75, 90]
-
Allows duplicates
-
Can store different data types
-
Elements can be modified
2.3 tuple
Ordered, immutable collection.
point = (10, 20)
-
Faster than list
-
Cannot modify elements after creation
2.4 range
Represents a sequence of numbers.
r = range(1, 5)
-
Used mainly in loops
-
Generates numbers from 1 to 4
3. Set Types
3.1 set
Unordered collection of unique elements.
numbers = {1, 2, 3, 3}
-
Automatically removes duplicates
-
No indexing
3.2 frozenset
Immutable version of set.
fs = frozenset([1, 2, 3])
4. Mapping Type
4.1 dict (Dictionary)
Stores data in key–value pairs.
student = {
"name": "Prasath",
"age": 22,
"dept": "CS"
}
-
Keys must be unique
-
Mutable
-
Very fast lookup (hash table based)
5. Boolean Type
5.1 bool
Represents logical values.
a = True
b = False
-
Used in conditions and comparisons
-
Internally subclass of
int(True = 1,False = 0)
6. Binary Types
6.1 bytes
Immutable sequence of bytes.
b = b"hello"
6.2 bytearray
Mutable version of bytes.
ba = bytearray(5)
6.3 memoryview
Access memory of binary objects without copying.
mv = memoryview(b"abc")
7. None Type
7.1 NoneType
Represents absence of value.
x = None
Checking Data Type
Use the type() function:
x = 10
print(type(x)) # <class 'int'>
Summary Table
|
|
|
|
|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Comments
Post a Comment