Python - Things to know before proceed - Operator Tutorial
An operator is used to perform operations between two operands. Operands can be variables or values and an operator can be +, -, *, / etc.
There are 7 types of an operator in Python:-
- Arithmetic Operator
- Comparison Operator
- Logical Operator
- Identity Operator
- Membership Operator
- Bitwise Operator
- Assignment Operator
1] Arithmetic Operator
It is used to perform basic mathematics operation i.e addition (+), subtraction (-), division (/), multiplication (*), modulus (%) , exponentiation (**) and floor division (//) between two operands.
Operator |
Description |
+ Addition |
This operator will do the addition of two operands. print(x + y) |
- Subtraction |
This operator will do subtraction between two operands. print(x - y) |
/ Division |
This operator will divide two operands. print(x / y) |
* Multiplication |
This operator will do the multiplication of two operands. print(x * y) |
% Modulus |
This operator will give a remainder on dividing of two operands. print(x % y) |
** Exponentiation |
This operator will find the exponentiation (i.e x power to the y) value. print(x ** y) |
// Floor Division |
This operator will find the floor division value. Suppose on the division you get a value which is between 3 and 2 i.e 2.5. But the floor division will only take the floor whole number value. i.e 2. print(x // y) |
Program:-
x = 7
y = 3
print(x + y) #Addition
print(x - y) #substraction
print(x * y) #Multiplication
print(x / y) #Division
print(x % y) #Modulus
print(x ** y) #Exponentiation
print(x // y) #Floor Divsion
Output:-
10
4
21
2.3333333333333335
1
343
2
2] Comparision Operator
It is used to compare the value of two operands and return the result in the form of the booleans - either True or False.
Operator |
Description |
< Less than |
If the first operand is less than the second operator. Then it will return true, or else it will return false. Example:- print(3 < 5) Output:- True |
> Greater Than |
If the first operand is greater than the second operator. Then it will return true, or else it will return false. Example:- print(5 > 3) Output:- True |
<= Less Than Or Equal To |
If the first operand is less than or equal to the second operator. Then it will return true, or else it will return false. Example:- print(3 <= 3) Output:- True |
>= Greater Than Or Equal To |
If the first operand is greater than or equal to the second operator. Then it will return true, or else it will return false. Example:- print(5 >= 5) Output:- True |
== Equal To |
If the first operand is equal to the second operator. Then it will return true, or else it will return false. It is also known as the equality operator. Example:- print(3 == 3) Output:- True |
!= Not Equal To |
If the first operand is not equal to the second operator. Then it will return true, or else it will return false. It is also known as the equality operator Example:- print(5 != 3) Output:- True |
Program:-
x = 7
y = 3
print(x == y) #Equal
print(x != y) #Not Equal
print(x > y) #Greater Than
print(x < y) #Less Than
print(x >= y) #Greater Than Equal To
print(x <= y) #Less Than Equal To
Output:-
False
True
True
False
True
False
3] Logical Operator
A logical operator is used to combine the conditional statements and make decisions based on them.
Operator |
Description |
and |
In logical and Operator, If anyone's condition is false the output will be false. Logical and Operator doesn’t check the second condition if the first condition is false Logical and Operator only check the second condition, if the first condition is true. Explanation-
|
or |
In logical or Operator, If anyone's condition is true the output will be true. Logical or Operator doesn’t check the second condition if the first condition is true Logical or Operator only checks the second condition, if the first condition is false. Explanation-
|
And Operator Program-
x=15;
y=25;
z=35;
print(x<y and x<z) #true and true =true
print(x<y and x>z) #true and false =false
print(x>y and x<z) #false and true =false
print(x>y and x>z) #false and false =false
Output:-
True
False
False
False
Or Operator Program-
x=15;
y=25;
z=35;
print(x<y or x<z) #true or true =true
print(x<y or x>z) #true or false =true
print(x>y or x<z) #false or true =true
print(x>y or x>z) #false or false =false
Output:-
True
True
True
False
4] Identity Operator
It is used to compare the object of two different variables. id() is used here to get the identity of an object.
In identity operator, we will use the concept of object reusability.
Int:- - 5 to 256 is applicable for object reusability, above it will not be applicable.
String:- A-Z, a-z, 0-9, _ is applicable.
Bool:- 0 and 1 are applicable
None is applicable, float is not applicable.
Example:-
a = 'Sam'
b = 'Sam'
print(a is b) #True - reason is A-Z, a-z, 0-9, _ is applicable in String
a = 'Sam#'
b = 'Sam#'
print(a is b) #False - reason is A-Z, a-z, 0-9, _ is applicable, but # is not applicable in String
Output:-
True
False
Operator |
Description |
is |
It will return true if both operand are the same object. otherwise it will return false. Example:- x = ["fresherbell", "tutorial"] y = ["fresherbell", "tutorial"] z=x; print(x is z) #return true because x is the same object as z print(x is y) #return false because x is not the same object as y |
is not |
It will return true if both operands are not the same object. otherwise, it will return false. Example- x = ["fresherbell", "tutorial"] y = ["fresherbell", "tutorial"] z=x; print(x is not z) #return false because x is the same object as z print(x is not y) #return false because x is not the same object as y |
Program:-
x = ["fresherbell", "tutorial"]
y = ["fresherbell", "tutorial"]
z=x;
print(x is z) #return true because x is the same object as z
print(x is y) #return false because x is not the same object as y
print(x is not z) #return false because x is the same object as z
print(x is not y) #return true because x is not the same object as y
print(id(x))
print(id(y))
print(id(z))
a = 'Sam'
b = 'Sam'
print(a is b) #True - reason is A-Z, a-z, 0-9, _ is applicable in String
a = 'Sam#'
b = 'Sam#'
print(a is b) #False - reason is A-Z, a-z, 0-9, _ is applicable, but # is not applicable in String
Output:-
True
False
False
True
140160088391048
140160088391176
140160088391048
True
False
5] Membership Operator
It is used to test whether the sequence/value is present in the object or not.
Operator |
Description |
in |
It will return true if the value is present in the object, otherwise it will return false. Example:- x = ["fresherbell", "tutorial"] y = "fresherbell" print(y in x) |
in not |
It will return true because a fresherbell is present in the object x. Example:- x = ["fresherbell", "tutorial"] y = "fresherbell" print(y not in x) It will return false because fresherbell is present in the object |
Program:-
x = ["fresherbell", "tutorial"]
y = "fresherbell"
print(y in x)
print(y not in x)
Output:-
True
False
6] Bitwise Operator
Bitwise Operator will do the operation between binary values.
Operator |
Description |
& (And) |
In this, bit will set to 0, if any one of two bit is 0, Otherwise it will set bit to 1.
Example:- 0101 & 1001 = 0001 = 1 |
| (Or) |
In this, bit will set to 1, if any one of two bit is 1, Otherwise it will set bit to 0.
Example:- 0101 | 1001 = 1101 = 13 |
^ (Xor) |
In this, If the two-bit is the same, then it will set the bit to 0, otherwise, it will set a bit to 1.
Example:- 0101 ^ 1001 = 1100 = 12 |
~ (Not) |
It will return one compliment of the number. you can simply find the bitwise not of any value by simply adding a 1 and reversing the sign of the integer. a = 5 ~ a = - (5 + 1) = -6 b = -101 ~ b = - (-101 + 1) = 100 Explanation:- For 1 it will invert and set a bit to 0. For 0 it will invert and set bit to 1.
Example 1:-
Hence final result is 10 – 16 = -6 Example 2:-
Hence final result is 154 – 256 = 102 |
<< (Left Shift) |
In this, all the bits are shifted to the left, by pushing 0 from the left. Depending upon how many places the bit should be shifted. Example:- x = 5 x << 1 – This will left shift the bit of x by 1 place 5 = 0101 = 1010 (on shifting the bit to the left by 1 place and pushing 0 from left) Hence final result is 1010 = 10
x << 3 – This will left shift the bit of x by 3 place 5 = 0101 = 0010 1000 (on shifting the bit to the left by 3 place and pushing 0 from left) Hence final result is 0010 1000 = 40 Another method:- x<<1 (Shifting the operator by 1) x * ( 2 ** 1) = 5 * 2 = 10
x<<3 (Shifting the operator by 3) x * ( 2 ** 3) = 5 * 8 = 40
|
>> (Right Shift) |
In this, all the bits are shifted to the right, by pushing 0 from the right. Depending upon how many places the bit should be shifted. Example:- x = 5 x >> 1 – This will right shift the bit of x by 1 place 5 = 0101 = 0010 (on shifting the bit to the right by 1 place and pushing 0 from right) Hence final result is 0010 = 2
x >> 3 – This will right shift the bit of x by 3 place 5 = 0101 = 0000 (on shifting the bit to the right by 3 place and pushing 0 from right) Hence final result is 0000 = 0 Another method:- x>>1 (Shifting the operator by 1) x // ( 2 ** 1) = 5 // 2 = 2
x>>3 (Shifting the operator by 3) x // ( 2 ** 3) = 5 // 0 = 0 |
Program:-
x = 5 #0000 0101
y = 9 #0000 1001
print(x & y) # 0000 0101 & 0000 1001 = 0000 0001 = 1
print(x | y) # 0000 0101 | 0000 1001 = 0000 1101 = 13
print(x ^ y) # 0000 0101 ^ 0000 1001 = 0000 1100 = 12
print(~x) # ~ 0000 0101 = 1111 1010 = 250, Therefore 250-256 = -6
print(x << 1) # 0000 0101 (On Left Shifting by 1)= 0000 1010 = 10
print(x << 3) # 0000 0101 (On Left Shifting by 3)= 0010 1000 = 40
print(x >> 1) # 0000 0101 (On Right Shifting by 1)= 0000 0010 = 2
print(x >> 3) # 0000 0101 (On Right Shifting by 3)= 0000 0000 = 0
Output:-
1
13
12
-6
10
40
2
0
7] Assignment Operator
The assignment operator is used to assign a value to a variable.
Operator |
Explanation |
= |
It will assign value 1 to a variable x |
+= |
It will increment the value of x by 1, later it will assign it to the same. Also, known as a component operator. x += 1 is same as x = x + 1 |
-= |
It will decrement the value of x by 1, later it will assign to the same.
x -= 1 is same as x = x - 1 |
*= |
It will multiply the value of x by 2, later it will assign it to the same. x *= 2 is same as x = x * 1 |
/= |
It will divide the value of x by 2, later it will assign it to the same. x /= 2 is same as x = x / 1 |
%= |
It will mod the value of x with 2, later its remainder will assign to the same x %= 2 is same as x = x % 1 |
//= |
It will do floor division of the value of x with 2, later its result will assign to the same.
x //= 2 is same as x = x // 1 |
**= |
It will do the value of x to the power of 3, later its result will assign to the same.
x **= 3 is same as x = x ** 1 |
&= |
It will do bitwise & - And operation in between the value of x and 3. It will assign the result 0 or 1 to the same.
x &= 3 is same as x = x & 1 |
|= |
It will do bitwise | - Or operation in between the value of x and 3. It will assign the result 0 or 1 to the same.
x |= 3 is same as x = x | 1 |
^= |
It will do a bitwise ^ - Xor operation in between the value of x and 3. It will assign the result 0 or 1 to the same. x ^= 3 is same as x = x ^ 1 |
>>= |
It will do bitwise >> - Right Shift operation in between the value of x and 3. It will assign the result 0 or 1 to the same.
x >>= 5 is same as x = x >> 5 |
<<= |
It will do bitwise << - Left Shift operation in between the value of x and 3.It will assign the result 0 or 1 to the same.
x <<= 5 is same as x = x << 5 |
Program:-
x = 5 #It will assign 5 to variable x
print(x) #5
x+=1 #It will increment the value of x by 1
print(x) #6
x-=1 #It will decrement the value of x by 1
print(x) #5
x*=2 #It will multiply the value of x by 2
print(x) #10
x/=2 #It will divide the value of x by 2
print(x) #5.0 - It will give float value
x = 5 #reassign value to x
x%=2 #It will mod the value of x by 2
print(x) #remainder = 1
x = 5 #reassign value to x
x//=2 #It will do floor division the value of x by 2
print(x) #2 - It will not give the float value
x**=3 #It will do the value of x to the power of 3
print(x) #8 = 2 * 2 * 2 . Here x=2 from the previous iteration
x&=3 #It will do bitwise & - And operation in between the value of x and 3
print(x) #8 & 3 = 1000 & 0011 = 0000 . Hence Result is 0
x = 8 #reassign value to x
x|=3 #It will do bitwise | - Or operation in between the value of x and 3
print(x) #8 | 3 = 1000 | 0011 = 1011 . Hence Result is 11
x = 5 #reassign value to x
x^=3 #It will do bitwise ^ - Xor operation in between the value of x and 3
print(x) #5 ^ 3 = 0101 ^ 0011 = 0110 . Hence Result is 6
x = 5 #reassign value to x
x>>=1 #In this, all the bit are shifted to right, by pushing 0 from right.
print(x) #5 = 0101 = 0010(shifted to right by 1) = 2
x = 5 #reassign value to x
x<<=1 #In this, all the bits are shifted to the left, by pushing 0 from left.
print(x) #5 = 0101 = 1010(shifted to left by 1) = 10
Output:-
5
6
5
10
5.0
1
2
8
0
11
6
2
10