Type Conversion & Casting
Type conversion is the process of turning a value of one type into another — for example, turning the text "42" into the integer 42. Python performs some conversions automatically (implicit conversion) and requires you to request others explicitly (explicit casting).
Implicit Conversion
When you mix numeric types in an expression, Python automatically converts the “narrower” type to the “wider” one so the operation can proceed without any data loss. This happens without you writing any conversion code.
result = 5 + 2.0 # int + float print(result) # 7.0 print(type(result)) # <class 'float'> -- Python widened the int to a float automatically is_valid = True total = is_valid + 10 # bool + int print(total) # 11 -- True was implicitly treated as 1
Explicit Casting
For anything Python won’t convert automatically, you call a built-in constructor function to cast the value yourself: int(), float(), str(), bool(), list(), tuple(), set().
# Converting to numbers
print(int("42")) # 42
print(float("3.14")) # 3.14
print(int(3.9)) # 3 -- truncates, does not round
# Converting to strings
print(str(42)) # "42"
print(str(3.14)) # "3.14"
print(str(True)) # "True"
# Converting to bool
print(bool(0)) # False
print(bool("text")) # TrueCommon Conversion Pitfalls
int() can parse a plain integer string, but it cannot parse a string that contains a decimal point — that raises a ValueError. To convert a numeric string that might contain a decimal point, go through float() first.
print(int("42")) # 42 -- works fine
# int("3.5") # ValueError: invalid literal for int() with base 10: '3.5'
print(int(float("3.5"))) # 3 -- go through float() first, then truncate with int()
# int() also fails on non-numeric text entirely
# int("hello") # ValueError: invalid literal for int() with base 10: 'hello'Input | int(input) | Works? |
|---|---|---|
"42" | 42 | Yes |
"3.5" | ValueError | No — decimal strings need float() first |
" 42 " | 42 | Yes — surrounding whitespace is ignored |
"forty-two" | ValueError | No — not a numeric string at all |
3.9 | 3 | Yes — but truncates, does not round |
Converting Between Collections
The collection constructors list(), tuple(), and set() accept any iterable and build a new collection of that type from its elements. This is a common and useful pattern — for instance, using set() to deduplicate a list.
numbers = [1, 2, 2, 3, 3, 3]
as_set = set(numbers) # removes duplicates
print(as_set) # {1, 2, 3}
as_tuple = tuple(as_set) # immutable, ordered snapshot (order not guaranteed for sets)
print(as_tuple)
as_list_again = list(as_set)
print(as_list_again)
# Strings are iterables of characters too
print(list("abc")) # ['a', 'b', 'c']Float to Int: Truncation, Not Rounding
print(int(2.9)) # 2 -- truncated, not rounded print(int(-2.9)) # -2 -- truncates toward zero, not "down" print(round(2.9)) # 3 -- proper rounding print(round(2.5)) # 2 -- Python uses "round half to even" (banker's rounding)!
Quick Reference
Implicit conversion happens automatically between compatible numeric types (int, float, bool).
Explicit casting requires calling
int(),float(),str(),bool(),list(),tuple(), orset().int("3.5")fails — convert throughfloat()first if the string might have a decimal point.int()on a float truncates; useround()if you want actual rounding.Collection constructors accept any iterable, making
set(),list(), andtuple()easy converters between each other and strings.