0% found this document useful (0 votes)
52 views4 pages

Implicit Vs Explicit Type Functions

Uploaded by

Mamta Bhardwaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views4 pages

Implicit Vs Explicit Type Functions

Uploaded by

Mamta Bhardwaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Type Conversion in Python with Examples

The process of converting a Python data type into another data type is known as type
conversion. There are mainly two types of type conversion methods in Python: implicit
type conversion and explicit type conversion. In this module, we will go through the
following topics:

So, without any further delay, let’s get started.

Implicit Type Conversion in Python


In Python, when the data type conversion takes place during compilation or during the
runtime, it’s called an implicit data type conversion. Python handles the implicit data
type conversion, so we don’t have to explicitly convert the data type into another data
type. We have seen various examples of this kind of data type conversion throughout
the tutorial. Let’s see another example.

Let’s add two Python variables of two different data types and store the result in a
variable and see if the Python compiler is able to convert the data type of the resultant
variable or not.
1
a =5
2 b = 5.5
3 sum = a + b
4 print (sum)
5 print (type (sum)) #type() is used to display the datatype of a variable
6
7
Output:
8 10.5
9 <class ‘float’>
10

In the above example, we have taken two variables of integer, and float data types and
added them. Further, we have declared another variable named ‘sum’ and stored the
result of the addition in it. When we checked the data type of the sum variable, we could
see that the data type of the sum variable had been automatically converted into the
float data type by the Python compiler. This is called implicit type conversion.

The reason that the sum variable was converted into the float data type and not the
integer data type is that if the compiler had converted it into the integer data type, then it
would’ve had to remove the fractional part, which would have resulted in data loss. So,
Python always converts smaller data types into larger data types to prevent the loss of
data.

Explicit Type Conversion in Python


Explicit type conversion is also known as typecasting. Explicit type conversion takes
place when the programmer clearly and explicitly defines the same in the program. For
explicit type conversion, there are some in-built Python functions. The following table
contains some of the in-built functions for type conversion, along with their descriptions.
Function Description
int(y [base]) It converts y to an integer, and Base specifies the number base. For example, if
you want to convert the string into decimal numbers then you’ll use 10 as the
base.
float(y) It converts y to a floating-point number.
complex(real It creates a complex number.
[imag])
str(y) It converts y to a string.
tuple(y) It converts y to a tuple.
list(y) It converts y to a list.
set(y) It converts y to a set.
dict(y) It creates a dictionary and y should be a sequence of (key, value) tuples.
ord(y) It converts a character into an integer.
hex(y) It converts an integer to a hexadecimal string.
oct(y) It converts an integer to an octal string

Now that we know the in-built functions provided by Python that are used for explicit
type conversion, let’s see the syntax for explicit type conversion:
1 (required_data type)(expression)

Let’s go over the following examples for explicit type conversion in Python.
1
# adding string and integer data types using explicit type conversion
2
a = 100
3
b = “200”
4 result1 = a + b

5 b = int(b)

6 result2 = a + b

7 print(result2)

10 Output:

Traceback (most recent call last):


11
File “”, line 1, in
12
TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’
13
300
14

In the above example, variable a is of the number data type, and variable b is of the
string data type. When we try to add these two integers and store the value in a variable
named result1, a TypeError occurs, as shown in the output. So, in order to perform this
operation, we have to use explicit type casting.
As we can see in the above code block, we have converted the variable b into an int
type and then added variables a and b. The sum is stored in the variable named result2,
and when printed, it displays 300 as output, as we can see in the output block.

Go through this Python Course in London to get a clear understanding of Python!


Key Points to Remember While Performing Type
Conversion in Python
 Type conversion is the process of converting a data type into another data type.
 Implicit type conversion is performed by a Python interpreter only.
 Explicit type conversion is performed by the user by explicitly using type conversion
functions in the program code.
 Explicit type conversion is also known as typecasting.
 Python, when performing implicit type casting, avoids the loss of data.

You might also like