How to Convert From One Type to Another in Python


In this example, we will learn how to convert from one type to another with the int(), float(), and complex() methods in Python.

Source Code

a = 8       # int
b = 8.8     # float
c = 6 + 8j  # complex

# convert from int to float
a_to_float = float(a)
print(a_to_float)
print(type(a_to_float))

# convert from float to int
b_to_int = int(b)
print(b_to_int)
print(type(b_to_int))

# convert from int to complex
a_to_complex = complex(a)
print(a_to_complex)
print(type(a_to_complex))

Output:

8.0

8

(8+0j)

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments