A guide to all Python operators

Arithmetic Operators

Arithmetic operators are used for performing mathematical operations.

  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division
  • %: Modulus (remainder)
  • **: Exponentiation
  • //: Floor Division (quotient, rounded down to the nearest whole number)

Comparison Operators

Comparison operators are used to compare values.

  • ==: Equal to
  • !=: Not equal to
  • >: Greater than
  • <: Less than
  • >=: Greater than or equal to
  • <=: Less than or equal to

Logical Operators

Logical operators are used to combine or modify Boolean values.

  • and: Logical AND (True if both operands are True)
  • or: Logical OR (True if at least one operand is True)
  • not: Logical NOT (inverts the Boolean value)

Assignment Operators

Assignment operators are used to assign values to variables.

  • =: Assigns the value on the right to the variable on the left
  • +=: Adds the right operand to the left operand and assigns the result to the left operand
  • -=: Subtracts the right operand from the left operand and assigns the result to the left operand
  • *=: Multiplies the left operand by the right operand and assigns the result to the left operand
  • /=: Divides the left operand by the right operand and assigns the result to the left operand
  • %=: Computes the modulus of the left operand and the right operand and assigns the result to the left operand
  • **=: Calculates the exponentiation of the left operand by the right operand and assigns the result to the left operand
  • //=: Performs floor division on the left operand by the right operand and assigns the result to the left operand
  • := (Walrus Operator): Assigns a value to a variable as part of an expression

Bitwise Operators

Bitwise operators perform operations on binary representations of numbers.

  • &: Bitwise AND
  • |: Bitwise OR
  • ^: Bitwise XOR (exclusive OR)
  • ~: Bitwise NOT (complement)
  • <<: Left Shift
  • >>: Right Shift

Membership Operators

Membership operators are used to test if a value is present in a sequence.

  • in: Returns True if a value is found in the sequence
  • not in: Returns True if a value is not found in the sequence

Identity Operators

Identity operators are used to compare the memory location of two objects.

  • is: Returns True if both variables point to the same object
  • is not: Returns True if both variables point to different objects

Reference:

8 Likes

Nice! You should make one for JavaScript

2 Likes

Sure! I’ll make one right now!

2 Likes

I think we need to add a small introduction and examples of using operators.

2 Likes

you can never forget the walrus teeth.

3 Likes

You forgot many operators such as ternary conditional, unpacking (part of displays), matrix multiplication, subscription, calling, attribute reference.

I know it’s a wiki but I’m too lazy to edit it

4 Likes

I had no idea ** was even a thing…like ever…not even in JS :open_mouth:

2 Likes