Operators

Once you know how to store data in variables, the next thing to learn is to do something with that data — add numbers, compare values, combine conditions. And that’s what operators are for.

In this tutorial, you’ll learn all the major types of Python operators, how they work, and the order in which Python applies them when a single expression contains more than one.

Contents

  • What Is an Operator in Python?
  • Arithmetic Operators
  • Relational Operators (Comparison)
  • Logical Operators
  • Operators of assignment
  • Bitwise Operators
  • Identity Operators (is, is not)
  • Membership Operators (in, not in)
  • Operator Precedence and Associativity
  • Typical Mistakes
  • Best Practices
  • Interviewing Questions
  • FAQs
  • Summary

What Is an Operator in Python?

An operator in python is a symbol that tells python to do some operation on one or more values which are called operands.

Here, + is the operator, 5 and 3 are the operands and result holds the result. Python groups its operators into several categories, each for a different kind of task — math, comparisons, logic, and so on.

Arithmetic Operators

Arithmetic Operators are used to perform basic mathematical operations on numeric values.

Output:

  • / always returns a float, even if the numbers divide evenly
  • // returns the quotient rounded down to the nearest whole number
  • % returns the remainder left over after division
  • ** raises the first number to the power of the second

Comparision (Relational) operators

Comparison operators compare two values and return a bool (True or False).

Output:

They are most often used inside if statements, loops, etc.

Logical Operators

Logical operators are used to combine multiple conditions and return a bool.

Output:

  • andTrue only if both conditions are True
  • orTrue if at least one condition is True
  • not — flips True to False and vice versa

Assignment Operators

Assignment operators assign values to variables, usually with some other operation .

Output:

The “compound” assignment operators (+=, -=, *=, /=, //=, %=, **=) let you update a variable without repeating the variable’s name on both sides.

Bitwise Operator

Bitwise operators operate directly on the binary representation of integers. They are not as common in everyday beginner code, but they show up in performance-sensitive or low-level work.

Output :

  • & — sets each bit to 1 if both bits are 1
  • | — sets each bit to 1 if either bit is 1
  • ^ — sets each bit to 1 if the bits are different
  • ~ — inverts all the bits
  • << / >> — shifts bits left or right, which multiplies or divides by powers of 2

Identity Operators (is, is not)

Identity operators test whether two variables refer to the same object in memory, not just if their values seem equal.

Output:

This is a common source of confusion for beginners: == matches values, is matches identity.

Membership Operators (in, not in)

Membership operators test whether a value exists in a sequence such as a string, list, or tuple.

Output:

Operator Precedence & Associativity

Python has a fixed order of operations when an expression has more than one operator. (The order of operations in math.)

Output:

Walkthrough:

  • 3 ** 2 evaluates 1st (exponents have the highest precedence) → 9
  • 2 * 9 evaluates 2nd (multiplication before addition) → 18
  • 10 + 18 evaluates last → 28

The general order from highest to lowest is : parentheses ( ) , exponentiation ** , unary operators ( +x , -x , ~x ) , multiplication/division/floor division/modulus ( * / // % ) , addition/subtraction ( + – ) , bitwise shifts , comparisons , not , and , then or . If you are not sure, use parentheses to make the order explicit.

Common Errors

  • Confusing = and == — = is for setting a value, == is for checking equality.
  • is checks identity not equality, use == for comparing values such as numbers or strings, is to compare values
  • Division of two integers always returns a float, even if they divide evenly, it forgets.
  • Misinterpreting operator precedence, believing operators will operate strictly left to right instead of following precedence rules.
  • Chaining and/or without brackets in complex conditions can yield unpredictable results.

Best Practices

  • Use parentheses to make precedence explicit, even if not strictly required. It improves readability .
  • Reserve is for checking against None ( if value is None: ) . Prefer == for value comparisons .
  • Use compound assignment operators (+=, -=, etc.) to reduce code clutter.
  • Don’t use bitwise operators unless you want to work with bits at a low level.
  • Break out long conditional expressions into named variables.

Questions for Interview

Q1. What is the difference between == and is in python?

A. == compares the values of two objects, whereas is checks if they are the exact same object in memory.

Q2. What is the difference between / and //?

A. / is true division and will always return a float. // is floor division and will return the result rounded down to the nearest whole number.

Q3. What is the use of % operator?

A. It returns the remainder left over from a division – for example: 10 % 3 returns 1.

Q4. Why does 10 + 2 * 3 ** 2 equal to 28 not 108?

A. This is because in Python there is operator precedence: exponentiation is done first, then multiplication, then addition.

Q5. What is the difference between and/or and &/| ?

A. and/or are logical operators working on boolean expressions, while &/| are bitwise operators working on the binary representation of integers (but also used for element-wise logic in libraries like numpy).

FAQs

Q: Does Python have a ++ increment operator?

A. No. Python doesn’t have ++ or –; use += 1 or -= 1 instead.

Q: What does not in?

A. It tests that a value does not exist in a sequence. If the value is absent from the sequence, it returns True.

Q: Can I compare strings with < and > in Python?

A. Yes, you can compare strings using < and > in Python. Yes. String ordering is lexicographic (dictionary order) based on the Unicode value of the elements.

Q: is better than == ever?

A. Yes, especially when testing against singletons such as None, True or False. i.e. if x is None: is the preferred style.

Summary

  • In Python, operators are divided into the categories of arithmetic, comparison, logical, assignment, bitwise, identity, and membership.
  • Arithmetic operators are for math, comparison operators return booleans, logical operators combine conditions. == checks for equality of value, is checks for object identity – a common source of bugs.
  • Parentheses explicitly show the order that operator precedence dictates operations run in a single expression.
  • Compound assignment operators (+=, -=, etc.) provide a concise way to update variables in place.