Operators in Python (Arithmetic, Logical, Comparison, etc.)

August 01, 2025   5 Min Read 133

What You’ll Learn in This Blog

  • Types of operators in Python
  • How to use them with examples
  • Operator precedence
  • Python-specific operators (is, in)
  • Practical coding examples

What are Operators?

Operators are symbols or keywords that perform operation on variables and values. Python supports a wide range of operator types.

1. Arithmetic Operators

OperatorDescriptionExampleOutput
+Addition5 + 27
-Subtraction5 - 23
*Multiplication5 * 210
/Division5 / 22.5
//Floor Division5 // 22
%Modulus (remainder)5 % 21
**Exponentiation2 ** 38

a = 10
b = 3
print(a + b)  # 13
print(a ** b) # 1000

2. Comparison (Relational) Operators

Used to compare two values. Returns True or False.

OperatorMeaningExample
==Equal to5 == 5True
!=Not equal to5 != 3True
>Greater than5 > 3True
<Less than5 < 3False
>=Greater or equal5 >= 5True
<=Less or equal3 <= 5True

x = 5
print(5 == x)  # True
print(2 == x)  # False
print(2 != x)  # True

3. Logical Operators

Used to combine conditional statements.

OperatorDescriptionExample
andTrue if both are TrueTrue and TrueTrue
orTrue if any is TrueTrue or FalseTrue
notInverts the resultnot TrueFalse

x = 5
print(x > 2 and x < 10)  # True

4. Assignment Operators

Assign values to variables.

OperatorExampleSame As
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 2x = x - 2
*=x *= 2x = x * 2
/=x /= 2x = x / 2
%=x %= 2x = x % 2
**=x **= 2x = x ** 2

x = 10
x += 5
print(x)  # 15

5. Identity Operators

Used to check if two variables refer to the same object.

OperatorMeaningExample
isTrue if both refer to same objx is y
is notTrue if not same objectx is not y

a = [1, 2]
b = a
print(a is b)      # True
print(a is not b)  # False

6. Membership Operators

Used to test if a sequence contains a value.

OperatorMeaningExample
inTrue if value exists3 in [1,2,3]
not inTrue if value doesn’t4 not in [1,2,3]

7. Bitwise Operators (Advanced)

Perform bit-level operations.

OperatorMeaningExample
&AND5 & 31
``OR
^XOR5 ^ 36
~NOT~5-6
<<Left Shift2 << 28
>>Right Shift8 >> 22

Operator Precedence

Some operators are evaluated before others.


result = 2 + 3 * 4  # Output: 14, not 20!

Precedence (from high to low):


1. ** (Exponent)
2. *, /, %, //
3. +, -
4. Comparison (>, <, ==)
5. Logical (not → and → or)

Use parentheses () to control evaluation order.

Summary

Operator TypeExamples
Arithmetic+, -, *, /
Comparison==, !=, <, >
Logicaland, or, not
Assignment=, +=, *=
Identityis, is not
Membershipin, not in
Bitwise&, `

Task

  1. Write a program that checks:
    • If a number is even
    • If the number is greater than 10
    • And print a message accordingly using and, or
  2. Try using:
    • is, in with lists and integers
    • Assignment operators like **=, //=

Leave a Reply