How to implement type conversion in python?

Description

To see how type casting has been implemented in python3.

Implicit Type Conversion

  Python automatically converts one data type to another data type.

   This process doesn't need any user involvement.

Explicit Type Conversion

   In Explicit type conversion, users need to convert the data type of an object to required data type.

   We can use the predefined functions like int(), float(), str(), etc to perform explicit type conversion

Source code(Implicit Type Conversion):

#Implicit Type Conversion
#Get an integer input
n=int(input(“Enter the integer:”))

#Get another float input
m=float(input(“Enter the number:”))

#add those inputs
o=n+m

#print the variable o
print(o)
#print type of variable o
print(type(o))

Source code(Explicit Type Conversion):

#Explicit Type Conversion
print(“###########################”)
print(“Explicit Type conversion”)
n=int(input(“Enter the integer:”))
m=input(“Enter the number:”)
print(“\n”)
print(“Data type of m(before conversion) is:”,type(m))
print(“\n”)

#Explicit type conversion
m=int(m)
print(“Data type of m(after conversion) is:”,type(m))

#add those inputs
o=n+m

#print the variable o
print(o)

#print type of variable o
print(type(o))

Leave Comment

Your email address will not be published. Required fields are marked *

clear formSubmit