Type Conversion in Python | Implicit and Explicit Conversion

Type Conversion in Python helps you understand implicit and explicit type conversion and learn how to convert and work with different data types. It makes your programs more flexible and practical, allowing you to process and manipulate data correctly according to your requirements.

What is Type Conversion

  • Type conversion refers to converting one type of data to another type.
  • Type conversion can happen in two ways:
    • Explicit conversion
    • Implicit conversion

Explicit Conversion

  • Explicit conversion also refers to type casting.
  • In explicit conversion, programmer convert the data type of data to required data type.
  • We use the predefined functions like int(), float(), str(), chr() etc to perform explicit type conversion.

Syntax

required_datatype(expression)

Example: Combining Roll Number and Student Name

roll_no = 25
student_name = “Riya”

print(“roll_no:”, type(roll_no))
print(“student_name:”, type(student_name))

# Converting integer to string
roll_no = str(roll_no)

print(“roll_no after conversion:”, type(roll_no))

student_info = roll_no + ” – ” + student_name

print(“Student Info:”, student_info)
print(“student_info:”, type(student_info))

Output

roll_no: <class ‘int’>
student_name: <class ‘str’>
roll_no after conversion: <class ‘str’>
Student Info: 25 – Riya
student_info: <class ‘str’>

Explicit Type Conversion Function in Python

Explicit type conversion function in Python
FunctionDescription
int(value)Convert value to an integer
float(value)Convert value to a floating-point number
str(value)Converts value to string representation
chr(value)Converts value to a character
unichr(value)Converts value to a Unicode character

Example 1: Explicit Type Conversion from Float to Integer

Example 2: Explicit Type Conversion from String to Integer

The input() function always takes input as a string.
If we try to add a number directly to a string, Python gives an error.
Using int() converts the string input into an integer so addition can be performed.

Implicit conversion

  • Implicit conversion is also known as coercion
  • Implicit type conversion means Python automatically converts one data type into another without using any special function
  • Implicit conversion allows conversion from smaller data type to wider size data type without any loss of information
  • This usually happens when operations are performed between different data types.

Example: Code to calculate Simple Interest

# Code to calculate Simple Interest

principal = 3500
rate_of_interest = 6.2
time_period = 5

simple_interest = (principal * rate_of_interest * time_period) / 100

print(“datatype of principal amount : “, type(principal))
print(“datatype of rate of interest : “, type(rate_of_interest))
print(“value of simple interest : “, simple_interest)
print(“datatype of simple interest : “, type(simple_interest))

When we run the above program, the output will be:

datatype of principal amount : <class ‘int’>
datatype of rate of interest : <class ‘float’>
value of simple interest : 1085.0
datatype of simple interest : <class ‘float’>

Example:

  • In this example, one value is an integer and the other is a float.
  • When subtraction is performed, Python automatically converts the integer into a float, and the result becomes a float value.

Leave a Comment

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

error: Content is protected !!
Scroll to Top