I have a question: Does python have a whole number data type> Unlike integers and floats, whole numbers are non-negative (0 to infinity). Does python have a data type declaration for that?
Asked
Active
Viewed 415 times
3
-
int is whole numbers but it includes negatives, you could create a new subclass if you wanted, it would be easy to implement. float numbers include decimals. – Alan Kavanagh Dec 21 '17 at 21:03
-
so your conclusion is python don't have whole number data types. sir? – Chrisyantar Hasiholan Dec 21 '17 at 21:05
-
Just positive whole numbers? Python doesnt have this type by default. It would need to be implemented – Alan Kavanagh Dec 21 '17 at 21:05
-
If @ChrisyantarHasiholan does subclass `int`, he might find https://stackoverflow.com/questions/3238350/subclassing-int-in-python helpful. – Robᵩ Dec 21 '17 at 21:06
-
how many whole numbers do you need? If all than you must take into account that the cardinality of the sets of integers and whole numbers is equal, so you don't gain anything – freude Dec 21 '17 at 21:07
-
thanks for the answer, i think i will just give the code a bound to positive number. i just curious if there is python data type like i'm mentioned earlier. – Chrisyantar Hasiholan Dec 21 '17 at 21:17
-
@ChrisyantarHasiholan: Why do you need a non-negative integer data type? – Blender Dec 21 '17 at 21:18
-
@Blender i need to measuring distance some object using sensor and python, and because its a distance, it can't be a negative. my sensor have some deviation and sometimes causing the distance become negatives number which is impossible right, so i need to code it on python to reduce my sensor error. – Chrisyantar Hasiholan Dec 21 '17 at 21:59
-
@ChrisyantarHasiholan: I'm not sure why you would want a new data type for that when you can just check `if distance < 0: ...`. – Blender Dec 21 '17 at 22:27
-
@Blender yeah, thats what im doing right now. like i said before, i'm just curious actually – Chrisyantar Hasiholan Dec 22 '17 at 07:24
1 Answers
2
No, it doesn't have such a number as a native type. You could use int
, and just never store a negative number. Or you could create your own type and limit its value in its methods.

Robᵩ
- 163,533
- 20
- 239
- 308