ReviseAlgo Logo

Basic Syntax

Operators

Arithmetic, comparison, logical, bitwise operators

Interview: Essential for problem solving

Last Updated: June 12, 2026 7 min read

Python provides a comprehensive set of operators organized into arithmetic, comparison, logical, bitwise, identity, and membership categories. Understanding operator precedence and behavior differences between types is essential.

Arithmetic Operators

  • +, -, * — standard math operations
  • / — true division (always returns float): 5/2 = 2.5
  • // — floor division (rounds toward negative infinity): 5//2 = 2, -5//2 = -3
  • % — modulus: 5%2 = 1. Sign follows the divisor.
  • ** — exponentiation: 2**10 = 1024

Comparison & Logical Operators

  • Comparison: ==, !=, <, >, <=, >=
  • Chained comparison: 1 < x < 10 (equivalent to 1 < x and x < 10)
  • Logical: and, or, not (uses short-circuit evaluation)
  • and returns the first falsy value or the last value; or returns the first truthy value or the last value

Identity & Membership Operators

  • is / is not — test object identity (same memory address), not value equality
  • in / not in — membership test. O(1) for sets/dicts, O(n) for lists/tuples/strings
  • Always use is for None comparison: if x is None: (PEP 8 rule)

Bitwise Operators

  • & (AND), | (OR), ^ (XOR), ~ (NOT)
  • << (left shift), >> (right shift)
  • Frequently used in competitive programming and low-level optimizations

Operator Precedence (highest to lowest)

** > ~ + -(unary) > * / // % > + - > << >> > & > ^ > | > comparisons > not > and > or. When in doubt, use parentheses.

Use Cases

Floor division for integer math and array indexing

Chained comparisons for readable range checks

Short-circuit evaluation for safe null handling

Bitwise operations for competitive programming and flags

Common Mistakes

Using / when you need integer results (use // instead)

Forgetting floor division rounds toward negative infinity for negatives

Using == for None comparison instead of is

Not using parentheses when mixing bitwise and comparison operators