We will briefly take a look at the operators and their usage.
You can evaluate expressions using the interactive prompt.
For example, run 2+3
at the prompt and get
instant results.
>>> 2 + 3 5 >>> 3 * 5 15
Table 5.1. Operators and their usage
Operator | Name | Explanation | Examples |
---|---|---|---|
+ | Plus | Adds the two objects | 3 + 5 gives
8 . 'a' + 'b'
gives the string
'ab' . |
- | Minus | 'Subtract' one object's value from the other. It is also used for negative numbers. | -5.2 gives
a negative number. 50 - 24 gives
26 . |
* | Multiply | Gives the 'multiplication' of two objects. Strings are repeated. | 2 * 3 gives
6 . 'la' * 3
gives the string
'lalala' . |
** | Power | Returns x to the power of y | 3 ** 4 gives
81 (i.e. 3 * 3 * 3 *
3 ) |
/ | Divide | Divide x by y | 4/3 gives
1 . Notice that division of integers also returns an
integer. 4.0/3 or 4/3.0
gives 1.3333333333333333 . Notice that
division involving float numbers return a float
number. |
// | Floor Division | Returns the floor of the quotient | 4 // 3.0 gives
1.0 . |
% | Modulo | Returns the remainder of the division |
8%3 gives 2 .
-25.5%2.25 gives
1.5 . |
<< | Left Shift | Shifts the bits of the number to the left by specified amount. Numbers are represented in the computer's memory using binary digits i.e. 0s and 1s. | 2 is represented as
0010 in binary. 2 <<
1 gives 4 because its binary representation is
0100 . |
>> | Right Shift | Shifts the bits of the number to the right by specified amount. | 8
is represented as 1000 in binary.
8 >> 3 gives 1 because its binary
representation is 0001 . |
& | Bitwise AND | Bitwise AND of the numbers where each corresponding bit of the two numbers are multiplied to give a new number | 5 & 3 gives
1 since 0101 & 0011
gives 0001 (binary
representations). |
| | Bitwise OR | Bitwise OR of the numbers where each corresponding bit of the two numbers are ORed. | 5 | 3
gives 7 since 0101 |
0011 gives 0111 (binary
representations). |
^ | Bitwise XOR | Bitwise XOR of the numbers where each corresponding bit of the two numbers are XORed. | 5 ^ 3
gives 6 since 0101 %
0011 gives 0011 (binary
representations). |
~ | Bitwise invert | Flip each bit of the binary representation of the number | ~5 gives
-6 since ~0101 gives
1010 (binary representations, note that negative
numbers are indicated using 1's complement, explanation
of this is beyond the scope of this book) |
< | Less than | Returns whether x is less than y. All
comparison operators return True or
False . | 5 <
3 gives False . Note that
comparisons can be chained arbitrarily. For example,
3 < 5 < 7 gives
True . |
> | Greater than | Returns whether x is greater than y | 5 > 3 gives
True . |
<= | Less than or equal to | Returns whether x is less than or equal to y | x = 3; y = 6; x <= y
returns True . |
>= | Greater than or equal to | Returns whether x is greater than or equal to y | x = 4; y = 3; x >= y
returns True . |
== | Equal to | Compare if two objects are equal | x = 2; y = 2;
x == y returns True . x
= 'str'; y = 'stR'; x == y
returns False . x =
'str'; y = 'str'; x == y
returns True . |
!= | Not equal to | Compares if the objects are not equal | x = 2; y = 3; x != y
returns True . |
not | Boolean NOT | if x
is True , return False .
if x is False , return
True . | x = True; not
y returns False . |
and | Boolean AND |
x and y returns False
if x is False , otherwise is returns
evaulation of y. | x = False; y = True;
x and y returns False since x is
False. In this case, Python will not evaulate y since it
knows that the value of the expression will have to be False
since x is False. This is called short-circuit
evaluation. |
or | Boolean OR | If x is
True , return True, else return the
evaluation of y. | x = True; y = False;
x or y returns True .
Short-circuit evaulation applies here as well. |