In Python, operators are special symbols that designate that some sort of logic (like computation) to be performed. The values that an operator acts on are called operands.
a = 50 |
In the above example, the + operator adds the operands a and b together. An operand can be either a literal value or a variable that references an object.
In this chapter we will see how calculations can be performed on objects in Python. Objective of this chapter is to be able to create complex expressions by combining objects and operators. The table below lists the different types of operators and their that type.
Table 1: List of Operators
Operators |
Description |
Arithmetic Operators: arithematic operators supported by Python |
|
+ (unary) |
+a , doesn’t do anything |
+ (binary plus) |
Adds two objects: 3 + 5 gives 8 . ‘a’ + ‘b’ gives ‘ab’ . |
-(unary) |
-a, opposite sign of given value |
– (minus) |
Gives the subtraction of one number from the other; if the first operand is absent it is assumed to be zero. |
* (multiply) |
Gives the multiplication of the two numbers or returns the string repeated that many times : 2 * 3 gives 6 . ‘la’ * 3 gives ‘lalala’. |
** (power) |
Returns x to the power of y: 3 ** 4 gives 81 (i.e. 3 * 3 * 3 * 3 ) |
/ (divide) |
Divide x by y: 13 / 3 gives 4 . 13.0 / 3 gives 4.333333333333333 |
// |
a//b (Floor Division or Integer Division: Quotient when a is divided by b, rounded to the next smallest whole number |
% (modulo) |
Returns the remainder of the division: 13 % 3 gives 1 . -25.5 % 2.25 gives 1.5 |
Bitwise Operators: treat operands as sequences of binary digits and operate on them bit by bit.
|
|
<< (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 gives 8 . 2 is represented by 10 in bits. Left shifting by 2 bits gives 1000 which represents the decimal 8 . |
>> (right shift) |
Shifts the bits of the number to the right by the number of bits specified. 11 >> 1 gives 5. 11 is represented in bits by 1011 which when right shifted by 1 bit gives 101`which is the decimal `5 . |
& (bit-wise AND) |
Bit-wise AND of the numbers: 5 & 3 gives 1 . |
| (bit-wise OR) |
Bitwise OR of the numbers: 5 | 3 gives 7 |
^ (bit-wise XOR) |
Bitwise XOR of the numbers: 5 ^ 3 gives 6 |
~ (bit-wise invert) |
The bit-wise inversion of x is -(x+1): ~5 gives -6 . More details at http://stackoverflow.com/a/11810203 |
Comparison Operators: comparing the values
|
|
< (less than) |
Returns whether x is less than y. All comparison operators return True or False. Note the capitalization of these names. 5 < 3 gives False and 3 < 5 gives True . Comparisons can be chained arbitrarily: 3 < 5 < 7 gives True. |
> (greater than) |
Returns whether x is greater than y 5 > 3 returns True . If both operands are numbers, they are first converted to a common type. Otherwise, it always returns False |
# (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 >= 3 returns True |
== (equal to) |
Compares if the 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 . |
Logical Operators: expressions evaluated in Boolean context |
|
not (boolean) |
If x is True , it returns False . If x is False , it returns True . x = True; not x returns False . |
and (boolean) |
x and y returns False if x is False , else it returns evaluation of y x = False; y = True; x and y returns False since x is False. In this case, Python will not evaluate y since it knows that the left hand side of the ‘and’ expression is False which implies that the whole expression will be False irrespective of the other values. This is called short-circuit evaluation. |
or (boolean) |
If x is True , it returns True, else it returns evaluation of y x = True; y = False; x or y returns True . Short-circuit evaluation applies here as well. |
Assignment Operators |
|
= |
Assigns values from right side operands to left side operand c = a + b assigns value of a + b into c |
+= |
It adds right operand to the left operand and assign the result to left operand c += a is equivalent to c = c + a |
-= |
It subtracts right operand from the left operand and assign the result to left operand c -= a is equivalent to c = c – a |
*= |
It multiplies right operand with the left operand and assign the result to left operand c *= a is equivalent to c = c * a |
/= |
It divides left operand with the right operand and assign the result to left operand c /= a is equivalent to c = c / ac /= a is equivalent to c = c / a |
%= |
It takes modulus using two operands and assign the result to left operand c %= a is equivalent to c = c % a |
**= |
Performs exponential (power) calculation on operators and assign value to the left operand c **= a is equivalent to c = c ** a |
//= |
It performs floor division on operators and assign value to the left operand c //= a is equivalent to c = c // a |
Short Hand Assignment Operators |
|
+= |
Adds 2 numbers and assigns the result to left operand. x+=y sam as: x = x + y |
-= |
Subtracts 2 numbers and assigns the result to left operand. x-= y sam as: x = x –y |
*= |
Multiplies 2 numbers and assigns the result to left operand. x*= y sam as: x = x*y |
/= |
Divides 2 numbers and assigns the result to left operand. x/= y sam as: x = x/y |
%= |
Computes the modulus of 2 numbers and assigns the result to left operand. x%= y sam as: x = x%y |
**= |
Performs exponential (power) calculation on operators and assign value to the equivalent to left operand. x**=y sam as: x = x**y |
//= |
Performs floor division on operators and assign value to the left operand. x//=y sam as: x = x//y |
Memberships Operators |
|
in |
Evaluates to true if it finds a variable in the specified sequence and false otherwise. x in y, here in results in a 1 if x is a member of sequence y. |
not in |
Evaluates to true if it does not finds a variable in the specified sequence and false otherwise. x not in y, here not in results in a 1 if x is not a member of sequence y. |
Identity Operators |
|
is |
Evaluates to true if the variables on either side of the operator point to the same object and false otherwise. x is y, here is results in 1 if id(x) equals id(y). |
is not |
Evaluates to false if the variables on either side of the operator point to the same object and true otherwise. x is not y, here is not results in 1 if id(x) is not equal to id(y). |
Shortcut for Math operation: a = a* 3 can also be written as a*= 3
Evaluation Order
The following table gives the precedence table for Python, from the lowest precedence (least binding) to the highest precedence (most binding).
lambda: Lambda Expression if – else: Conditional expression or: Boolean OR and: Boolean AND not x: Boolean NOT in, not in, is, is not, <, #, >, >=, !=, == Comparisons, including membership tests and identity tests | Bitwise OR ^ Bitwise XOR & Bitwise AND <<, >> Shifts +, – Addition and subtraction *, /, //, % Multiplication, Division, Floor Division and Remainder +x, -x, ~x Positive, Negative, bitwise NOT ** Exponentiation x[index], x[index:index], x(arguments…), x.attribute Subscription, slicing, call, attribute reference (expressions…), [expressions…], {key: value…}, {expressions…} Binding or tuple display, list display, dictionary display, set display |
Example:
length = 5 |
Output:
More examples to practice
bool1 = True #bool type a=5 # 101 (biniary) |
#AND X NAND #OR X NOR #XOR Even or Odd number of True conditions : Even T -> F / Odd T -> T #NOT -> Reverse the output #logical statements |
#Assignment operator |