Operators
– special symbols that represent computations like addition and multiplication.
The values of the operator is applied to are called operands.
Basic operators are given in the following table
Character
|
Function
|
+
|
Addition
|
-
|
Subtraction
|
*
|
Multiplication
|
/
|
Division
|
**
|
exponentiation
|
Example
of the mathematical operations is given below
>>> 20 + 24 44 >>> hour = 3 >>> hour -1 2 >>>minutes = hour * 60 >>>(50 + 9)*(15-7) 472 >>>52/60 0
As
you can see from previous code the division operator might not do what you
expect. The result of 52/60 is 0.86667
not 0. The reason for that is that Python is performing floor division. In
older versions of Python before 3.0 the result is integer while in Python 3.0
the result of this division is a float.
Generally
when both of the operands are integer, the result is also an integer, floor
division chops off the fraction part, so in this example it rounds down to
zero.
If
either of the operands is a floating-point number, Python performs
floating-point division, and the result is a float:
>>>52.0/60.0 0.8666666666666667
Nema komentara:
Objavi komentar