Saturday, April 4, 2015

int() - Python Tutorial

int() in python



 


int() Python Built-in Function


int() is a built-in function that will convert a floating point number or string to an integer type. If a user or our program returns a string or a float we can use the int() function to convert the number to integer. The int() function rounds the float down to the floor check the examples below.


int() Built-in Function Example


#int() Examples

#int() example
>>> int(8.7)
8

#int() example
>>> int(9.876)
9

#int() example
>>> int(5.3)
5

#int() example
>>> int(6)
6

#int() example
>>> int("5677")
5677

#This does not work we can not convert a float string but check next example
>>> int("5.98")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '5.98'

#The fix to above example
>>> int(float("5.98"))
5

If you have any questions about int() built-in function leave a comment below.


 



No comments:

Post a Comment