Math‎ > ‎

types


# check type
>>> x=5
>>> type(x)
<type 'int'>

>>> x=5.8
>>> type(x)
<type 'float'>


# convert float to integer, use 'round' before converting
>>> x=5.8
>>> int(x)
5

>>> int(round(x))
6


# convert number to string
>>> x=5.8
>>> str(x)
'5.8'


# check for data type 'float' and raise error if wrong
x=5
if not isinstance(x,float):
    raise TypeError('x is not of type float')
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: x is not of type float