.
>>> 11 / 2 ①
5.5
>>> 11 // 2 ②
5
>>> −11 // 2 ③
−6
>>> 11.0 // 2 ④
5.0
>>> 11 ** 2 ⑤
121
>>> 11 % 2 ⑥
1
① | / . float, , , int. |
② | // . , , ( ) , . |
③ | , // . , , . . −6 −5; , , −5. |
④ | // . float, , float. |
⑤ | ** . 112 121. |
⑥ | % . 11, 2, 5 1 , 1. |
Python 2, / , . Python 3, / . . PEP 238. |
Python . , .
>>> import fractions ①
>>> x = fractions.Fraction(1, 3) ②
>>> x
Fraction(1, 3)
>>> x * 2 ③
Fraction(2, 3)
>>> fractions.Fraction(6, 4) ④
Fraction(3, 2)
>>> fractions.Fraction(0, 0) ⑤
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "fractions.py", line 96, in __new__
raise ZeroDivisionError('Fraction(%s, 0)' % numerator)
ZeroDivisionError: Fraction(0, 0)
:
( ):
"<stdin>", 1, <>
"fractions.py", 96, __new__
raise ZeroDivisionError('(%s, 0)' % )
ZeroDivisionError: (0, 0)
① | , fractions. |
② | , Fraction . |
③ | . Fraction. |
④ | Fraction . |
⑤ | Python , . |
|
|
Python .
>>> import math
>>> math.pi ①
3.1415926535897931
>>> math.sin(math.pi / 2) ②
1.0
>>> math.tan(math.pi / 4) ③
0.99999999999999989
① | math π . |
② | math , sin(), cos(), tan(), asin(). |
③ | , , Python . 1.0, 0.99999999999999989. |