1. 程式人生 > 實用技巧 >20201228-1 Numbers

20201228-1 Numbers

Numbers
2-1
>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 8 / 5  # division always returns a floating point number
1.6

2-2
>>> 17 / 3  # classic division returns a float
5.666666666666667
>>>
>>> 17 // 3  # floor division discards the fractional part
5 >>> 17 % 3 # the % operator returns the remainder of the division 2 >>> 5 * 3 + 2 # result * divisor + remainder 17 2-3 >>> 5 ** 2 # 5 squared 25 >>> 2 ** 7 # 2 to the power of 7 128 3-1 In interactive mode, the last printed expression is assigned to the variable _. This means that when you are using Python as a desk calculator, it
is somewhat easier to continue calculations, for example: >>> tax = 12.5 / 100 >>> price = 100.50 >>> price * tax 12.5625 >>> price + _ 113.0625 >>> round(_, 2) 113.06 This variable should be treated as read-only by the user. Don’t explicitly assign a value to it — you would create an independent local variable
with the same name masking the built
-in variable with its magic behavior.