Python Tutorial

Python Operators

Learn Python operators with clear tables and examples for arithmetic, comparison, assignment, logical, bitwise, membership, identity, and precedence rules.

What are Python operators?

Operators are symbols or words that perform an operation on one or more values. The values being operated on are called operands. For example, in total = price + tax, + adds two operands and = assigns the result to total.

Types of Python operators

CategoryOperatorsTypical use
Arithmetic+ - * / // % **Calculations with numbers.
Comparison== != < <= > >=Compare values and produce a Boolean result.
Assignment= += -= *= /= //= %= **=Assign or update a variable.
Logicaland or notCombine or reverse conditions.
Bitwise& | ^ ~ << >>Operate on the bits of integers.
Membershipin not inCheck whether a value exists in a collection.
Identityis is notCheck whether two names refer to the same object.

1. Arithmetic operators

OperatorMeaningExampleResult
+Addition15 + 419
-Subtraction15 - 411
*Multiplication15 * 460
/True division15 / 43.75
//Floor division15 // 43
%Remainder15 % 43
**Exponentiation2 ** 38
a = 15
b = 4
print(a + b)
print(a / b)
print(a // b)
print(a % b)
print(2 ** 3)

Python’s / operator returns a floating-point result. Floor division rounds down, which is important for negative values as well as positive values.

2. Comparison operators

OperatorMeaningExample
==Equal toage == 18
!=Not equal tostatus != "closed"
<Less thanscore < 50
<=Less than or equal toitems <= 10
>Greater thanprice > 100
>=Greater than or equal toscore >= 50

Comparison expressions normally produce True or False and are commonly used in conditions:

score = 72
if score >= 50:
    print("Pass")

3. Assignment operators

The basic assignment operator stores a value. Augmented assignment combines an operation with assignment:

OperatorEquivalent formExample
=Assigntotal = 10
+=total = total + 5total += 5
-=total = total - 5total -= 5
*=total = total * 5total *= 5
/=total = total / 5total /= 5
//=total = total // 5total //= 5
%=total = total % 5total %= 5
**=total = total ** 5total **= 5

4. Logical operators

OperatorMeaningExample
andTrue when both expressions are truthy.age >= 18 and active
orTrue when at least one expression is truthy.admin or owner
notReverses a truth value.not completed

Python’s logical operators use short-circuit evaluation. For example, the right side of and may not run if the left side is already false.

5. Bitwise operators

Bitwise operators work on the binary representation of integers. They are useful for flags, masks, permissions, and low-level protocols.

OperatorMeaningExample
&Bitwise AND6 & 3
|Bitwise OR6 | 3
^Bitwise XOR6 ^ 3
~Bitwise inversion~6
<<Shift bits left6 << 1
>>Shift bits right6 >> 1

6. Membership operators

in and not in test whether a value is contained in a string, list, tuple, set, or dictionary. For a dictionary, membership checks keys by default:

languages = ["Python", "Java"]
print("Python" in languages)
print("Go" not in languages)

student = {"name": "Asha"}
print("name" in student)

7. Identity operators

is and is not test whether two names refer to the same object. They are different from ==, which compares values:

first = [1, 2]
second = [1, 2]
same = first

print(first == second)  # True: values are equal
print(first is second)  # False: different list objects
print(first is same)    # True: same object

Use is None and is not None for None. Use == when you want to compare ordinary values.

Operator precedence

Precedence determines which operation is evaluated first. Parentheses make the intended order clear and are recommended when an expression could be misunderstood.

Higher priorityOperators
1**
2Unary +x, -x, ~x
3* / // %
4+ -
5<< >>
6&, then ^, then |
7Comparisons, in, is
8not, then and, then or
result = (10 + 5) * 2
print(result)

Operator best practices

  • Use parentheses when they improve readability.
  • Use == for value comparison and is mainly for identity checks such as None.
  • Be careful when mixing strings and numbers; convert values explicitly when needed.
  • Use bitwise operators only when the bit-level behavior is intentional and documented.

Further Reading

Continue learning with these related Python tutorials:

Continue learning