What are the operators in python?
Python provides a diverse set of operators that enable the execution of various operations on variables and values. The following are several frequently used operator categories in Python.
The value that the operator operates on is called as operand.
Example
10 + 20
Here + is the operator; 10 and 20 are the operands of adding these two numbers.
Operator categories find in python
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
Arithmetic Operators
x=10
y=20
Operator Category | Meaning | Example |
+ | Add two operands | x + y = 30 |
– | Subtract right operand from the left operand | x – y = -10 |
* | Multiply two operands | x * y = 200 |
/ | Divide left operand by the right operand | x / y = 0.5 |
% | Get the Reminder by division of left operand by the right operand | x % y = 10 |
// | Integer division by division of eft operand by the right operand; but omit the fraction part No round off | x // y = 0 |
** | Exponent by left operand to the power of right | x ** y |
Relational Operators
x=10
y=20
Operator Category | Meaning | Example |
== | Return True if both operands are equal | x == y = False |
!= | Return True if both operands are not equal | x != y = True |
> | Return True if left operand is greater than to the right | x > y = False |
>= | Return True if left operand is greater than or equal to the right | x >= y = False |
< | Return True if left operand is less than to the right | x < y = True |
<= | Return True if left operand is less than or equal to the right | x <= y = True |
Logical Operators
x=10 >=20
y=10==10
Operator Category | Meaning | Example |
and | Return True if both operands are true | x and y = False |
or | Return True if either one of the operands is true | x or y = True |
not | Return True if operand is false (vice versa) | not y = False |
Bitwise Operators
x=5
y=6
Operator Category | Meaning | Example |
& | Bitwise AND (Logical AND operation) | x & y 5 & 6 1012 & 1102 1002 4 |
| | Bitwise OR (Logical OR operation) | x | y 5 | 6 1012 & 1102 1112 7 |
^ | Bitwise XOR (Logical XOR operation) | x ^ y 5 ^ 6 1012 & 1102 0112 3 |
~ | Bitwise NOT -(x+1) | ~x -(x+1) -(5+1) -6 |
Python Operator Precedence
Exercise
print(7/2)
print(7//2)
print(7%2)
print(“A”<=”B”)
print(19 % 3==1)
print(5**2<=10)
print(18 / 2 * 3 + 2)
print(2 + 18 / 2 * 3)
print(19 % 3 + 15 / 2 * 3)
print(18 / 2 % 2 * 2**3)
Answers
print(7/2)
3.5
print(7//2)
3
print(7%2)
1
print(“A”<=”B”)
True
print(19 % 3==1)
True
print(5**2<=10)
False
print(18 / 2 * 3 + 2)
29.0
print(2 + 18 / 2 * 3)
29.0
print(19 % 3 + 15 / 2 * 3)
23.5
print(18 / 2 % 2 * 2**3)
8.0