KnowledgeBoat Logo
|

Robotics & Artificial Intelligence

Explain each of the following with an example:

(i) Implicit type conversion

(ii) Explicit type conversion

Python Funda

3 Likes

Answer

(i) Implicit Type Conversion: In a mixed mode expression, the result gets automatically converted into the highest data type available in the expression, without any intervention from the user. This system of conversion is known as implicit type conversion or coercion.

Example:

a = 15; b = 4; c = 12.4
ans = (a + b) - c
print(ans)

Here, (a + b) gives an integer (int – int = int), then int – float gives a float. So, the result is 6.6 (float type), obtained automatically without any user intervention.

(ii) Explicit Type Conversion: Explicit type conversion is another way of type conversion, in which one data type gets converted into another data type depending upon the user's choice. When the data type is converted after the user's intervention, the conversion is known as explicit type conversion or type casting.

Example:

a = 32; b = 12; c = 4
ans = int((a - b) / c)
print(ans)

Here, (a - b) / c would normally give 5.0 (float). But the user explicitly applies int() to forcibly convert it to int type, resulting in 5. This forceful conversion by the user is called type casting.

Answered By

3 Likes


Related Questions