Python Operators – A Beginner’s Guide

What are Operators?

In Python, operators are special symbols used to perform operations on values and variables. They allow you to perform calculations, compare values, make decisions.

Types of Operators

  • Arithmetic Operators
  • Comparison (Relational) Operators
  • Logical Operators
  • Assignment Operators
  • Membership Operators
  • Identity Operators
  • Bitwise Operators
Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

OperatorDescriptionExample
+Addition5 + 3 = 8
-Subtraction10 - 4 = 6
*Multiplication4 * 2 = 8
/Division10 / 2 = 5.0
%Modulus (remainder)10 % 3 = 1
**Exponentiation2 ** 3 = 8
//Floor division7 // 2 = 3
a = 10
b = 3

print(a + b)
print(a % b)
print(a ** b)
Comparison Operator

These operators compare two values and return a boolean result (True or False).

OperatorMeaning
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
x = 10
y = 5

print(x > y)    # True
print(x == y)   # False
Logical Operators

Logical operators are used to combine conditional statements.

OperatorMeaning
andTrue if both conditions are true
orTrue if at least one condition is true
notReverses the condition
age = 25
has_id = True

print(age > 18 and has_id)
Assignment Operators

Assignment operators assign values to variables.

OperatorExampleEquivalent To
=x = 5Assign value
+=x += 2x = x + 2
-=x -= 2x = x - 2
*=x *= 2x = x * 2
/=x /= 2x = x / 2
x = 10
x += 5
print(x)
Membership Operators

Membership operators test whether a value exists inside a sequence.

OperatorDescription
inReturns True if value is found
not inReturns True if value is not found
fruits = ["apple", "banana", "mango"]

print("apple" in fruits)
print("grape" not in fruits)
Identity Operators

Identity operators check if two variables refer to the same object in memory.

OperatorMeaning
isTrue if both variables refer to same object
is notTrue if they do not
a = [1, 2, 3]
b = a
c = [1, 2, 3]

print(a is b)   # True
print(a is c)   # False
Bitwise Operators

Bitwise operators work on binary (bit-level) representations of numbers. Instead of working with whole values, they operate on individual bits (0s and 1s).

OperatorNameDescription
&ANDSets bit to 1 if both bits are 1
|ORSets bit to 1 if either of the bits are 1
^XORSets bit to 1 if bits are different
~NOTInverts all bits
<<Left ShiftShifts bits to the left
>>Right ShiftShifts bits to the right

Example

a = 5   # 0101
b = 3   # 0011

print(a & b)  # Output: 1

Explanation

0101  AND (Both Bits must be 1)
0011
----
0001 → 1

Operator Precedence

Operator precedence is used when multiple operators are used in a single expression. The official Python Doc has a Precedence table which goes from highest precedence (most binding) to lowest precedence (least binding). Operators in same cell have the same precedence

Worthwhile Books to learn Python from

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top