Completion requirements
Read this to learn more about operators and expressions.
We will briefly take a look at the operators and their usage.
Note that you can evaluate the expressions given in the examples using the interpreter interactively. For example, to test the expression 2 + 3
, use the interactive Python interpreter prompt:
>>> 2 + 3 5 >>> 3 * 5 15 >>>
Here is a quick overview of the available operators:
-
+
(plus)- Adds two objects
3 + 5
gives8
.'a' + 'b'
gives'ab'
.
-
-
(minus)- Gives the subtraction of one number from the other; if the first operand is absent it is assumed to be zero.
-5.2
gives a negative number and50 - 24
gives26
.
-
*
(multiply)- Gives the multiplication of the two numbers or returns the string repeated that many times.
2 * 3
gives6
.'la' * 3
gives'lalala'
.
**
(power)- Returns x to the power of y
3 ** 4
gives81
(i.e.3 * 3 * 3 * 3
)
-
/
(divide)- Divide x by y
13 / 3
gives4.333333333333333
-
//
(divide and floor)- Divide x by y and round the answer down to the nearest integer value. Note that if one of the values is a float, you'll get back a float.
13 // 3
gives4
-13 // 3
gives-5
9//1.81
gives4.0
-
%
(modulo)- Returns the remainder of the division
13 % 3
gives1
.-25.5 % 2.25
gives1.5
.
-
<<
(left shift)- Shifts the bits of the number to the left by the number of bits specified. (Each number is represented in memory by bits or binary digits i.e. 0 and 1)
2 << 2
gives8
.2
is represented by10
in bits.- Left shifting by 2 bits gives
1000
which represents the decimal8
.
-
>>
(right shift)- Shifts the bits of the number to the right by the number of bits specified.
11 >> 1
gives5
.11
is represented in bits by1011
which when right shifted by 1 bit gives101
which is the decimal5
.
-
&
(bit-wise AND)- Bit-wise AND of the numbers: if both bits are
1
, the result is1
. Otherwise, it's0
. 5 & 3
gives1
(0101 & 0011
gives0001
)
- Bit-wise AND of the numbers: if both bits are
-
|
(bit-wise OR)- Bitwise OR of the numbers: if both bits are
0
, the result is0
. Otherwise, it's1
. 5 | 3
gives7
(0101 | 0011
gives0111
)
- Bitwise OR of the numbers: if both bits are
-
^
(bit-wise XOR)- Bitwise XOR of the numbers: if both bits (
1 or 0
) are the same, the result is0
. Otherwise, it's1
. 5 ^ 3
gives6
(O101 ^ 0011
gives0110
)
- Bitwise XOR of the numbers: if both bits (
-
~
(bit-wise invert)- The bit-wise inversion of x is -(x+1)
~5
gives-6
. More details at http://stackoverflow.com/a/11810203
-
<
(less than)- Returns whether x is less than y. All comparison operators return
True
orFalse
. Note the capitalization of these names. 5 < 3
givesFalse
and3 < 5
givesTrue
.- Comparisons can be chained arbitrarily:
3 < 5 < 7
givesTrue
.
- Returns whether x is less than y. All comparison operators return
-
>
(greater than)- Returns whether x is greater than y
5 > 3
returnsTrue
. If both operands are numbers, they are first converted to a common type. Otherwise, it always returnsFalse
.
-
<=
(less than or equal to)- Returns whether x is less than or equal to y
x = 3; y = 6; x <= y
returnsTrue
-
>=
(greater than or equal to)- Returns whether x is greater than or equal to y
x = 4; y = 3; x >= 3
returnsTrue
-
==
(equal to)- Compares if the objects are equal
x = 2; y = 2; x == y
returnsTrue
x = 'str'; y = 'stR'; x == y
returnsFalse
x = 'str'; y = 'str'; x == y
returnsTrue
-
!=
(not equal to)- Compares if the objects are not equal
x = 2; y = 3; x != y
returnsTrue
-
not
(boolean NOT)- If x is
True
, it returnsFalse
. If x isFalse
, it returnsTrue
. x = True; not x
returnsFalse
.
- If x is
-
and
(boolean AND)x and y
returnsFalse
if x isFalse
, else it returns evaluation of yx = False; y = True; x and y
returnsFalse
since x is False. In this case, Python will not evaluate y since it knows that the left hand side of the 'and' expression isFalse
which implies that the whole expression will beFalse
irrespective of the other values. This is called short-circuit evaluation.
-
or
(boolean OR)- If x is
True
, it returns True, else it returns evaluation of y x = True; y = False; x or y
returnsTrue
. Short-circuit evaluation applies here as well.
- If x is