Posts

Showing posts with the label Python - Type Conversion and Casting

Python Programming Language - Type Conversion and Casting

Image
The process of transforming data from one type to another is known as type conversion in programming. Converting int data to str, for instance. There are two types of type conversion in Python: 1.  Implicit  Conversion 2.  Explicit Casting Python Implicit Conversion Python automatically changes one data type to another with implicit type conversion. User involvement is not necessary for this process. Let's look at an example where Python converts a low range data type (integer) to a higher range data type (floating point) to avoid data loss. # Convert an integer to a floating point number num_int = 123 num_float = 1.23 num_new = num_int + num_float print( "Data type in num_int:" , type(num_int)) print( "Data type in num_float:" , type(num_float)) print( "num_new value:" , num_new) print( "Data type in num_new:" , type(num_new)) Console output: Data type in num_int: <class 'int'> Data type in num_float: <class 'float'&