The Python operator is a symbol that operates on one or more operands. An operand is a variable or value on which the operation is performed.
The Python operator is divided into 7 categories:
- Python arithmetic operator
- Python relational operator
- Python Assignment Operator
- Python Logical Operator
- Python Membership operator
- Python Identity Operator
- Python Bitwise Operator
Other article you might like: Virtual Reality Technology-Future of Education Sector
1) Arithmetic Operators in Python
These Python arithmetic operators include Python operators for basic math operations.
a) addition (+)
Adds values on each side of the operator.
For example >>> 3 + 4
Output
7
b) subtraction (-)
Subtract the value on the right from the value on the left.
For example >>> 3-4
Output
-1
c) multiplication (*)
Multiplies the values on each side of the operator.
For example >>> 3 * 4
Output
12
d) division (/)
Divide the left value by the right value. Note that division results in a floating point value.
For example >>> 3/4
Output
0.75
e) exponentiation (**)
Increases the first number to the power of the second.
For example >>> 3 ** 4
Output
81
f) Floor division (//)
Divides and returns the integer value of the quotient number. It empties the decimal places.
For example >>> 10 // 3
Output
3
g) modulus (%)
Divides and returns the remainder value.
For example >>> 3% 4
Output
3
2) Python relational operator
The Python relational operator performs the comparison between operands.
They tell us whether one operand is greater than the other, less, equal, or a combination of the two.
a) Less than (<)
This operator checks if the value to the left of the operator is less than the value to the right.
For example >>> 3 <4
Output
True
b) greater than (>)
It checks whether the left value of the operator is greater than the right.
For example >>> 3> 4
Output
False
c) Less than or equal to (<=)
It checks whether the value on the left side of the operator is less than or equal to the value on the right side.
For example >>> 7 <= 7
Output
True
d) Greater than or equal to (> =)
It checks if the value on the left of the operator is greater than or equal to the one on the right of the operator.
For example >>> 0> = 0
Output
True
e) equal (= =)
This operator checks whether the value to the left of the operator is equal to the value to the right.
1 corresponds to the Boolean value True, but 2 does not. Also, 0 equals False.
For example >>> 3 == 3.0
Output
True
f) Not Equal to (!=)
It checks whether the left value of the operator is not equal to the right.
The Python <> operator does the same job but has been discontinued in Python 3.
If the condition of a relative operator is met, True is returned. Otherwise, it will return False. You can use this return value in another statement or expression.
For example >>> 1! = 1.0
Output
False
3) Python Assignment Operator
The Python assignment operator assigns a value to a variable. It can manipulate the value by a factor before assignment.
We have 8 assignment operators – one simple and seven for Python’s 7 arithmetic operators.
a) Assign (=)
Assigns a value to the expression on the left of the equal to sign. Note that == is used for comparison, but = is used for assignment.
>>> a = 7
>>> print (a)
Output
7
b) Add and assign (+ =)
Adds the values on each side and assigns them to the expression on the left. a + = 10 corresponds to a = a + 10.
The same applies to all subsequent assignment operators.
>>> a + = 2
>>> print (a)
Output
9
c) Subtract and assign (- =)
Subtracts the right value from the left value and then assigns it to the expression on the left.
>>> a- = 2
>>> print (a)
Output
7
d) Divide and assign (/ =)
Divide the left value by the right value and then assigns it to the expression on the left.
>>> a / = 7
>>> print (a)
Output
1.0
e) Multiply and assign (* =)
Multiply the values on each side and then assigns it to the expression on the left.
>>> a * = 8
>>> print (a)
Output
8.0
f) Modulus and assign (% =)
Run the module with the values on each side and then assigns it to the expression on the left.
>>>% = 3
>>> print (a)
Output
2.0
g) Exponent and assign (** =)
Makes the values on each side exponentiated. Then assign it to the expression on the left.
>>> a ** = 5
>>> print (a)
Output
32.0
h) Floor divide and assign (// =)
Make a floor division on the values on each side. Then assign it to the expression on the left.
>>> a // = 3
>>> print (a)
Output
10.0
4) Logical Python Operator
These are conjunctions you can use to combine multiple conditions.
We have three logical Python operators – and, or, and not, which fall under the Python operators.
To learn and master Python Programming skills visit Python Classes in Pune
a) and operator
If the conditions on both sides of the operator are true, the expression becomes true.
b) or operator
The expression is only false if both statements about the operator are false. Otherwise, it’s true.
c) not operator
This reverses the Boolean value of an expression. It turns True into False and False into True.
So as you can see below, the Boolean value for 0 is False. So don’t revert to true.
>>> a = no (0)
>>> print (a)
Output
True
5) Python Membership operator
These operators test whether a value is a member of a string. The sequence can be a list, a string, or a tuple.
We have two Python binding operators – “in” and “not in”.
a) in operator
This checks whether a value is a member of a string.
In our example we can see that the string ‘fox’ does not belong to the pet list. But the string ‘cat’ is one of them, so it returns True.
Also, the string “me” is a substring of the string “deception”. Therefore, it will return true.
>>> Animals = [“dog”, “cat”, “ferret”]
>>> “Fox” with pets
Output
False
b) not in operator in
In contrast to “in”, “not in” checks that a value is not a member of a string.
>>> ‘pot’ not in ‘disappointment’
Output
True
6) Python Identity Operator
We come to the Python operator identity.
These operators test whether the two operands have a common identity. Now we have two identity operators: “is” and “is not”.
a) is operator
If two operands have the same identity, True will be returned. Otherwise it will return False. Here, 2 does not equal 20, so False is returned.
Also, “2” and “2” are the same. The difference between quotes does not make them different. Therefore, it returns True.
>>> 2 is 20
Output
False
>>> “2” is “2”
Output
True
b) is not operator
2 is a number and ‘2’ states it is a string. So it returns a true.
>>> 2 is not ‘2’
Output
True
7) Python Bitwise operator
Now, let’s take a look at the Bitwise Python operator.
Bitwise Operators in Python
They work as you go through the operands.
a) Binary AND (&) operator in Python
Performs an AND operation of the two values bit-by-bit. Here the binary for 2 is 10 and for 3 it is 11. & – You give 10, which is binary for 2.
Likewise, & -ing 011 (3) and 100 (4) result in 000 (0).
>>> 2 and 3
Output
2
>>> 3 and 4
Output
0
b) Binary OR Operator (|) in Python
It performs a bitwise OR on both values. Here, the OR combination of 10 (2) and 11 (3) results in 11 (3).
>>> 2 | 3
Output
3
c) Binary XOR (^) operator in Python
It performs bit to bit XOR (exclusive OR) on both values. Here, the XOR operation of 10 (2) and 11 (3) results in 01 (1).
>>> 2^3
Output
1
d) Binary One’s Complement(~) in Python
It returns the one’s complement of the binary number of a number. He turns the bits. The binary number for 2 is 0000010. The one’s complement is 11111101.
It’s binary for -3. This equates to -3. Likewise, ~1 -2 results.
>>> ~ -3
Output
2
Again, your complement to -3 is 2.
e) Binary left shift operator (<<) in Python
It shifts the value of the left operand by the number of digits specified by the right operand.
Here is the binary number of 2 10. 2 << 2 it shifts it two places to the left. That makes 1000, which is binary for 8.
>>> 2 << 2
Output
8
f) Binary Right Shift (>>) in Python
It shifts/moves the value of the left operand to the right by the number of places specified by the right operand.