0
what’s the difference between a float and a regular number? (i’m learning python)
6 Respostas
+ 7
A regular number is called an integer, which may also be defined as a number which when divided by 1, has 0 remainder.
Num %1 == 0 - > true for integer
a decimal number is referred to as a float.
Decimal % 1 != 0 -> true for float
+ 3
integer do not have POINT and digits after point.
as to say whole number.
float is number where DOT can move forth and back.
that's why it is float.
The dot is floating!
2.3
21.3
45.345
+ 3
Rik Wittkopp ,
> Decimal % 1 != 0 -> true for float < does not necessarily result True. it depends on the value in Decimal:
Decimal = 7.0
print(Decimal % 1 != 0) #-> False
Decimal = 7.01
print(Decimal % 1 != 0) #-> True
+ 3
Lothar
Just as an addition: We could use is_integer() to check if the float is integer-like and if so, then cast it to integer
+ 3
Lisa ,
thanks, you are right!
▪︎if we can be sure that the number is a float, we can call the float built-in method is_integer().
▪︎if the number is an int, the call will fail
to be sure what data type the number is, we can use the buil-in functions type() or isinstance()
+ 1
Thanks Lothar
Once again you have looked deeper into a problem and made a valid comment.
Each day is a new lesson
😁👍