- Operators are special symbols in python that carry out Arithmetic or Logical computation.
- The value that the operator operates on is called Operands.
TYPES OF OPERATORS:
- 01. Arithmetic Operators
- 02. Comparisons Operators
- 03. Logical Operators
- 04. Bitwise Operators
- 05. Assignment Operators
- 06. Membership Operators
1. ARITHMETIC OPERATORS:
- Performs the arithmetic operations for a given input.
OPERATOR | FUNCTION | SYNTAX |
---|---|---|
+ | Adds the value assigned to variables x and y respectively. | x+y |
– | Subtracts the value assigned to variables x and y respectively. | x-y |
* | Multiplies the values assigned to variables x and y respectively. | x*y |
/ | Divides the values assigned to variables x and y respectively. | x/y |
// | It is a Floor operator. It gives the value near to and less than the output value. | x//y |
% | It gives the value of the remainder when x divides y. | x%y |
** | Exponent operator. Gives the value of x exponent y. | x**y |
Example
Addition Operation
x=10 y=20 print("Addition of x and y is: ", x+y)
Output:
30
Example
Subtraction Operation
x=100 y=20 print("Addition of x and y is: ", x-y)
Output:
80
Example
Multiplication Operation.
x=10 y=20 print("Addition of x and y is: ", x*y)
Output:
200
Example
Division Operation.
x=100 y=20 print("Addition of x and y is: ", x/y)
Output:
5
Example
Floor Operations.
x=111 y=20 print("Addition of x and y is: ", x//y)
Output:
5
Example
Remainder Operations.
x=108 y=20 print("Addition of x and y is: ", x%y)
Output:
8
Example
Exponent Operations.
x=10 y=3 print("Addition of x and y is: ", x**y)
Output:
1000
2. Comparisons Operators:
- These operators are also known as Relational Operators.
- It either returns True or False according to the condition.
OPERATOR | FUNCTION | SYNTAX |
---|---|---|
> | Checks for the greater value among the given two values. | x>y |
< | Checks for the lower value among the given two values. | x<y |
>= | Checks for the greater than or equal to value among the given two values. | x>=y |
<= | Checks for the less than or equal to value among the given two values. | x<=y |
== | Checks whether the given two values are equal. | x==y |
!= | Checks whether the given two values are not equal. | x!=y |
Example
Greater than operator(>)
x=10 y=20 print(x > y)
Output:
False
Example
Less than operator(<)
x=10 y=20 print(x < y)
Output:
True
Example
Greater than equal to the operator(>=)
x=10 y=20 print(x >= y)
Output:
False
Example
Less than Equal To operator(<=)
x=10 y=20 print(x <= y)
Output:
True
Example
Equal To operator(==)
x=10 y=20 print(x = y)
Output:
False
Example
Not Equal To operator(!=)
x=10 y=20 print(x != y)
Output:
True
3. Logical Operators:
- Use to perform the Logical operations.
- Logical operators are used to combining conditional statements.
OPERATOR | FUNCTION | SYNTAX |
---|---|---|
and | When both the values are True, “and” operator returns True or else it returns False. | x and y |
or | When both the values are False, “or” operator returns False or else it returns True. | x or y |
not | Returns the negated value of the given input. | x not y |
Example
AND Operator.
x = True y = False print(x and y)
Output:
False
Example
OR Operator.
x = True y = False print(x or y)
Output:
True
Example
NOT Operator.
x = True print(not x)
Output:
False
4. Bitwise Operator:
- It acts on operands as if they were strings of binary digits.
- It operates bit by bit.
OPERATOR | FUNCTION | SYNTAX |
---|---|---|
& | Performs the bitwise and operation between x and y. It returns 1 if both bits are 1, else 0. | x & y |
| | Performs the bitwise or operation between x and y.It returns 0 if both bits are 0,else 1. | x or y |
~ | Performs the negation operation. it inverts the given bit. | ~x |
^ | Performs the XOR operation. It returns 1 if at least one of the bits is 1, else 0. | x^y |
>> | Performs the right shift(of 1 bit) operation by inserting the zeros from the left side and leaving the rightmost value. | x>>1 |
<< | Performs the left shift(of 1 bit) operation by inserting the zeros from the right side and leaving the leftmost value. | x<<1 |
Example
Bitwise AND(&) Operator.
x = 0 y = 1 print(x & y)
Output:
0
Example
Bitwise OR(|) Operator.
x = 0 y = 1 print(x | y)
Output:
1
Example
Bitwise NOT(~) Operator.
x = 0 print( ~x )
Output:
1
Example
Bitwise X-OR(^) Operator.
x = 0 y = 1 print(x ^ y)
Output:
1
Example
Bitwise Right Shift(>>) Operator.
x = 14 print( x>>1 )
Output:
7
Example
Bitwise Left Shit(<<) Operator.
x = 14 print( x<<1 )
Output:
28
5. Assignment Operator:
- It is used to assign values to variables.
OPERATOR | FUNCTION | SYNTAX |
---|---|---|
= | It assigns the value to a variable. | x=2 |
+= | It updates the value of x by adding 2 to it. | x+=2 |
-= | It updates the value of x by subtracting 2 from it | x-=2 |
/= | It updates the value of x by dividing it by 2. | x/=2 |
%= | It gives the updated remainder of x. | x%=2 |
//= | It gives the updated near to or less than the value of x. | x//=2 |
**= | It gives the updated value of x by taking its exponent as 2. | x**=2 |
&= | It gives the updated value of x by performing bitwise and operation on x. | x&=2 |
|= | It gives the updated value of x by performing a bitwise or operation on | x|=2 |
^= | It gives the updated value of x by performing a bitwise XOR operation | x^=2 |
>>= | It gives the updated value of x by performing the right shift by 2 operations. | x>>=2 |
<<= | It gives the updated value of x by performing the left shift by 2 operations. | x<<=2 |
Example
Value Assign Operator(=)
x = 10 print(x)
Output:
10
Example
Value Addition Operator(+=)
x = 10 x += 2 print(x)
Output:
12
Example
Value Subtraction Operator(-=)
x = 10 x -= 2 print(x)
Output:
8
Example
Value Division Operator(/=)
x = 10 x /= 2 print(x)
Output:
5.0
Example
Value Modulus Operator(=)
x = 10 x %= 7 print(x)
Output:
3
Example
Floor Operator(//=)
x = 20 x //= 7 print(x)
Output:
2
Example
Exponent Operator(**=)
x = 10 x **= 2 print(x)
Output:
100
Example
Bitwise And Operator(&).
x = 10 x &= 2 print(x)
Output:
2
Example
Bitwise Or Operator(|).
x = 10 x |= 2 print(x)
Output:
10
Example
Bitwise X-OR Operator(^).
x = 10 x ^= 2 print(x)
Output:
8
Example
Bitwise Right Shift(>>=) Operator.
x = 10 x >>= 2 print(x)
Output:
2
Example
Bitwise Left Shift(<<=) Operator.
x = 10 x <<= 2 print(x)
Output:
40
6. Membership Operators:
‘in’ and ‘not in’: They are used to test whether a value or variable is found in a sequence(string, list, tuple, set and dictionary).In the dictionary, it can check only in the keys and not in values.
Example
Using ‘in’ Membership Operator.
cars = ['Lamborghini','Ford','BMW'] print('Ford' in cars)
Output:
True
Example
Using ‘not in’ Membership Operator.
cars = ['Lamborghini','Ford','BMW'] print('Ferrari' not in cars)
Output:
True
Example
cars = {1:'Lamborghini',2:'Ford',3:'BMW'} print('BMW' in cars)
Output:
False
Example
cars = {1:'Lamborghini',2:'Ford',3:'BMW'} print(3 in cars)
Output:
True