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))
- print("%d - %d = %d"%(a,b,a-b))
- print("%d * %d = %d"%(a,b,a*b))
- print("%d / %d = %d"%(a,b,a/b))
- print("%d ** %d = %d"%(a,b,a**b))
- print("%d // %d = %d"%(a,b,a//b))
Output:
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)
|
The relational operator always give result in Boolean means result will be either true or false. If the expression of the relational operator will be true then the result will be true and if the expression of the relational operator will be fast then the result will be false.
Example:
- a=5
- b=6
- print("%d < %d : %s"%(a,b,(a<b)))
- print("%d <= %d : %s"%(a,b,(a<=b)))
- print("%d > %d : %s"%(a,b,(a>b)))
- print("%d >= %d : %s"%(a,b,(a>=b)))
- print("%d == %d : %s"%(a,b,(a==b)))
- print("%d != %d : %s"%(a,b,(a!=b)))
Output:
Logical Operator: The logical operators are used when we want to test more than one condition and make decisions. Python's logical operators are listed below:
- AND (and)
- OR (or)
- NOT (not)
As I said when we want to compare more than one expression then we use Logical operator but logical operators work on the truth table which is given below:
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))
- print((a>b) or (a<c))
- print(not(a>b))
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:
Where
v: is a Variable
exp: is an expression
op= : Assignment Operator. The assignment statement
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
- b=4
-
- a+=b;
- print(a);
-
- a-=b;
- print(a);
-
- a*=b;
- print(a);
-
- a/=b;
- print(a);
-
- a%=b;
- print(a);
-
- a**=b;
- print(a);
-
- a//=b;
- print(a);
Bitwise Operators: For manipulating data at the bit level Python has bitwise operators. These operators are used to testing the bits or shifting the bits. Python's Bitwise Operators are listed as following:
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.