What is the different between implicit and explicit conversions in programming, specifically python?
Everywhere I look I get complex answers that are hard to understand. Especially when this is supposed to show the basic proprieties of using float, int, and just how python numbers/ strings work.
If Python needs to convert a variable to another data type, it does so automatically.
For example,
myInt = 5
myFloat = 1.6
mySum = myInt + myFloat
print(mySum)
In this case, Python automatically promoted myInt
to a floating point value and then added.
Suppose you actually wanted an integer. We could use this example
myInt = 5
myFloat = 1.6
mySum = myInt + int(myFloat)
print(mySum)
In this case we’ve explicitly converted myFloat
to an integer.
Implicit conversion is when Python automatically converts the variable to a compatible type. Explicit conversion is when you, the programmer, convert the variable to the target type.
2 Likes