[Python Operators] What is the operator in Python? How many types of operators are in Python?

  1. Arithmetic operators
  2. Comparison operators
  3. Assignment Operators
  4. Logical Operators
  5. Bitwise Operators
  6. Membership Operators
  7. Identity Operators

1. Arithmetic operators

+ (addition)
- (subtraction)
*(multiplication)
/(divide), %(reminder)
//(floor division) 
exponent (**) operators.
OperatorNameExample
+Additionx + y
Subtractionx – y
*Multiplicationx * y
/Divisionx / y
%Modulusx % y
**Exponentiationx ** y
//Floor divisionx // y
# Arithmetic Operator
a = 9
b = 4

# Addition 
a1 = a + b

# Subtraction 
a2 = a - b

# Multiplication 
a3 = a * b

# Division(float) 
a4 = a / b

# Division(floor) 
a5 = a // b

# Modulo of both number
a6 = a % b

# Power
a7 = a ** b

# print results
print(a1)
print(a2)
print(a3)
print(a4)
print(a5)
print(a6)
print(a7)

Output
13
5
36
2.25
2
1
6561

2. Comparison operators

OperatorNameExample
==Equalx == y
!=Not equalx != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equal tox <= y
# Relational Operators
a = 13
b = 33

# a > b is False
print(a > b)

# a < b is True
print(a < b)

# a == b is False
print(a == b)

# a != b is True
print(a != b)

# a >= b is False
print(a >= b)

# a <= b is True
print(a <= b)

Output
False
True
False
True
False
True

3. Assignment Operators

OperatorExampleSame As
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 3x = x – 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3x = x % 3
//=x //= 3x = x // 3
**=x **= 3x = x ** 3
&=x &= 3x = x & 3
|=x |= 3x = x | 3
^=x ^= 3x = x ^ 3
>>=x >>= 3x = x >> 3
<<=x <<= 3x = x << 3
# Assignment Operators
a = 10

# Assign value
b = a
print(b)

# Add and  value
b += a
print(b)

# Subtract and  value
b -= a
print(b)

# multiply and assign
b *= a
print(b)

# bitwise lishift operator
b <<= a
print(b)

Output
10
20
10
100
102400

4. Logical Operators

OperatorDescriptionExample
andReturns True if both statements are truex < 5 and  x < 10
orReturns True if one of the statements is truex < 5 or x < 4
notReverse the result, returns False if the result is truenot(x < 5 and x < 10)
# Logical Operator
a = True
b = False

# Print a and b is False
print(a and b)

# Print a or b is True
print(a or b)

# Print not a is False
print(not a)

Output:
False
True
False

5. Bitwise Operators

OperatorNameDescription
&ANDSets each bit to 1 if both bits are 1
|ORSets each bit to 1 if one of two bits is 1
 ^XORSets each bit to 1 if only one of two bits is 1
~NOTInverts all the bits
<<Zero fill left shiftShift left by pushing zeros in from the right and let the leftmost bits fall off
>>Signed right shiftShift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off
# Bitwise operators
a = 10
b = 4

# AND operation
print(a & b)

# OR operation
print(a | b)

#  NOT operation
print(~a)

#  XOR operation
print(a ^ b)

# right shift operation
print(a >> 2)

# left shift operation
print(a << 2)

Output:
0
14
-11
14
2
40

6. Membership Operators

OperatorDescriptionExample
inReturns True if a sequence with the specified value is present in the objectx in y
not inReturns True if a sequence with the specified value is not present in the objectx not in y

x = 24
y = 20
list = [15, 20, 35, 40, 50]

if (x not in list):
	print("x is NOT present in given list")
else:
	print("x is present in given list")

if (y in list):
	print("y is present in given list")
else:
	print("y is NOT present in given list")

Output
x is NOT present in given list
y is present in given list

7. Identity Operators

OperatorDescriptionExample
is Returns True if both variables are the same objectx is y
is notReturns True if both variables are not the same objectx is not y
a = 10
b = 20
c = a

print(a is not b)
print(a is c)

Output:
True
True

Keywords:

What is the operator in Python?
What are the 3 operators in Python?
What are the 4 operators in Python?
How many types of operators are in Python?
Arithmetic operators,
Comparison operators,
Assignment Operators,
Logical Operators,
Bitwise Operators,
Membership Operators,
Identity Operators,

Spread the love

Leave a Comment

Your email address will not be published.

Scroll to Top