0

what’s the difference between a float and a regular number? (i’m learning python)

19th Aug 2021, 10:13 PM
Lila Colon
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
19th Aug 2021, 10:28 PM
Rik Wittkopp
Rik Wittkopp - avatar
+ 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
20th Aug 2021, 6:34 AM
Shadoff
Shadoff - avatar
+ 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
20th Aug 2021, 7:24 AM
Lothar
Lothar - avatar
+ 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
20th Aug 2021, 7:40 AM
Lisa
Lisa - avatar
+ 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()
20th Aug 2021, 8:12 AM
Lothar
Lothar - avatar
+ 1
Thanks Lothar Once again you have looked deeper into a problem and made a valid comment. Each day is a new lesson 😁👍
20th Aug 2021, 8:39 AM
Rik Wittkopp
Rik Wittkopp - avatar