Type Conversion in Python

We perform type conversion in Python to ensure that operations between different data types work correctly and produce meaningful results. Without conversion, combining incompatible types (like adding a string and an integer) would raise errors.

Type conversion is essential for data consistency, error prevention, and smooth execution of operations in Python.

Types of Conversion in Python

The following are the types of conversions in Python:

Type Conversion in Python

Example of Implicit Type Conversion in Python

Let us see an example to perform automatic, i.e., implicit type casting when types are mixed in an expression:

# Implicit (Automatically) Type Conversion in Python

# Implicit Type
val1 = 10 # int
val2 = 10.5 # float

# Output
print(val1 + val2) # int + float

Output

20.5

Built-in Methods for Explicit (Manual) Type Conversion

Type Conversion in Python allows users to convert one type to another, for example, float to int, int to complex, int to float, etc. To convert a number from one type to another, for example, into a float, we have the following methods in Python:

  • float(num): To convert from num int to float type.
  • int(num): To convert num to the int type.
  • complex(num): To convert from num int to complex type.

Let us now see three examples of Explicit Type Conversion and convert one datatype to another:

Convert int to float in Python

Let us see an example of float() in Python to convert type int into float:

Demo15.py

# int datatype
val = 5
print("Value: ",val)
print("Value type: ",type(val))

# convert int to float
res =  float(val)
print("Converted Value: ",res)
print("Type of converted Value: ",type(res))

The output is as follows:

Value: 5
Value type: <class 'int'>
Converted Value: 5.0
Type of converted Value: <class 'float'>

Convert from float to int in Python

Let us see an example of the int() in Python to convert from type float to int:

Demo16.py

# float datatype
val = 5.2
print("Value: ",val)
print("Value type: ",type(val))

# convert float to int
res =  int(val)
print("Converted Value: ",res)
print("Type of converted Value: ",type(res))

The output is as follows:

Value: 5.2
Value type: <class 'float'>
Converted Value: 5
Type of converted Value: <class 'int'>

Convert int to complex in Python

Let us see an example of the complex() in Python to typecast an int to a complex type:

Demo17.py

#!/usr/bin/python

# int datatype
val = 7
print("Value: ",val)
print("Value type: ",type(val))

# convert int to complex
res =  complex(val)
print("Converted Value: ",res)
print("Type of converted Value: ",type(res))

The output is as follows:

Value: 7
Value type: <class 'int'>
Converted Value: (7+0j)
Type of converted Value: <class 'complex'>

Video Tutorial (English)

Video Tutorial (Hindi)

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

Python Numbers with Examples
Python IDLE
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment