A Byte of Python

Operators

We will briefly take a look at the operators and their usage.

Tip

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

OperatorNameExplanationExamples
+PlusAdds the two objects3 + 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.
*MultiplyGives the 'multiplication' of two objects. Strings are repeated.2 * 3 gives 6. 'la' * 3 gives the string 'lalala'.
**PowerReturns x to the power of y3 ** 4 gives 81 (i.e. 3 * 3 * 3 * 3)
/DivideDivide x by y4/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 DivisionReturns the floor of the quotient4 // 3.0 gives 1.0.
%ModuloReturns the remainder of the division 8%3 gives 2. -25.5%2.25 gives 1.5.
<<Left ShiftShifts 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 ShiftShifts 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 ANDBitwise AND of the numbers where each corresponding bit of the two numbers are multiplied to give a new number5 & 3 gives 1 since 0101 & 0011 gives 0001 (binary representations).
|Bitwise ORBitwise 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 XORBitwise 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 invertFlip 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 thanReturns 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 thanReturns whether x is greater than y5 > 3 gives True.
<=Less than or equal toReturns whether x is less than or equal to yx = 3; y = 6; x <= y returns True.
>=Greater than or equal toReturns whether x is greater than or equal to yx = 4; y = 3; x >= y returns True.
==Equal toCompare if two objects are equalx = 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 toCompares if the objects are not equalx = 2; y = 3; x != y returns True.
notBoolean NOTif x is True, return False. if x is False, return True.x = True; not y returns False.
andBoolean 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.
orBoolean ORIf 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.