Skip to main content

Type Casting and Type Conversion in python

Difference between Type Casting and Type Conversion

The basic difference between type casting and type conversion is that type conversion is made automatically made by the interpreter or compiler whereas type casting is to be explicitly done by the developer.


We need type casting and type conversion when we need to convert the type of one variable to another type. When we want to perform addition or subtraction,the datatype of both variables .Type casting can be defined as, casting of one data type to another data type, by the programmer, at the time of program design.

Automatic Type Conversion(Implicit Type Conversion)
The interpreter only converts lower datatypes into higher datatypes.It cannot automatically convert higher datatype to lower datatype . It is also called Implicit Type Conversion

We can understand it by taking a real world example.Suppose that there is a person in a company who is working at a higher post, he cannot be automatically demoted by company, instead he has to be forced for doing that. Likewise, we may have other person in the company who is working at lowest position. If we want to promote him then it will happen very smoothly and the person is not required to be forced.

eg. 
var_1=80
var_2=100.98
print("Type of var_1 is ",type(var_1))
print("Type of var_2 is ",type(var_2))

var_3=var_1+var_2

print("Type  of var_3 is ",type(var_2))
print("Value of var_3 is ",var_3)

Output:


Type of var_1 is  <class 'int'>

Type of var_2 is  <class 'float'>

Type  of var_3 is  <class 'float'>
Value of var_3 is  180.98000000000002

In this example we see that int(lower data type) is automatically converted to float(higher data type).

eg.
var_1="80"
var_2=100
print("Type of var_1 is ",type(var_1))
print("Type of var_2 is ",type(var_2))

var_3=var_1+var_2

print("Type  of var_3 is ",type(var_2))
print("Value of var_3 is ",var_3)

Output:


Type of var_1 is  <class 'str'>

Type of var_2 is  <class 'int'>

Traceback (most recent call last):
  File "C:/Users/your name/PycharmProjects/firstproj/newfile.py"., line 5, in <module>
    var_3=var_1+var_2
TypeError: can only concatenate str (not "int") to str

In the above example we are getting typeerror as interpreter cannot perform implicit type conversion

Explicit Type Conversion
If we convert some data type to other datatype forcibly(i.e. manually) then it is called explicit type conversion.

Type conversion can be done by assigning the required data type function to the expression.

eg.
var_1="80"
var_2=100
print("Type of var_1 is ",type(var_1))
print("Type of var_2 is ",type(var_2))

var_1=int(var_1) # Converting string to integer datatype
var_3=var_1+var_2

print("Type of var_1 after conversion is is ",type(var_1))
print("Type  of var_3 is ",type(var_2))
print("Value of var_3 is ",var_3)
Output:


Type of var_1 is  <class 'str'>
Type of var_2 is  <class 'int'>
Type of var_1 after conversion is is  <class 'int'>
Type  of var_3 is  <class 'int'>
Value of var_3 is  180

In the above example we are converting string to integer datatype using int() function

eg.
var_1="80"
var_2=100
print("Type of var_1 is ",type(var_1))
print("Type of var_2 is ",type(var_2))

var_2=str(var_2) # Converting integer to string datatype
var_3=var_1+var_2

print("Type of var_2 after conversion is ",type(var_2))
print("Type  of var_3 is ",type(var_3))
print("Value of var_3 is ",var_3)
Output:


Type of var_1 is  <class 'str'>

Type of var_2 is  <class 'int'>

Type of var_2 after conversion is  <class 'str'>
Type  of var_3 is  <class 'str'>
Value of var_3 is  80100

In the above example we are converting integer to string datatype using str() function

eg.
var_1=100.58
var_2=100
print("Type of var_1 is ",type(var_1))
print("Type of var_2 is ",type(var_2))

var_1=int(var_1) # Converting string to integer datatype
var_3=var_1+var_2

print("Type of var_1 after conversion is is ",type(var_1))
print("Type  of var_3 is ",type(var_2))
print("Value of var_3 is ",var_3)

Output:


Type of var_1 is  <class 'float'>

Type of var_2 is  <class 'int'>

Type of var_1 after conversion is is  <class 'int'>
Type  of var_3 is  <class 'int'>
Value of var_3 is  200

In the above example we are converting float to integer datatype using int() function.
Similarly we can convert a datatype to float using float() function.Syntax is same.


Converting user input to int()

eg.
a=int(input("Enter first value")) # Converting input to integer
b=float(input("Enter second value"))# Converting input to float
c=int(input("Enter third value"))# Converting input to integer
print("Type of a is",type(a))
print("Type of b is",type(b))
print("Type of c is",type(c))
print("Addition of numbers is",a+b+c)

Output:


Enter first value23

Enter second value34.56

Enter third value12
Type of a is <class 'int'>
Type of b is <class 'float'>
Type of c is <class 'int'>
Addition of numbers is 69.56

In the above example we are converting user input to integer and float. By default the input() takes values as string. To know more Click here

Comments

Popular posts from this blog

How to install python IDE ? IDLE and Pycharm

How to install Python IDE on Windows 10(Pycharm and IDLE)? Pycharm is the best Python IDE available.It comes with many in -built features. We will discuss its shortcuts in our next blogs.Pycharm is a cross-platform editor developed by jetbrains.   Installing Python Step 1) Visit the official website of python or click the below link              https://www.python.org/downloads/  .  Step 2) Click on Download Python               Step 3) After the download is complete ,open the exe file and click               on Install now  Step 4) After the successful installation it will show you a message         Setup was successful Step 5) Now go to your windows search bar and type "IDLE"              Press Enter and you will see that idle is opened Installing Pycharm St...

String Slicing in python

String Slicing Python  Slicing refers to extracting some words from a string. As in many other languages there are many different concepts for extracting a letters or words from a string ,in python we are having the concept of slicing. We can also the same things using looping, we will learn about it in our next tutorials. Indexing in Strings Before starting slicing, we need to understand the indexing in python. We have the concept of positive indexing  and  negative indexing.  Positive indexing starts from 0 to (length of string).It goes from forward to backward. Negative indexing starts from -1 to -(length of string + 1). It is illustrated in below example Below are some examples of how to use string slicing to extract characters from string. Accessing the characters in a string using positive index eg. string= "We are studying string slicing" s=string[ 0 ] # A single character will be printed present at index 0 w=string[ 4 ] # A...