Python Type Conversion and Type Casting

The procedure of converting the data type(int, float, string, etc) of a variable to another data type is called the type conversation. Python has two kinds of type conversation:

  • Implicit Type Conversion
  • Explicit Type Conversion

 

Implicit Type Conversion

Implicit type conversation, Python automatically convert one data type of a variable to another data type while execution.

Example 1:

x = 2         # integer data type
y = 2.5       # float data type

z = x+y
print("The value of z :",z)
print("The data type of z :",type(z))

Output:

The value of z : 4.5
The data type of z : <class 'float'>

Explanation:

Here, the x is an integer data type and y is the float data type. The variable z is the summation of the x and y variable. We can see that the data type of the variable z is a float as the Python always convert the smaller data type to larger data type in order to avoid the loss of information.

In some scenario, Python can’t use Implicit Type Conversion and we need to use Explicit Type Conversion. Let’s see the example where Implicit Type Conversion won’t work.

Example 2:

x = "Python"            # string datatype
y = 2                   # integer datatype

z = x+y
print("The value of z :",z)
print("The data type of z :",type(z))

Output:

Traceback (most recent call last):
  File "test.py", line 4, in <module>
    z = x+y
TypeError: must be str, not int

Explanation:

The above code has given the type error as Python is not able to use implicit type conversion in such condition.

.     .     .

Explicit Type Conversion

Explicit Type Conversion is also known as type casting. In Explicit Type Conversion, the programmer explicitly covert the datatype of a variable to the required data type. The syntax of Explicit Type Conversion is as follow:

(required_data_type)(expression)
int(x)       # It convert x to integer
float(x)     # It convert x to float
str(x)       # It convert x to string

Example

x = "123"     # string datatype
y = 2         # integer datatype

print("Before type conversion")
print("The datatype of x is :",type(x))
print("The datatype of y is :",type(y))

x = int(x)
z = x+y

print("After type conversion")
print("The datatype of x is :",type(x))
print("The value of z       :",z)
print("The data type of z   :",type(z))

Output

Before type conversion
The datatype of x is : <class 'str'>
The datatype of y is : <class 'int'>
After type conversion
The datatype of x is : <class 'int'>
The value of z       : 125
The data type of z   : <class 'int'>

Note: Type conversation of alphabetic string datatype variable to integer or float data type won’t work in Python.

Example:

x = "Python"     # string datatype
y = 2            # integer datatype

x = int(x)
z = x+y

print("The value of z :",z)
print("The data type of z :",type(z))

Output:

Traceback (most recent call last):
  File "test.py", line 4, in <module>
    x = int(x)
ValueError: invalid literal for int() with base 10: 'Python'

.     .     .

Leave a Reply

Your email address will not be published. Required fields are marked *

Python Tutorials

Python – List Comprehension

Python Set

Python : Assert Keyword

Python Dictionary

Python Tuple

Python List

Python: String

Python: String Formatting using f-string

Python: String Formatting – 2

Python: String Formatting – 1

Python Input/Output

Python Comments

Python – Regular Expression (RegEx)

Python Iterators

Python File Handling

Python Exceptions

Python Function

Python Loops

Python If…Else

Python Collections

Python Operators

Variables in Python

Python Syntax

Introduction to Python

Python OOPs Concepts

Study Machine Learning