The term operator refers to a symbol (or sometimes a phrase of alphabets) which is predefined to perform a certain process such as addition, comparison etc. Each symbol requires one or more objects for the process to be performed. The objects are called operands and symbol itself is called operator. Most of the operators are binary in nature, in the sense they require two operands. Unary operator requires only one operand.

Everybody is familiar with arithmetic operators performing addition, subtraction, multiplication and division. Python has additionally modulus exponent and floor operators.

Operator | Purpose | Description |
---|---|---|

+ | Addition | Adds operands on either side of the operator. |

- | Subtraction | Subtracts right hand operand from operand on left. |

* | Multiplication | returns Multiplication of values on either side of the operator. |

/ | Division | left operand acts as numerator and right operand denominator for division |

% | Modulus | returns remainder of division of left hand operand by right. |

** | Exponent | Calculates value of operand raised to right. a**3 is a raised to 3 i.e. a*a*a |

// | Floor Division | The division of operands where the result is the quotient in which the digits after the decimal point are removed. But division is negative, the result is floored, i.e., rounded away from zero (towards negative infinity). |

In [1]: a,b=21,10#additiona+b Out[1]: 31 In [2]:#subtractiona-b Out[2]: 11 In [3]:#multiplicationa*b Out[3]: 210 In [4]:#divisiona/b Out[4]: 2.1 In [5]:#modulusa%b Out[5]: 1 In [6]:#floor of positive divisiona//b Out[6]: 2 In [7]:#floor of negative division-a//b Out[7]: -3

These operators allow any arithmetic operation and assignment in one step. Result of corresponding arithmetic operation of two operands is assigned back to left operand.

symbol | purpose | Description |
---|---|---|

= | assignment | Assigns values from right side operands to left side operand |

+= | Add AND assign | adds right operand to the left and assign result to left operand |

-= | Subtract AND assign | subtracts right operand from left and assign the result to left operand |

*= | Multiply AND assign | multiplies right operand with left and assign result to left operand |

/= | Divide AND assign | divides left operand with right and assign result to left operand |

%= | Modulus AND assign | assigns modulus of two operands and assigns result to left operand |

**= | Exponent AND assign | Performs exponentiation of left operand by right and assign result to the left operand |

//= | Floor Division and assign | It performs floor division on operators and assign value to the left operand |

In [14]: a,b=21,10#add and assigna+=b a Out[14]: 31 In [15]: a,b=21,10#subtract and assigna-=b a Out[15]: 11 In [16]: a,b=21,10#multiply and assigna*=b a Out[16]: 210 In [17]: a,b=21,10#divide and assigna/=b a Out[17]: 2.1 In [18]: a,b=21,10#modulus and assigna%=b a Out[18]: 1 In [19]: a,b=21,10#exponent and assigna**=b a Out[19]: 16679880978201 In [20]: a,b=21,10#floor and assigna//=b a Out[20]: 2

In order to compare two objects for equality or to decide whether one is greater than other etc. the logical comparison operators are used. Primarily used with numeric objects, they can very well be used with other Python objects such as string, list or tuple.

Symbol | purpose | Description |
---|---|---|

== | equals | returns true if both operands are equal false otherwise |

!= | not equal to | returns true if both operands are not equal false otherwise |

> | greater than | returns true if left operand is greater than right operand, otherwise false |

< | less than | returns true if left operand is less than right operand, otherwise false |

>= | greater than or equal to | returns true if left operand is greater or equal to right operand, otherwise false |

<= | less than or equal to | returns true if left operand is less than or equal to right operand, otherwise false |

In [8]: a=21 b=10# equalsa==b Out[8]: False In [9]:#not equal toa!=b Out[9]: True In [10]:#greater thana>b Out[10]: True In [11]:#less thana<b Out[11]: False In [12]:#greater than or equala>=b Out[12]: True In [13]:#less than or equal toa<=b Out[13]: False

This category of operators is common to all sequence data type i.e. string, list and tuple. All of them use zero based index to access items in them. Hence operators for indexing and slicing have been commonly defined.

symbol | purpose | Description |
---|---|---|

+ | Concatenation | Appends second sequence to first |

* | Repetition | concatenates multiple copies of the same sequence |

[] | Slice | Gives the item at given index |

[ : ] | Range Slice | fetches item in range specified by two index operands separated by : symbol. If first operand is omitted, range starts at zero index If second operand is omitted, range goes upto end of sequence |

in | Membership | Returns true if a item exists in the given sequence |

not in | Membership | Returns true if a item does not exist in the given sequence |

