Some text some message..
Back 2.2 🧠 Type Conversion in Python 19 Jun, 2025

Type conversion means changing the data type of a value from one type to another. Python supports two types of conversions:


🔄 1. Implicit Type Conversion (Automatic)

Python automatically converts a smaller data type to a larger data type.

a = 5       # int
b = 2.0     # float
c = a + b   # Result: 7.0 (float)
print(type(c))  # Output: <class 'float'>

🔧 2. Explicit Type Conversion (Type Casting)

Done manually using built-in functions.

🔢 Numeric Conversion

Function Converts To Example
int() Integer int("10")10
float() Floating-point float("5")5.0
complex() Complex number complex("2+3j")2+3j

🔤 String & Others

Function Converts To Example
str() String str(100)"100"
list() List list("abc")['a','b','c']
tuple() Tuple tuple([1, 2])(1, 2)
set() Set set([1, 1, 2]){1, 2}
dict() Dictionary dict([(1,'a'), (2,'b')])

⚠️ Important Notes

  • Not all conversions are allowed. E.g. int("abc") throws an error.

  • Use type() to check a variable's current data type.