Type casting (or type conversion) is the process of converting one data type into another using Python's built-in conversion functions.
Note: Not every conversion is valid. Invalid conversions raise
TypeErrororValueError.
| From | Can Convert To |
|---|---|
str |
int, float, complex, list, tuple, set, frozenset, bytes, bytearray, bool |
int |
str, float, complex, bool, bytes |
float |
str, int, complex, bool |
complex |
str, bool |
list |
str, tuple, set, frozenset, bool, bytes*, bytearray* |
tuple |
str, list, set, frozenset, bool, bytes*, bytearray* |
range |
list, tuple, set, frozenset, bool |
dict |
list, tuple, set, frozenset, bool, str |
set |
list, tuple, frozenset, bool, str |
frozenset |
list, tuple, set, bool, str |
bool |
int, float, complex, str |
bytes |
str (decode), list, tuple, set, bytearray, bool |
bytearray |
bytes, list, tuple, set, bool, str (decode) |
memoryview |
bytes, bytearray, list, bool |
NoneType |
bool, str |
*****
bytes()andbytearray()require iterable elements to be integers in the range 0–255.
s = "123"
int(s) # 123
float(s) # 123.0
complex(s) # (123+0j)
bool(s) # True
list(s) # ['1', '2', '3']
tuple(s) # ('1', '2', '3')
set(s) # {'1', '2', '3'}
frozenset(s) # frozenset({'1', '2', '3'})
bytes(s, "utf-8")
bytearray(s, "utf-8")x = 10
str(x) # '10'
float(x) # 10.0
complex(x) # (10+0j)
bool(x) # True
bytes(x) # 10 zero bytesx = 10.75
int(x) # 10
str(x) # '10.75'
complex(x) # (10.75+0j)
bool(x) # Truex = 3 + 4j
str(x)
bool(x)
# Invalid
# int(x)
# float(x)L = [1, 2, 3]
tuple(L)
set(L)
frozenset(L)
str(L)
bool(L)
bytes(L)
bytearray(L)T = (1, 2, 3)
list(T)
set(T)
frozenset(T)
str(T)
bool(T)
bytes(T)
bytearray(T)r = range(5)
list(r)
tuple(r)
set(r)
frozenset(r)
bool(r)d = {"a": 1, "b": 2}
list(d) # ['a', 'b']
tuple(d)
set(d)
frozenset(d)
str(d)
bool(d)list(d.values())list(d.items())s = {1, 2, 3}
list(s)
tuple(s)
frozenset(s)
str(s)
bool(s)fs = frozenset({1, 2, 3})
list(fs)
tuple(fs)
set(fs)
str(fs)
bool(fs)b = True
int(b) # 1
float(b) # 1.0
complex(b) # (1+0j)
str(b) # 'True'b = b"Hello"
str(b) # "b'Hello'"
b.decode() # 'Hello'
list(b)
tuple(b)
set(b)
bytearray(b)
bool(b)ba = bytearray(b"Hello")
bytes(ba)
list(ba)
tuple(ba)
set(ba)
ba.decode()
bool(ba)mv = memoryview(b"Hello")
bytes(mv)
bytearray(mv)
list(mv)
bool(mv)x = None
bool(x) # False
str(x) # 'None'
# Invalid
# int(None)
# float(None)
# list(None)The following values evaluate to False:
bool(0)
bool(0.0)
bool(0j)
bool("")
bool([])
bool(())
bool({})
bool(set())
bool(frozenset())
bool(range(0))
bool(b"")
bool(bytearray())
bool(None)Everything else evaluates to True.
int("abc") # ValueError
float("hello") # ValueError
complex("abc") # ValueError
int(3 + 4j) # TypeError
float(3 + 4j) # TypeError
list(10) # TypeError
tuple(10) # TypeError
set(10) # TypeError
dict([1, 2, 3]) # TypeError
bytes([300]) # ValueError- Use Python's built-in conversion functions such as
int(),float(),str(),list(), andtuple()for type casting. - Some conversions are lossy (e.g.,
float→inttruncates the decimal part). - Not every conversion is valid; invalid conversions raise
TypeErrororValueError. - Empty containers and zero-like values evaluate to
False; everything else evaluates toTrue.