Implicit Type Conversion in Python

In Implicit type conversion of data types in Python, the Python interpreter automatically converts one data type to another without any user involvement. To get a more clear view of the topic see the below examples.

Example

As we can see the data type of ‘z’ got automatically changed to the “float” type while one variable x is of integer type while the other variable y is of float type. The reason for the float value not being converted into an integer instead is due to type promotion that allows performing operations by converting data into a wider-sized data type without any loss of information. This is a simple case of Implicit type conversion in Python.

Python3




x = 10
 
print("x is of type:",type(x))
 
y = 10.6
print("y is of type:",type(y))
 
z = x + y
 
print(z)
print("z is of type:",type(z))


Output

x is of type: <class 'int'>
y is of type: <class 'float'>
20.6
z is of type: <class 'float'>

Type Conversion in Python

Python defines type conversion functions to directly convert one data type to another which is useful in day-to-day and competitive programming. This article is aimed at providing information about certain conversion functions.

There are two types of Type Conversion in Python:

  1. Python Implicit Type Conversion
  2. Python Explicit Type Conversion

Similar Reads

Type Conversion in Python

The act of changing an object’s data type is known as type conversion. The Python interpreter automatically performs Implicit Type Conversion. Python prevents Implicit Type Conversion from losing data.The user converts the data types of objects using specified functions in explicit type conversion, sometimes referred to as type casting. When type casting, data loss could happen if the object is forced to conform to a particular data type....

Implicit Type Conversion in Python

In Implicit type conversion of data types in Python, the Python interpreter automatically converts one data type to another without any user involvement. To get a more clear view of the topic see the below examples....

Explicit Type Conversion in Python

...

Contact Us