Concept

int()

The int function. cantake a floating point number or a string, and turn it into an int. For floating point numbers, it discards the decimal portion of the number, a process called "truncation towards zero" on the number line.

Example:

print(3.14, int(3.14)) print(3.9999, int(3.9999)) # This doesn't round to the closest int! print(3.0, int(3.0)) print(-3.999, int(-3.999)) # Note that the result is closer to zero print("2345", int("2345")) # parse a string to produce an int print(17, int(17)) # int even works on integers print(int("23bottles"))
3.14 3 3.9999 3 3.0 3 -3.999 -3 2345 2345 17 17

The last line throws a ValueError as the int() function does not take String literals.

0

1

Updated 2021-06-29

Contributors are:

Who are from:

Tags

Python Programming Language

Data Science

Related