In Python, operators are used to operating mathematical operations on operands. In simple words, operators are symbols and operands are variable & values. Python has various types of operators or operator groups. Each group is used for individual purposes.
Groups are,
These operators are used for mathematical calculations. It only deals with numerical values. Those operations can be addition, subtraction, multiplication, division, etc. Let's say, a=10
and b=30
Operator | Name | Description | Example |
---|---|---|---|
+ | Addition | Add two operands | print(a+b) |
- | Subtraction | Subtract two operands | print(a-b) |
* | Multiplication | Multiply two operands | print(a*b) |
/ | Division | Divide two operands | print(a/b) |
% | Modulus | Divide two operands and look on remainder | print(a%b) |
** | Exponentiation | Perform exponential operations (power) | print(a**b) |
// | Floor Division | Divide two operands and look on quotient and make whole number | print(a//b) |
We use assignment operators for assigning value to a variable. For example: b=5
. Here =
is an assignment operator.
Operator | Example | Equivalent to |
---|---|---|
= | b=5 | b=5 |
+= | b+=5 | b=b+5 |
-= | b-=5 | b=b-5 |
*= | b*=5 | b=b*5 |
/= | b/=5 | b=b/5 |
%= | b%=5 | b=b%5 |
//= | b//=5 | b=b//5 |
**= | b**=5 | b=b**5 |
&= | b&=5 | b=b&5 |
|= | b|=5 | b=b|5 |
^= | b^=5 | b=b^5 |
>>= | b>>=5 | b=b<<5 |
<<= | b<<=5 | b=b<<5 |
Comparison Operators are also known as relational operators. For comparing two values or variables, we use this operator. Based on the condition or statement, it returns either true or false. Let's say, a=10
and b=30
Operator | Name | Description | Example |
---|---|---|---|
== | Equal | If two operands are the same then it will return true | print(a==b) |
!= | Not equal | It will return true if two operands aren't the same | print(a!=b) |
> | Greater than | It will return true if the left variable is greater than the right variable | print(a>b) |
< | Less than | It will return true if the left variable is lesser than the right variable | print(a<b) |
>= | Greater than or equal to | It will return true if the left variable is greater than or equal to the right variable | print(a>=b) |
<= | Less than or equal to | It will return true if the left variable is lesser than or equal to the right variable | print(a<=b) |
Python supports three logical operators. Those are and, or, and not. To combine conditional statements, these logical operators are used. Let say, a=10
Operator | Description | Example |
---|---|---|
and | It returns true if both conditions are true | print(a>5 and a>10) |
or | It returns true if one of the condition is true | print(a<5 or a<10) |
Not | It returns true if the statement is false and vice versa | print(not(a<5 and a<10)) |
Python identity operator is known as the membership operator. These operators are used to compare the objects. It is used to check if two values are positioned on the same memory portion or not. Two variables can be equal. It does not indicate that they are identical. Let say, a=10
Operator | Description | Example |
---|---|---|
Is | If the operand is identical, it returns true | print(a is 10) |
is not | If the operand is not identical, it returns true | print(a is not 15) |
These operators are used to check the sequence object. Let say, a=[5,10,15]
Operator | Description | Example |
---|---|---|
In | If a variable is found in the sequence, it returns true | print(5 in a) |
not in | If a variable is not found in the sequence, it returns true | print(6 not in a) |
These operators are used to examine binary digits.
Operator | Name | Description |
---|---|---|
Operator | Name | Description |
& | AND | It will set each bit to 1 if both contain 1 |
| | OR | It will set each bit to 1 if any 1 contains 1 |
^ | XOR | It will set each bit to 1 if only 1 contains 1 |
~ | NOT | reverses all |
>> | Zero left shift | by shifting zeros, it will move to left |
<< | Signed right shift | shift right by pushing leftmost copies |