Last Updated on January 2, 2023 by Prepbytes
The python operator is used to do operations on variable and their values. For example, if we want to add two numbers then we use + is called an operator. Let’s see one example to understand operators in python.
num1 = 10
num2 = 20
ans = num1 + num2
print(ans)
# output:- 30
In the above program, + is an operator while num1 and num2 are operands. In this article, we will see various types of operators in python.
Types of Operators in Python:-
There are different types of operators in python listed below.
- Arithmetic Operators
- Comparision Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Identity Operators
- Membership Operators
Arithmetic Operators:-
Arithmetic operators are used to performing basic arithmetic operations like addition, subtraction, multiplication, division, and others. All the arithmetic python operators are given in the below table.
Operator | Syntax | Description |
---|---|---|
+ (Addition) | A+B | adds A and B |
–Â (Subtraction) | A-B | subtract B from A |
*Â (Multiplication) | A*B | multiply A and B |
/ (Division) | A/B | divide A by B (result: float) |
// (Division) | A//B | divide A by B (result: floor (int)) |
% (Modulus) | A%B | gives reminder of A divide B |
** (Power) | A**B | gives A power of B |
Precedence order of operators are Parenthese, Exponential, Multiplication, Division, Addition, and Subtraction. Let’s understand how to use all the python operators in code with an example.
A=18
B=5
add = A+B
sub = A-B
mul = A*B
div_float = A/B
div_floor = A//B
mod = A%B
power = A**B
print("A + B = "+str(add))
print("A - B = "+str(sub))
print("A * B = "+str(mul))
print("A / B = "+str(div_float))
print("A // B = "+str(div_floor))
print("A % B = "+str(mod))
print("A ** B = "+str(power))
Output:
A + B = 23
A - B = 13
A * B = 90
A / B = 3.6
A // B = 3
A % B = 3
A ** B = 1889568
Comparision Operator:-
Comparison operators are used to making a comparison between values of two operands. It gives output as True or False based on the comparison. All the Comparison Python Operators are given in the below table.
Operator | Syntax | Description |
---|---|---|
\== (Equal to) | A==B | return true if A and B are equal |
> (Greater than) | A>B | return true if A is greater than B |
< (Less than) | A<B | return true if A is less than B |
!= (Not equal to) | A!=B | return true if A is not equal to B |
>= (Greater than or equal to) | A>=B | return true if A is greater than or equal to B |
<= (Less than or equal to) | A<=B | return true if A is less than or equal to B |
Let’s understand above python operators using an example.
A=18
B=5
print("A == B is "+str(A==B))
print("A > B is "+str(A>B))
print("A= B is "+str(A>=B))
print("A < = B is "+str(A <= B))
Output:
A == B is False
A > B is True
A < B is False
A != B is True
A >= B is True
A <= B is False
Logical Operator:-
Logical operators are used to performing logical operations like logical AND, logical OR. and logical NOT. All the logical python operators are given in the below table.
Operator | Syntax | Description |
---|---|---|
and (Logical and) | A and B | Return true only if A and B both are true |
or (Logical or) | A or B | Return true if either A or B is true |
not (logical not) | not A | Return true if A is false and vice versa |
Let’s understand above python operators using an example.
A=True
B=False
_and = A and B
_or = A or B
_not = not A
print("A and B = "+str(_and))
print("A or B = "+str(_or))
print("not A = "+str(_not))
Output:
A and B = False
A or B = True
not A = False
Bitwise Operator:-
Bitwise operators are used to performing bitwise operations like bitwise and, bitwise or and many more. All the Bitwise Python Operators are given in the below table.
Operator | Syntax | Description | |
---|---|---|---|
& (Bitwise and) | A & B | return A bitwise and B | |
(Bitwise or) | A | B | return A bitwise or B |
~ (Bitwise not) | ~ A | return bitwise not of A | |
^ (Bitwise xor) | A ^ B | return A bitwise xor B | |
>> (Bitwise right shift) | A >> B | return A right shift by B | |
<< (Bitwise left shift) | A << B | return A left shift by B |
Let’s understand bitwise python operators with help of an example.
A=8
B=3
_and = A & B
_or = A | B
_not = ~ A
_xor = A ^ B
_right_shift = A >> B
_left_shift = A << B
print("A & B = "+str(_and))
print("A | B = "+str(_or))
print("~ A = "+str(_not))
print("A ^ B = "+str(_xor))
print("A >> B = "+str(_right_shift))
print("A << B = "+str(_left_shift))
Output:
A & B = 0
A | B = 11
~ A = -9
A ^ B = 11
A >> B = 1
A << B = 64
Assignment Operator:-
Assignment operators are used to assign values to python variables. All the Assignment Python Operators are given in the below table.
Operator | Syntax | Same As | Description | |||
---|---|---|---|---|---|---|
\= | A=10 | A=10 | Assign value 10 to variable A | |||
+= | A+=B | A=A+B | Add value B into the value of variable A and assign it to variable A | |||
-= | A-=B | A=A-B | Subtract the value of B from the value of A and assign it to variable A | |||
*= | A*=B | A=A*B | Multiply the values of A and B and assign it to variable A | |||
/= | A/=B | A=A/B | Divide value of A by B and assign it’s float value to variable A | |||
//= | A//=B | A=A//B | Divide value of A by B and assign it’s floor value to variable A | |||
%= | A%=B | A=A%B | Get A modulus of B and assign it to variable A | |||
**= | A**=B | A=A**B | Get A power of B and assign it to variable A | |||
&= | A&=B | A=A&B | Get bitwise and of A and B and assign it to variable A | |||
= | A | =B | A=A | B | Get bitwise or of A and B and assign it to variable A | |
^= | A^=B | A=A^B | Get bitwise xor of A and B and assign it to variable A | |||
>>= | A>>=B | A=A>>B | Get A left shift by B and assign it to variable A | |||
<<= | A<<=B | A=A<<B | Get A left shift by B and assign it to variable A |
Let’s understand Assignment Python Operators with help of an example.
A,B=8,3
A+=B
print("'A += B' = "+str(A))
A,B=8,3
A-=B
print("'A -= B' = "+str(A))
A,B=8,3
A*=B
print("'A *= B' = "+str(A))
A,B=8,3
A/=B
print("'A /= B' = "+str(A))
A,B=8,3
A//=B
print("'A //= B' = "+str(A))
Output:-
'A += B' = 11
'A -= B' = 5
'A *= B' = 24
'A /= B' = 2.6666666666666665
'A //= B' = 2
Identity Operator:-
Identity operators are used to check whether the memory locations of values of two variable are same or not. All the Identity Python Operators are given in the below table.
Operator | Syntax | Description |
---|---|---|
is | A is B | Return true if memory locations of A and B are the same else return false. |
is not | A is not B | Return true if memory locations of A and B are not the same else return false |
Let’s understand identity python operators with help of an example.
A=10
B=5
C=A
print(A is B)
print(A is not B)
print(A is C)
Output:-
False
True
True
Membership operators:-
Membership operators are used to check whether an operand belongs to any group like list, tuple, set. All the Membership Python Operators are given in the below table.
Operator | Syntax | Description |
---|---|---|
in | A in B | Return true if A belongs to B else return False |
not in | A not in B | Return true if A does not belong to B else return False |
Let’s understand membership python operators with help of an example.
_list=["C","C++","Java","Python"]
print("Python" in _list)
print("PHP" in _list)
print("Python" not in _list)
print("PHP" not in _list)
Output:
True
False
False
True