Learn Python Programming  /  Getting Started  /  Operators

Operators

Operators are the symbols Python uses to perform calculations, comparisons, and logical tests. This page covers every operator you will actually use day to day.

Arithmetic Operators

R Code
x = 17
y = 5

print(x + y)    # addition
print(x - y)    # subtraction
print(x * y)    # multiplication
print(x / y)    # division (always returns a float)
print(x // y)   # floor (integer) division
print(x % y)    # modulus (remainder)
print(x ** y)   # exponentiation
Output
22
12
85
3.4
3
2
1419857
⚠ Common Mistake
In Python 3, / always returns a float, even when dividing two integers evenly (10 / 2 gives 5.0, not 5). Use // if you specifically need integer division.

Comparison Operators

R Code
a = 10
b = 20

print(a == b)   # equal to
print(a != b)   # not equal to
print(a >  b)   # greater than
print(a <  b)   # less than
print(a >= 10)
print(a <= 5)
Output
False
True
False
True
True
False

Logical Operators

Python spells these out as words rather than symbols: and, or, not.

R Code
age = 20
has_id = True

print(age >= 18 and has_id)
print(age < 18 or has_id)
print(not has_id)
Output
True
True
False

Membership Operators: in / not in

R Code
fruits = ["apple", "banana", "mango"]

print("banana" in fruits)
print("grape" not in fruits)
Output
True
True

Identity Operators: is / is not

== checks if two values are equal; is checks if they are literally the same object in memory. These are not interchangeable.

R Code
a = [1, 2, 3]
b = [1, 2, 3]
c = a

print(a == b)   # same VALUES -> True
print(a is b)    # different OBJECTS in memory -> False
print(a is c)    # c points to the exact same object as a
Output
True
False
True
⚠ Common Mistake
Never use is to compare numbers or strings for equality (if x is 5 can behave unpredictably due to how Python caches small integers internally). Always use == for value comparisons; reserve is strictly for checking against None, e.g. if x is None.

Key Points

  • / always returns a float in Python 3; use // for integer (floor) division.
  • Logical operators are spelled out as words: and, or, not.
  • Use in / not in to test membership in a list, string, or other collection.
  • Use == for value equality and is only for identity checks (especially against None).