0% found this document useful (0 votes)
46 views

Python Cast Data Types

Python allows explicit conversion between different data types like integer, float, and string using functions like int(), float(), and str(). Integer to float conversion retains decimal places while float to integer truncates them. String to number conversion uses these functions to interpret string values as numbers, but conversion fails if the string contains non-numeric characters.

Uploaded by

Nandini Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Python Cast Data Types

Python allows explicit conversion between different data types like integer, float, and string using functions like int(), float(), and str(). Integer to float conversion retains decimal places while float to integer truncates them. String to number conversion uses these functions to interpret string values as numbers, but conversion fails if the string contains non-numeric characters.

Uploaded by

Nandini Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Python Cast Data Types

Samatrix Consulting Pvt Ltd

https://samatrix.io (https://samatrix.io)

In Python, we use data types to classify the type of the data for example int , float , str etc.
However in programming, we need to convert the data type between different data type
Python does not support implicit conversion excet int to float
So we need to cast data types

Integer to Float
There are instances when you need to convert int type to float type. Even though python implicitly
converts int to float . However, sometimes it is required to convert int to float manually or float to
int

float() method in Python can be used to convert int to float

In [1]: a=56
float(a) # Converts int to float by addition decimal point

Out[1]: 56.0

In [4]: a = 56 #Int can be assigned to variable that can be converted to float


a = float(a)
print(a)

56.0

Float to Integer

Python can convert float to integer by using int() method


In the case of conversion from float to integer, python truncates the values after decimal

In [2]: int(45000.789)

Out[2]: 45000
The float can be assigned to a variable and then converted into integer

In [6]: a = 45.789
int(a)

Out[6]: 45

Python does not round the float when converting the float to int. It just cutoff the values after decimal

Number converted through division

in Python 3, the division of two integers returns float

In [7]: 4/2

Out[7]: 2.0

In [4]: 5/2

Out[4]: 2.5

You can use floor operator // to return integer

In [9]: 5//2

Out[9]: 2

Converting Number to String

In various operation, it becomes essential to convert numbers to string


For exmple the concatenation of number with string requires the number to be converted to string
Python does not explicitly converts number to string
We can use str() method to convert number to strinf manually

In [2]: 2+' Python' # Concatenation of Number with string gives error

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-c11b81a42b78> in <module>
----> 1 2+' Python' # Concatenation of Number with string gives error

TypeError: unsupported operand type(s) for +: 'int' and 'str'


In [1]: '2' + 'nd Python'

Out[1]: '2nd Python'

To convert number to string use str() method

In [8]: a=str(2)
type(a)

Out[8]: str

In [3]: s = 2
print(str(s) + 'nd Python')

2nd Python

Similarly we can convert float to string

In [20]: str(56.897)

Out[20]: '56.897'

In [5]: sr='234'
int(sr)

Out[5]: 234

In [6]: st='234j'
int(st)

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-6-6a6a2aec2236> in <module>
1 st='234j'
----> 2 int(st)

ValueError: invalid literal for int() with base 10: '234j'

In [21]: a = 56.897
print('I have '+str(a)+' points')

I have 56.897 points

String to Number
In programming, on various ocassion, we need to convert the string values to numbers
For example the mathematical operations on strings need conversion to numbers (integer or float)
We use int() or float() methods to convert string to number
In [15]: var1='123.45'
float(var1)

Out[15]: 123.45

In [3]: var='hell0123456'
float(var)

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-3-86843a3fe513> in <module>
1 var='hell0123456'
----> 2 float(var)

ValueError: could not convert string to float: 'hell0123456'

In [24]: a = '58'
b = '50'
a+b
int(a) + int(b)
c = int(a) - int(b)
c

Out[24]: 8

In [8]: a=67
b=30.90
int(a+b)

Out[8]: 97

In [25]: float('78.98')

Out[25]: 78.98

In [26]: a = '89.76'
b = '78.67'
c = float(a) - float(b)
c

Out[26]: 11.090000000000003

Thank You

You might also like