Numbers
Python has two everyday number types:
| Type | What it stores | Example |
|---|---|---|
int | Whole numbers | 42 |
float | Decimal numbers | 3.5 |
items = 6
price = 2.5
total = items * price
print(type(items))
print(total)
print(7 / 2)
print(2 ** 64)
Output:
<class 'int'>
15.0
3.5
18446744073709551616
When you do math with an integer and a float together, the result is a float. That is why items * price gives 15.0. Dividing with / always gives a float too, even when both numbers are integers. So 7 / 2 gives 3.5.
** raises a number to a power. The last line shows that a Python integer can be as large as it needs to be. Python prints every digit of 2 to the power 64.
The computer stores floats in binary (ones and zeros). Because of this, most decimals are stored as a value that is very close, but not exact. For example, 0.1 + 0.2 gives 0.30000000000000004. This is normal for floats. It is also why programs often store money as a whole number of cents.
You can put underscores in a long number to make it easier to read. 1_000_000 is the same value as 1000000.
In the exercise, finish the whole-number, decimal, and power calculations.