bing
Flat 10% & upto 50% off + 10% Cashback + Free additional Courses. Hurry up
×
UPTO
50%
OFF!
Intellipaat
Intellipaat
  • Live Instructor-led Classes
  • Expert Education
  • 24*7 Support
  • Flexible Schedule

Type Conversion in Python with Example

The process of converting a datatype into another datatype is known as Type conversion. There are mainly two types of type conversions in Python, namely, implicit type conversion and explicit type conversion. In this module, we will go through the following topics, in case you want to jump to a specific one.

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

Learn Python in 16 hrs from experts

Implicit Type conversion

In Python, when the datatype conversion takes place during compilation or during run time, then it’s called implicit Datatype conversion. Python handles the implicit datatype conversion, so we don’t have to explicitly convert the datatype into another datatype. We have seen various examples of this kind of Datatype conversion throughout the tutorial.

Don’t worry if you can’t recall it, let’s see another example through the following code block.

Let’s add two variables of two different datatypes and store the result in a variable and see if the python compiler is able to convert the datatype of the resultant variable or not.

a = 5
b = 5.5
sum = a + b
print (sum)
print (type (sum)) #type() is used to display the datatype of a variable

Output:

10.5
<class ‘float’>

In the above example, we have taken two variables of datatype integer and float and added them. Further we have declared another variable named sum and stored the result of the addition in it. When we checked the datatype of the sum variable, we can see that the datatype of sum variable has been automatically converted into float datatype by the Python compiler. This is called Implicit Type conversion.

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

In some cases, Python cannot use implicit type conversion and that’s where Explicit type conversion comes into play. Moving ahead, let’s learn more about it.

Wish to Learn Python? Click Here

Explicit Type conversion

Explicit type conversion is also known as type casting. Explicit type conversion takes place when the programmer clearly ad explicitly defines the same in the program. For explicit type conversion, Python provides some inbuilt functions. Following table contains some of the inbuilt function for type conversion along with their descriptions.

Function Description
int(y [base]) Converts y to an integer. Base specifies the base if y is a string.
float(y) Converts y to a floating-point number.
complex(real [imag]) Creates a complex number.
str(y) Converts y to a string.
tuple(y) Converts y to a tuple.
list(y) Converts y to a list.
set(y) Converts y to a set.
dict(y) Creates a dictionary. y should be a sequence of (key,value) tuples.
ord(y) Converts a character into an integer
hex(y) Converts integer to hexadecimal string
oct(y) Converts integer to octal string

Now that we know the in-built functions provided by Python, that are used for type explicit type conversion.

The syntax for explicit type conversion is as follows:

(required_datatype)(expression)

Let’s go over the following examples for explicit type conversion in Python.

# adding string and integer datatypes using explicit type conversion
a = 100
b = “200”
result1 = a + b
b = int(b)
result2 = a + b
print(result2)

Output:

Traceback (most recent call last):
File “”, line 1, in
TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’
300

In the above example, the variable a is of number datatype and variable b is of string datatype. When we try to add these two integers and store the value in 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 int type and then added variable a and b. The sum is stored in variable named result2 and when printed it display 300 as output, as we can see in the output block.

Become Python Certified in 16 hrs.
CLICK HERE

key points to remember

  • Type conversion is the process of converting a datatype into another datatype.
  • Implicit type conversion is performed by 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 type casting.
  • Python, when performing implicit type casting, avoids the loss of data.

With this, we come to the end of this module. The next module highlights the if-else statements in Python. See you there!

Now, you can head towards our Python Certification Training to excel your career to the new heights. Also check our free interview questions and answers for Python.

Previous Next

Download Interview Questions asked by top MNCs in 2019?

"0 Responses on Type conversion in Python"

Leave a Message

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.
top

Sales Offer

Sign Up or Login to view the Free Type conversion in Python.