Skip to main content

Module 4: Operators

In this module, we will learn about operators. Operators are symbols used to perform calculations, make comparisons, and run logical tests in Python.


4.1 Arithmetic Operators

These operators are used for basic mathematical calculations:

OperatorNameDescriptionExample ($x = 10, y = 3$)Result
+AdditionAdds two numbersx + y13
-SubtractionSubtracts one number from anotherx - y7
*MultiplicationMultiplies two numbersx * y30
/DivisionDivides numbers (always returns a float)x / y3.3333
//Floor DivisionDivides numbers and rounds down to the nearest whole numberx // y3
%ModulusReturns the remainder of a divisionx % y1
**ExponentiationCalculates power ($x$ raised to the power $y$)x ** y (i.e. $10^3$)1000
x = 10
y = 3
print("Division:", x / y) # Output: 3.3333333333333335
print("Floor Division:", x // y) # Output: 3
print("Remainder:", x % y) # Output: 1
print("Power:", x ** y) # Output: 1000

4.2 Assignment Operators

These operators are used to store or update values in variables:

OperatorExampleEquivalent to
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3x = x % 3
a = 10
a += 5 # Adds 5 to a and saves it
print(a) # Output: 15

4.3 Comparison Operators

These operators are used to compare two values. They always return a boolean value: True or False.

OperatorNameExample ($a = 10, b = 20$)Result
==Equal toa == bFalse
!=Not Equal toa != bTrue
>Greater Thana > bFalse
<Less Thana < bTrue
>=Greater Than or Equal toa >= bFalse
<=Less Than or Equal toa <= bTrue
x = 15
y = 15
print(x == y) # Output: True
print(x != y) # Output: False

4.4 Logical Operators

These operators are used to combine multiple conditions:

  • and: Returns True only if both conditions are True.
  • or: Returns True if at least one condition is True.
  • not: Reverses the result (returns False if the result is True, and vice versa).
x = 10
print(x > 5 and x < 20) # True and True => Output: True
print(x > 15 or x < 20) # False or True => Output: True
print(not(x == 10)) # not(True) => Output: False

4.5 Membership Operators

These operators check if a value is present in a collection (like a list or string):

  • in: Returns True if the value is found.
  • not in: Returns True if the value is not found.
fruits = ["apple", "banana", "mango"]

print("apple" in fruits) # Output: True
print("grapes" in fruits) # Output: False
print("grapes" not in fruits) # Output: True

4.6 Identity Operators

These operators check if two variables point to the same memory location (or the same object):

  • is: Returns True if both variables are the exact same object.
  • is not: Returns True if they are different objects.
x = [1, 2, 3]
y = [1, 2, 3]
z = x

# x and y have the same values, but they are different list objects in memory
print(x == y) # Output: True (values are the same)
print(x is y) # Output: False (different memory locations)

print(x is z) # Output: True (z is assigned to x, so they point to the same memory)