What is it?
Python is dynamically typed — you do not declare variable types. Python infers the type from the assigned value. It has five primitive types: int, float, str, bool, and NoneType.
Why does it matter?
Understanding Python types prevents the most common beginner bugs — string concatenation errors, unexpected comparisons, and None-related crashes.
Learn Python data types — int, float, str, bool, NoneType — dynamic typing, type checking, and type conversion.
Real-World Use Cases
- 📝 User input processing - Input from users is always a string — you must convert to int or float for calculations.
- 🗄️ Database results - Database drivers return strings and ints — knowing how to check and convert types prevents runtime errors.
- 📊 CSV data processing - Every CSV field starts as a string — convert numbers, parse dates, and validate types before analysis.
- 🌐 API response parsing - JSON values become Python types — knowing the type hierarchy helps handle missing values safely.
Python Data Types
age = 25 # int
price = 99.99 # float
name = "Rahul" # str
active = True # bool
result = None # NoneType
big = 1_000_000 # underscores for readability
print(type(age)) #
print(type(name)) #
print(type(None)) #
Type Conversion
# String to number
number = int("42") # 42
price = float("99.50") # 99.5
# Number to string
age = 25
message = f"Age: {age}" # f-string handles it automatically
also = "Age: " + str(age) # explicit conversion
# Bool conversions
bool(0) # False
bool(1) # True
bool("") # False — empty string is falsy
bool("hi") # True — non-empty string is truthy
bool(None) # False
bool([]) # False — empty list is falsy
isinstance() — Pythonic Type Checking
x = 42
print(isinstance(x, int)) # True
print(isinstance(x, str)) # False
print(isinstance(x, (int, float))) # True — check multiple types
def process_score(score):
if not isinstance(score, (int, float)):
raise TypeError(f"Expected number, got {type(score).__name__}")
if score < 0 or score > 100:
raise ValueError("Score must be 0-100")
return score
Q: What is the difference between == and is in Python?
== checks if two values are equal. is checks if two variables point to the SAME object in memory. Use == for value comparison; use is only to check for None: if result is None.
Comments (0)
No comments yet. Be the first!
Leave a Comment