PYTHON CONVERSIONS

In this article, you will learn about the Type conversion and uses of type conversion.
Type Casting is the method to convert the variable data type into a certain data type in order to the operation required to be performed by users.
Casting in python is therefore done using constructor functions:

  • int() - constructs an integer number from an integer literal, a float literal (by removing all decimals), or a string literal (providing the string represents a whole number).
  • float() - constructs a float number from an integer literal, a float literal or a string literal (providing the string represents a float or an integer).
  • str() - constructs a string from a wide variety of data types, including strings, integer literals and float literals.

Examples for Integers

a=int(634)        #Output is 634
b=int(6.73)       #Output is 6

Examples for Float Numbers

a=float(375)        #Output is 375.0
b=float(3.776)       #Output is 3.776

Examples for Strings

a=str("this is rguktweb team")        #Output is 'this is rgukt web team'
b=str(3.776)                          #Output is '3.776'
Types of Conversions

The process of converting the value of one data type (integer, string, float, etc.) to another data type is called type conversion.
Python has two types of type conversion.

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

In Implicit type conversion, Python automatically converts one data type to another data type.
This process doesn't need any user involvement.
In this, methods, Python converts data type into another data type automatically.
In this process, users don’t have to involve in this process.
Example to convert integer to float

int_num=143
float_num=1.23
new_num= int_num+float_num
print("data type of int_num is ",type(int_num))          
 #Output is datatype of int_num: <class 'int'>
print("data type of float_num is ",type(float_num))      
 #Output is datatype of float_num: <class 'float'>
print("data type of new_num is ",type(new_num))          
 #output is datatype of new_num: <class 'float'>

In the above program,

  • We add two variables int_num and float_num, storing the value in new_num.
  • We will look at the data type of all three objects respectively.
  • In the output, we can see the data type of int_num is an integer while the data type of float_num is a float.
  • Also, we can see the new_new has a float data type because Python always converts smaller data types to larger data types to avoid the loss of data.
Example for Addition of string(higher) data type and integer(lower) datatype
int_num=123
str_num="143"
print("Data type of int_num:",type(int_num))      
 #Output is Data type of int_num: <class 'int'> 
print("Data type of str_num:",type(str_num))	     
 #Output is Data type of str_num: <class 'str'> 
print(int_num+str_num)			
Traceback (most recent call last): 
  File "python", line 7, in <module> 
TypeError: unsupported operand type(s) for +: 'int' and 'str'

in the above programm

  • We add two variables int_num and str_num.
  • As we can see from the output, we got TypeError. Python is not able to use Implicit Conversion in such conditions.
  • However, Python has a solution for these types of situations which is known as Explicit Conversion.
Explicit Type Conversion

In Explicit Type Conversion, users convert the data type of an object to required data type.
We use the predefined functions like int(), float(), str(), etc to perform explicit type conversion.
This type of conversion is also called typecasting because the user casts (changes) the data type of the objects.
Example for Addition of string and integer using explicit conversion

A=143
B="253"
print("Data type of A:",type(A))
#Output is Data type of A: <class 'int'>
print("Data type of B before Type Casting:",type(B))
#Output is Data type of B before Type Casting: <class 'str'>
B=int(B)
print("Data type of B after Type Casting:",type(B))
#Output is Data type of B after Type Casting: <class 'int'>
C=A+B
print("Sum of A and B:",C)
#Output is Sum of A and B:396
print("Data type of the sum:",type(C))
#Output is Data type of the sum: <class 'int'>

In the above program

  • We add B and A variable
  • We converted B from string(higher) to integer(lower) type using int() function to perform the addition.
  • After converting B to an integer value, Python is able to add these two variables
  • We got the C value and data type to be an integer.
Key points in Conversions

Type Conversion is the conversion of object from one data type to another data type.
Implicit Type Conversion is automatically performed by the Python interpreter.
Python avoids the loss of data in Implicit Type Conversion.
Explicit Type Conversion is also called Type Casting, the data types of objects are converted using predefined functions by the user.
In Type Casting, loss of data may occur as we enforce the object to a specific data type.