Converts a value to a whole number.
Synopsis
int(value)
Description
Use int on what input returns to get a number you can do maths with.
On a float, int drops everything after the decimal point. This is called truncation: int(3.9) is 3, and int(-3.9) is -3.
Parameters
| Name | Meaning |
|---|---|
value |
A string of digits, such as "42", or a float. |
Return value
An int.
Example
age = int(input("Age: "))
print(age + 1)
print(int(3.9))
print(int(-3.9))
It prints:
Age: 14
15
3
-3
Common mistake
n = int(input("Number: "))
Python stops with:
Number: cat
ValueError: invalid literal for int() with base 10: 'cat'
The string must hold only a whole number. "cat" fails, and so does "3.5". To accept decimals, use float.
See also
Last updated 2 weeks ago