In this tutorial, we will see all the python operators.
What is the operator?
Operators are the symbols that trigger the action on an object or variable. The object and variables in which the operation is performed are called operands.
There are many operators in python programming. It is something like this.
Arithmetic Operator
- Assignment Operator
- Comparison Operator
- Identity Operator
- Logical Operator
- Membership Operator
- Bitwise Operator
We will see all the operators here :
Arithmetic Operator
Arithmetic operators are performed in numerical values to perform normal mathematical operations.
Operator | Name | Description | Example |
---|---|---|---|
+ | Addition | Addition of two operands | a+b |
- | Subtraction | Subtraction of two operands | a-b |
* | Multiplication | Multiplication of two operands | a*b |
/ | Division | Division of one operand by another one. Key Point: Output is always float. | a/b |
% | Modulus | When division of one operand by another operand, %(modules) return remainder. | a%b |
** | Exponentiation | Return Power | a**b |
// | Floor Division | Division of one operand by another one same as division ( / ) operator. Key point : Output depends on input, if input is int type, output will be int type, if input is float type, output will be float type. | a//b |
Examples: Arithmetic Operators in Python
#Example of Arithmetic operator a = 20 b = 10 print(a+b) #addition print(a-b) #subtration print(a*b) #Multiplication print(a/b) #Division, Always return Float value print(a**b) #Exponentiation, Power print(a%b) #Modules, for remainder print(a//b) #Floor Division
30 10 200 2.0 10240000000000 0 2
Assignment Operator
Assignment operators are used to assign a value to the variable name.
Operator | Description | Example |
---|---|---|
= | It assign the value to the variable. | a = 20 |
+= | Adding the value given in the right operand to the left operand. This will modify the value of the left operand. | a+=10 |
-= | Reduce the value given in the right operand to the left operand. This will modify the value of the left operand. | a-=10 |
*= | Multiplication the value given in the right operand to the left operand. This will modify the value of the left operand. | a*=2 |
/= | Division the value given in the right operand to the left operand. This will modify the value of the left operand. | a/=2 |
//= | Almost same as /= but the key point is output depends on operand , if operand is int type then output will be int type. | a//=2 |
**= | Same as previous operator but this operator performs Exponentiation (power). | a**=2 |
%= | The remainder obtained by dividing the value of the right operand by the value of the left operand. This will modify the value of the left operand. | a%=2 |
Examples: Assignment Operators in Python
#Example of Assignment operator # = , += , -= , *= , /= a = 4 #assign 4 to variable a. a += 2 #Now the value of a is 6 a -= 3 #Now the value of a is 3 a *= 2 #Now the value of a is 6 a /=3 #Now the value of a is 2.0 , / operator returns float print(a)
2.0
#Example of Assignment operator # //= , **= , %= b = 10 b //= 2 """ Now the value of b is 5. If the input is int then output is int in the case of floor division. """ b **= 3 #Now the value of b is 125. b %= 4 #Now the value of b is 1. print(b)
Output
1
Comparison Operator
A comparison operator is used to compare two values. It gives output in the form of True and False.
Examples: Comparison Operator in Python
#Example of Comparison operator a = 10 b = 10 c = 12 print(a == b) print(a != b) print(c > a) print(c < a) print(a >= b) print(a <= b)
Output
True False True False True True
Identity Operator
The most important point is, if two values are exactly the same, then they are saved in the same memory location.
The identity operator compares the memory location of the value and checks the value is the same or not.
is and is not are the two identity operators in python.
Operator | Description | Example |
---|---|---|
is | If the memory location of the objects are same then the output will true otherwise false. | a = 10 b = 10 print( a is b ) |
is not | If the memory location of the objects are not same then the output will true otherwise false. | a = 10 b = 10 print( a is not b ) |
Examples: Identity Operators in Python
#Example of identity operator a = 10 b = 10 c = "10" #string print(a is b) print(a is not b) print(a is c) print(a is not c)
Output
True False False True
Logical Operator
The logical operator is one of the most important operators in python. A logical operator is used when we have to combine two or more than two conditions.
Types of Logical Operator
There are three logical operators in python.
- and
- or
- not
And Logical Operator
In the case of and logical operator, if both the conditions are true then the output is True. if both the conditions are false the output will also be false. One of the main things is if only one condition is false the output will be false.
Here is the and operator table. Here are all the possible conditions.
Condition 1 | Condition 2 | Output |
---|---|---|
True | True | True |
True | False | False |
False | True | False |
False | False | False |
Examples: And Logical Operator in Python
#Example of and logical operator a = 10 print(a > 5 and a < 15) #Both conditions are true then the output is true. print(a < 5 and a > 15) #Both conditions are false then the output is false. print(a > 9 and a < 8) #One condition is false then the output is false.
Output
True False False
Or Logical Operator
In the case of or logical operator, if both the conditions are true then the output is True. if both the conditions are false the output will also be false. One of the main things is if only one condition is true the output will be true.
Here is the or operator table. Here are all the possible conditions.
Condition 1 | Condition 2 | Output |
---|---|---|
True | True | True |
True | False | True |
False | True | True |
False | False | False |
Examples: Or Logical Operators in Python
#Example of or logical operator a = 10 print(a > 5 or a < 15) #Both conditions are true then the output is true. print(a < 5 or a > 15) #Both conditions are false then the output is false. print(a > 9 or a < 8) #One condition is true then the output is true.
Output
True False True
Not Logical Operator
In the case of this operator, if the result is coming true and applying this operator it becomes false. This means it converts True to False and False to True.
Here is the not-operator table.
Input | Result |
---|---|
True | False |
False | True |
Examples: Not Logical Operator in Python
#Example of not logical operator a = True print(not(a)) b = False print(not(b)) c = 10 print(not(a > 5 and a < 15)) '''Both conditions are true then the Output will be true but here output is false because not operator is engaged.'''
Output
False True True
Membership Operator
The membership Operator is used to check particular element is present or not in the specified sequence such as string, list, tuples, and dictionary, etc.
in and not in are the two membership operators in python.
Operator | Description | Example |
---|---|---|
in | If the element to be found is in the specified sequence then it will return true. If that element is not in sequence then it will return false. | a = "Membership" print("r" in a) |
not in | This is the just opposite of in operator. If the element to be not found is in the specified sequence then it will return true. If that element is in sequence then it will return false. | a = "Membership" print("r" not in a) |
Example: Membership Operator in Python
#Example of Membership operator # in, not in - Membership Operator a = [10,20,30,40,50] #list print(50 in a) print(40 in a) print(60 not in a) print(70 not in a) b = "python" #string print("p" in b) print("h" not in b)
Output
True True True True True False
Bitwise Operator
Bitwise operators are used to perform operations in binary numbers.
Bitwise operators work in bits ( 0 and 1). If bitwise operations are performed in an integer number then it is converted to binary and then perform operations.
Operator | Description | Example |
---|---|---|
& | Bitwise AND | a & b |
| | Bitwise OR | a | b |
^ | Bitwise XOR | a ^ b |
~ | Bitwise inversion(one's Complement) | ~a |
<< | Bitwise Left Shift | a << 2 |
>> | Bitwise Right Shift | a >> 2 |
Bitwise And
In the case of AND bitwise operator, it will return 1 if both operands are 1. If the value of any of the operands is 0 then the result will be O. The symbol of bitwise and operator is &.
Here is a table of AND Bitwise Operator.
Operand 1 | Operand 2 | Result |
---|---|---|
1 | 1 | 1 |
1 | 0 | 0 |
0 | 1 | 0 |
0 | 0 | 0 |
Examples: Bitwise AND Operator in Python
#Example of Bitwise and operator a = 10 # 1010 --> Binary Value b = 15 # 1111 --> Binary Value c = a & b print(c)
Output
10
Bitwise OR
In the case of OR bitwise operator, If the value of only one operand is 1, then the result will be 1. If both operand’s values are 0, then the value will be 0. The symbol of bitwise and operator is |.
Here is a table of OR Bitwise Operators.
Operand 1 | Operand 2 | Result |
---|---|---|
1 | 1 | 1 |
1 | 0 | 1 |
0 | 1 | 1 |
0 | 0 | 0 |
Examples: Bitwise OR Operators in Python
#Example of OR Bitwise Operator a = 10 #1010 --> Binary Value b = 15 #1111 --> Binary Value c = a | b print(c)
Output
15
Bitwise XOR
In the case of XOR bitwise operator, If both operands are the same then the result will be 0. If both the operands are different then the result will be 1. It means if one operand is 1 and the other is 0 then the result will be 1. The symbol of the XOR operator is ^.
Here is the table of the XOR bitwise operator.
Operand 1 | Operand 2 | Result |
---|---|---|
1 | 1 | 0 |
1 | 0 | 1 |
0 | 1 | 1 |
0 | 0 | 0 |
Examples: Bitwise XOR Operators in Python
#Example of OR Bitwise Operator a = 10 #1010 --> Binary Value b = 15 #1111 --> Binary Value c = a ^ b print(c)
Output
5
Bitwise NOT
In the Bitwise NOT operator, reverse the beat, if it means 1 then it will turn 0 and if it’s 0, we’ll make it 1. The symbol of the bitwise not operator is ~.
Bitwise NOT(~) is also a unary operator. Unary operators are those which require only one operand to work.
Operand | Result | Example |
---|---|---|
1 | 0 | 1101 --> 0010 (Binary Number) |
0 | 1 | 1010 --> 0101 (Binary Number) |
Shift Operator: Left Shift and Right Shift
Shifts the bits in the shift operator. Bits can be shifted left and right.
Left Shift (<<)
In Left Shift, As many numbers are given, we will shift the same bit to the left and will fill 0 in whatever place is left by this operation.
Examples: Bitwise Left Shift and Right Shift Operator in Python
#Example of Left Shift and Right Shift a = 10 a << 2 # 2 step left shift print(a) a = 10 a >> 2 # 2 step right shift print(a)
Example: Bitwise operator in python
#Example of Bitwise operator a = 10 # 1010 --> Binary Value b = 15 # 1111 --> Binary Value c = a & b #Bitwise AND (&) operator d = a | b #Bitwise OR (|) operator e = a ^ b #Bitwise XOR (^) operator print(c) print(d) print(e) print(~a) #Bitwise NOT operator #Bitwise Shift Operator a << 2 # 2 step left shift print(a) a >> 2 # 2 step right shift print(a)
Output
10 15 5 -11 10 10
Check Your Knowledge
Python Operator Quiz
For more educational posts like this, Please rate this post and keep visiting on cyberfauz.com.