How To Change Data Type In Python
At that place are lots of datatype conversion which are possible in python. I am list some of the commonly used conversions which are important.
- string to integer
- integer to cord
- integer to float
- float to integer
- cord to appointment
string to integer
This blazon of conversion is used when demand to perform mathematical operations on a part of string which is number.
1 2 3 four five 6 7 8 9 10 11 12 13 14 15 16 17 | '' 'string to integer datatype conversion' '' # defining int every bit an cord intVar = '10' # Print datatype earlier conversion print ( 'Datatype Before conversion' , type ( intVar ) ) # Converting the datatype to integer intVar = int ( intVar ) # Print datatype after conversion print ( 'Datatype afterward conversion' , blazon ( intVar ) ) # mutual use in performing mathematical opeations # which is not possible on strings print ( 'The percentage of products sold were' , 100 * ( intVar / 100 ) , '%' ) |
Sample Output

integer to string
This type of conversion is commonly used when nosotros need to combine a string and number together into a unmarried string message.
i two three 4 5 6 7 8 9 x 11 12 13 14 15 16 17 | '' 'integer to string datatype conversion' '' # defining an integer variable intVar = 10 # Print datatype earlier conversion print ( 'Datatype Before converion' , type ( intVar ) ) # Converting the datatype to integer intVar = str ( intVar ) # Print datatype after conversion print ( 'Datatype subsequently conversion' , type ( intVar ) ) # common use in creating custom messages strMessage = 'The number of products sold are ' + str ( 200 ) + ' units' impress ( strMessage ) |
Sample Output

integer to float
This type of datatype conversion is used when we need decimal signal precision in the numbers.
ane two 3 4 five half-dozen 7 8 9 10 11 12 13 14 15 16 17 18 | # integer to float datatype conversion # defining an integer variable intVar = 10 # Print datatype before conversion impress ( 'Datatype Before conversion' , blazon ( intVar ) ) # Converting the datatype to integer intVar = float ( intVar ) # Print datatype later on conversion print ( 'Datatype later conversion' , blazon ( intVar ) ) # mutual use in getting decimal precision strMessage = 'The percentage of products sold are ' + str ( float ( 10 ) ) + ' units' print ( strMessage ) |
Sample Output

float to integer
This type of conversion is unremarkably used when we are representing values which are quantities and generated as a result of some division operation.
1 two three 4 5 6 7 8 ix ten eleven 12 xiii 14 15 16 17 xviii 19 | # float to integer datatype conversion # defining an integer variable floatVar = 10.22 # Impress datatype before conversion print ( 'Datatype Before conversion' , type ( floatVar ) ) # Converting the datatype to integer # and assigning to new variable intVar = int ( floatVar ) # Print datatype after conversion print ( 'Datatype after conversion' , type ( intVar ) ) # common apply in representing quantities strMessage = 'The production per person are ' + str ( int ( 200 / 22 ) ) + ' units' print ( strMessage ) |
Sample Output

String to Engagement
Date is a special datatype, although date values look like strings only, in that location are many operations we tin perform using a date like comparison, difference in appointment etc. Most of fourth dimension you get a appointment string equally an input to your applications/programs. Hence, we must convert that string blazon to engagement type using library "datetime" in order to perform the appointment operations.
1 ii 3 four 5 6 7 8 9 10 11 12 13 xiv 15 16 17 xviii nineteen twenty 21 22 | # String to Date datatype conversion # defining an string variable stringDate = '21-ten-2020' # Print datatype before conversion print ( 'Datatype Before conversion' , blazon ( stringDate ) ) # Converting the datatype to Date # and assigning to new variable import datetime StartDate = datetime . datetime . strptime ( stringDate , '%d-%k-%Y' ) # Print datatype later conversion print ( 'Datatype after conversion' , type ( StartDate ) ) print ( StartDate ) # common utilize in finding departure in days from other date Deadline = datetime . datetime ( 2021 , 12 , 24 ) DateDifference = Deadline - StartDate print ( 'Difference in the deadline and start date is:' , DateDifference . days , 'days' ) |
Sample Output

Popular Cord to engagement formats
# importing the library import datetime # Generating dates using various string input formats impress ( datetime . datetime . strptime ( '22-Apr-2020' , '%d-%B-%Y' ) ) print ( datetime . datetime . strptime ( '22-04-2020' , '%d-%1000-%Y' ) ) print ( datetime . datetime . strptime ( '04/22/2020' , '%m/%d/%Y' ) ) print ( datetime . datetime . strptime ( 'Mar/22/2020' , '%b/%d/%Y' ) ) |
Sample Output

Lead Data Scientist
Farukh is an innovator in solving industry problems using Artificial intelligence. His expertise is backed with 10 years of industry experience. Existence a senior data scientist he is responsible for designing the AI/ML solution to provide maximum gains for the clients. As a thought leader, his focus is on solving the central business problems of the CPG Industry. He has worked across different domains like Telecom, Insurance, and Logistics. He has worked with global tech leaders including Infosys, IBM, and Persistent systems. His passion to teach inspired him to create this website!
How To Change Data Type In Python,
Source: https://thinkingneuron.com/how-to-change-variable-datatype-in-python/
Posted by: donohueyoulle64.blogspot.com
0 Response to "How To Change Data Type In Python"
Post a Comment