411 REFERENCE · Wikibooks
Mathematics with Python and Ruby/Whole numbers in Python
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Whole numbers are not the only numbers, as we will see in the following chapters. So what does Python do to know if a number is whole? As the language is weakly typed, it must guess. The criterium is simple: in order for a number to be whole, it must not have a decimal point.
400px|right
Whole numbers in Python
Thus, if one enters
a = 3
print(type(a))
b = 3.14
print(type(b))
c = int(b)
print(c)
one notes that Python knows that a is whole, that b is not, and that c can be whole although it was obtained from b (which is real).
Certain calculations that should give a whole result do not always do so in Python. For example, while , Python treats this number as a real number (not a whole number)!
from math import sqrt
a = sqrt(100)
print(a.is_integer())
Operations
Addition, subtraction and multiplication
The first three operations are denoted by the symbols +, - and * as in most programming languages. The sum, difference and product of two (or more) whole numbers are all integers.
a=5
b=-8
print(a+b)
print(a-b)
print(a*b)