✅ Python Data Types – Full Guide with Examples
🔹 What is a Data Type?
A data type defines the type of value a variable holds.
Python is dynamically typed, so it automatically detects the data type when you assign a value.
x = 10 # int
y = 3.14 # float
name = "Arvind" # string
Use type()
to check:
print(type(x)) # <class 'int'>
🔸 1. Numeric Types
✅ int
– Whole numbers
a = 100
print(type(a)) # <class 'int'>
✅ float
– Decimal numbers
b = 99.99
print(type(b)) # <class 'float'>
✅ complex
– Complex numbers
c = 3 + 4j
print(type(c)) # <class 'complex'>
📌 Use Case: Finance, math, engineering.
🔸 2. Text Type
✅ str
– String (sequence of characters)
name = "Arvind"
msg = 'Hello'
print(type(name)) # <class 'str'>
✅ Strings can be:
- Single
'Hello'
- Double
"Hello"
- Triple
'''Multi-line'''
🔸 3. Boolean Type
✅ bool
– Only two values: True
or False
is_active = True
print(type(is_active)) # <class 'bool'>
📌 Used for logic, conditions, switches.
🔸 4. Sequence Types
✅ list
– Ordered, changeable, allows duplicates
fruits = ["apple", "banana", "mango"]
print(type(fruits)) # <class 'list'>
print(fruits[0]) # apple
✅ tuple
– Ordered, unchangeable, allows duplicates
colors = ("red", "blue", "green")
print(type(colors)) # <class 'tuple'>
✅ range
– Represents a range of numbers
r = range(1, 6)
print(list(r)) # [1, 2, 3, 4, 5]
🔸 5. Set Types
✅ set
– Unordered, no duplicates
nums = {1, 2, 3, 2}
print(nums) # {1, 2, 3}
✅ frozenset
– Immutable version of set
fset = frozenset([1, 2, 3])
print(fset)
🔸 6. Mapping Type
✅ dict
– Key-value pair storage
person = {"name": "Arvind", "age": 30}
print(person["name"]) # Arvind
print(type(person)) # <class 'dict'>
📌 Great for storing structured data like user profiles, configurations.
🔸 7. Binary Types
✅ bytes
b = b"Hello"
print(type(b)) # <class 'bytes'>
✅ bytearray
ba = bytearray([65, 66, 67])
print(ba) # bytearray(b'ABC')
✅ memoryview
mv = memoryview(bytes(5))
print(mv)
📌 Useful for working with binary files, images, audio, etc.
🔸 8. NoneType
✅ None
– Represents absence of value
x = None
print(type(x)) # <class 'NoneType'>
📌 Used to initialize variables or return "nothing".
✅ Type Casting (Convert Data Types)
a = int("10") # string to int
b = float(5) # int to float
c = str(99) # int to string
print(type(a), type(b), type(c))
✅ Real-world Example
user = {
"name": "Arvind", # str
"age": 25, # int
"is_verified": True, # bool
"skills": ["Python", "AI"], # list
}
print(user)
✅ Summary Table
Data Type | Description | Example |
---|---|---|
int |
Integer numbers | x = 10 |
float |
Decimal numbers | pi = 3.14 |
str |
Text/String | name = "Arvind" |
bool |
True or False | is_on = True |
list |
Ordered, mutable | [1, 2, 3] |
tuple |
Ordered, immutable | (1, 2, 3) |
set |
Unordered, unique | {1, 2, 3} |
dict |
Key-value store | {"name": "Arvind"} |
NoneType |
Empty/Null | x = None |
✅ Best Practices
- Use descriptive variable names (
student_name
notsn
) - Don’t confuse
=
(assignment) with==
(comparison) - Prefer lists for ordered collections, sets for uniqueness
- Use
None
to declare an uninitialized value - Always test your types with
type()
for clarity
0 Comments