Operators
An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. Operators are used in programs to manipulate data and variables. They usually form a part of the mathematical or logical expressions.
Python Operators: In this article, I will explain to you the following operators-
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Bitwise Operators
- Identity operators
- Membership operators
Arithmetic Operators: Same like C, C++, Java, C#; Python provides all the basic arithmetic operators, which are used to perform an arithmetic operation. Python. Python has the following Arithmetic Operators:
| Operators | Meaning |
| + | Addition Operators |
| - | Subtraction Operators |
| * |
Multiplication Operators
|
| / |
Division Operators
|
| % |
Modulo
|
| ** |
Exponent
|
| // |
Floor Division
|
Example:
- a=14
- b=4
- print("%d + %d = %d"%(a,b,a+b)) #Addition (+) Operator
- print("%d - %d = %d"%(a,b,a-b)) #Subtraction (-) Operator
- print("%d * %d = %d"%(a,b,a*b)) #Multiplication (*) Operator
- print("%d / %d = %d"%(a,b,a/b)) #Division (/) Operator
- print("%d ** %d = %d"%(a,b,a**b)) #Exponent (**) Operator
- print("%d // %d = %d"%(a,b,a//b)) #Floor Division (//) Operator
From the above example you was thinking that what is actually difference between simple divide and floor divide?
Relational Operators: We often compare two quantities and depending on their relation, take certain decisions. For example, we compare the age of the person for voting, On online shopping sites, we compare the price of two items and so on. These types of comparisons can be done with the help of relational operators. Python supports the following list of relational operators.
| Operators |
Meaning
|
| < |
is less than
|
| <= |
is less than or equal to
|
| > |
is greater than
|
| >= |
is greater than or equal to
|
| == |
is equal to
|
| != |
is not equal to (preferred)
|
| <> |
is not equal to (deprecated)
|
Example:
- a=5
- b=6
- print("%d < %d : %s"%(a,b,(a<b)))#is less than
- print("%d <= %d : %s"%(a,b,(a<=b)))#is less than od equal to
- print("%d > %d : %s"%(a,b,(a>b))) #is greater than
- print("%d >= %d : %s"%(a,b,(a>=b)))#is greater than or equal to
- print("%d == %d : %s"%(a,b,(a==b)))#is equal to
- print("%d != %d : %s"%(a,b,(a!=b)))#is not equal to
- AND (and)
- OR (or)
- NOT (not)
| Expression-1 | Expression-2 | (Expression-1 AND Expression-2) |
| False | False | False |
| False | True | False |
| True | False | False |
| True | True | True |
| Truth Table for AND | ||
| Expression-1 | Expression-2 | (Expression-1 OR Expression-2) |
| False | False | False |
| False | True | True |
| True | False | True |
| True | True | True |
| Truth Table for OR | ||
| Expression | NOT Expression | |
| False | True | |
| True | False | |
| Truth Table for NOT | ||
Example :
- a=5
- b=6
- c=7
- print((a>b) and (a<c))# AND Operation
- print((a>b) or (a<c))# OR Operation
- print(not(a>b))#NOT Operation
Assignment Operators: Assignment operators are used to assigning the result of an expression to a variable. The usual assignment operator is '=' operator. Same like other languages like C, C++, Java, and C#; Python also contains a set of "shorthand assignment operators" of the form:
- v op=exp
is equal to
- v =v op (exp)
Shorthand assignment operators are listed below:
| Operators |
Meaning
|
| += |
Add Shorthand
|
| -= |
Subtract Shorthand
|
| *= |
Multiplication Shorthand
|
| /= |
Division Shorthand
|
| %= |
Modulo Shorthand
|
| **= |
Exponent Shorthand
|
| //= |
Floor Division Shorthand
|
Example:
- a=14 #Assign 5 to a
- b=4 #Assign 6 to b
- a+=b;#Add Shorthand operator (Means a=a+b)
- print(a);
- a-=b;#Subtract Shorthand operator (Means a=a+b)
- print(a);
- a*=b;#Multiplication Shorthand operator (Means a=a+b)
- print(a);
- a/=b;#Division Shorthand operator (Means a=a+b)
- print(a);
- a%=b;#Modulo Shorthand operator (Means a=a+b)
- print(a);
- a**=b;#Exponent Shorthand operator (Means a=a+b)
- print(a);
- a//=b;#Floor Division Shorthand operator (Means a=a//b)
- print(a);
Output :
| Operators |
Meaning
|
| & |
Bitwise AND
|
| || |
Bitwise OR
|
| ^ |
Bitwise XOR(Exclusive OR)
|
| ~ |
One's Complement
|
| << |
Shift Left
|
| >> |
Shift Right
|
Bitwise AND, Bitwise OR, and Bitwise XOR works on truth table which is given below:
| a | b | a & b |
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
| Truth Table for BITWISE AND | ||
| a | b | a | b |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
| Truth Table for BITWISE OR | ||
| a | b | a ^ b |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
| Truth Table for BITWISE XOR | ||
Bitwise operators perform on the bits so Assume we have two variables a=40 and b=14 now you want to calculate a & b, a | b and a^b then, first of all, you have to convert a and b to binary then you have to perform AND, OR and XOR on each and every bits then again convert the result to decimal then only you will get a result. like as following:
One's Compliment (~): The ones' complement of a binary number is defined as the value obtained by inverting all the bits in the binary representation of the number (swapping 0s for 1s and vice versa). The ones' complement of the number then behaves like the negative of the original number in some arithmetic operations.
Left Shift (<<): The left shift (<<) operator shifts the bits of expression 1 left by the number of bits specified. Example: Assume you have a number a=2 and you want to shift the bits left two times (a<<2) then, first of all, you have to convert then shift the bits two times and convert that result as decimal.

Shaili DashoraPosted Jul 28, 2015, 1:18 PM
Nice one..
Gopi ChandPosted Jul 20, 2015, 1:00 PM
Great
Santhakumar MunuswamyPosted Jul 20, 2015, 12:46 PM
Good one
Sibeesh VenuPosted Jul 20, 2015, 9:07 AM
Good one :)
Gowtham RajamanickamPosted Jul 20, 2015, 6:52 AM
good one...
SharadPosted Jul 20, 2015, 6:39 AM
Good one..
Nilesh JadavPosted Jul 20, 2015, 5:16 AM
Nice one sir