In [21]: s1,s2="Hello","World" l1,l2=[1,2,3],[4,5,6] t1,t2=(1,2,3),(4,5,6) In [22]:#concatenationprint (s1+s2) print (l1+l2) print (t1+t2) HelloWorld [1, 2, 3, 4, 5, 6] (1, 2, 3, 4, 5, 6) In [23]:#repetitionprint (s1*3) print (l1*3) print (t1*3) HelloHelloHello [1, 2, 3, 1, 2, 3, 1, 2, 3] (1, 2, 3, 1, 2, 3, 1, 2, 3) In [24]:#sliceprint(s2[2]) print (l2[2]) print (t2[2]) r 6 6 In [25]: s1='Hello World' l1=[1, 2, 3, 4, 5, 6] t1=(1, 2, 3, 4, 5, 6) In [26]:#range slice of stringprint (s1[2:5]) print (s1[:3]) print (s1[4:]) llo Hel o World In [27]:#range slice on listprint (l1[2:4]) print (l1[:3]) print (l1[3:]) [3, 4] [1, 2, 3] [4, 5, 6] In [28]:#range slice on tupleprint (t1[2:4]) print (t1[:3]) print (t1[3:]) (3, 4) (1, 2, 3) (4, 5, 6) In [29]: s1='Hello World' l1=[1, 2, 3, 4, 5, 6] t1=(1, 2, 3, 4, 5, 6) In [30]: 'He' in s1 Out[30]: True In [31]: 'W' not in s1 Out[31]: False In [32]: 10 in l1 Out[32]: False In [33]: 100 not in l1 Out[33]: True In [34]: 10/2 in t1 Out[34]: True In [35]: 10/3 not in t1 Out[35]: True

These operators are specifically designed for performing operations on set data type as defined in set theory of Mathematics.

purpose | description |
---|---|

Union | Union of two sets is a set of all elements in both. |

Intersection | Intersection of two sets is a set containing elements common to both |

Difference | Difference of two sets results in a set containing elements only in first but not in second set. |

Symmetric difference | Result of Symmetric difference is a set consisting of elements in both sets excluding common element |

In [36]: set1,set2={10,20,30,40,50},{20,40,60} In [37]:#unionset3=set1|set2 set3 Out[37]: {10, 20, 30, 40, 50, 60} In [38]:#intersectionset3=set1&set2 set3 Out[38]: {20, 40} In [39]:#differenceset3=set1-set2 set3 Out[39]: {10, 30, 50} In [40]:#symmetric differenceset3=set1^set2 set3 Out[40]: {10, 30, 50, 60}

These operators are not really symbols but phrases. Each Python object is allocated required space in computer memory. Address of memory location is obtained by built-in id() function. Python variable is really reference name given to this location. More than one variables can refer to same location. Following identity operators tell us if id() values of both objects is same or different.

Operator | Description |
---|---|

is | returns true if both operands point to the same object and false otherwise. |

is not | returns true if both operands don’t point to the same object and false otherwise. |

In [41]: a=10 b=a print (id(a), id(b)) a is b 1860398432 1860398432 Out[41]: True In [42]: a=10 b=a*2 print (id(a), id(b)) a is b 1860398432 1860398752 Out[42]: False

Any object in memory is represented in binary bits. The bitwise representation of object is manipulated by following operators. In following illustration, built-in bin() function is used to verify result of operations. The bin() function returns the binary equivalent of integer object. For example:

a=45 and bin(45) is 00101101

b=25 and bin(25) is 00011001

Following table lists the bitwise operators.

symbol | Operator | Description |
---|---|---|

& | Binary AND | 0&0=0 0&1=0 1&0=0 1&1=1 |

| | Binary OR | 0|0=0 0|1=1 1|0=1 1|1=1 |

^ | Binary XOR | 0^0=0 0^1=1 1^0=1 1^1=0 |

~ | Binary Ones Complement | It is unary operator that flips bits. |

<< | Binary Left Shift | bits moved to left by specified places and adds trailing 0s |

>> | Binary Right Shift | bits moved to right by specified places and adds leading 0s |

In [43]: a,b=45,25 bin(a),bin(b) Out[43]: ('0b101101', '0b11001') In [44]:#binary andc=a&b c, bin(c) Out[44]: (9, '0b1001') In [45]:#binary orc=a|b c, bin(c) Out[45]: (61, '0b111101') In [46]:#binary xorc=a^b c, bin(c) Out[46]: (52, '0b110100') In [47]:#binary complementc=~a c, bin(c) Out[47]: (-46, '-0b101110') In [48]:#left shiftc=a<<2 c, bin(c) Out[48]: (180, '0b10110100') In [49]:#right shiftc=a>>2 c, bin(c) Out[49]: (11, '0b1011')

Just to verify bitwise & operation on a and b by using truth table for & operator

a = 45 = 00101101 b = 25 = 00011001 ----------------------- a&b = 00001001 = 9

9128

C# is an object-oriented programming developed by Microsoft that uses ...

## Leave a Reply

Your email address will not be published. Required fields are marked *