Python Operators – MyPython Fundamentals

In Python, the following are the commonly used operators:

  1. Arithmetic Operators:
    • (addition) – Adds two values and returns the result.
    • (subtraction) – Subtracts one value from another and returns the result.
    • (multiplication) – Multiplies two values and returns the result. / (division) – Divides one value by another and returns the result (as a float if one of the values is a float). % (modulo) – Returns the remainder of dividing one value by another. // (floor division) – Divides one value by another and rounds down to the nearest integer. ** (exponentiation) – Raises one value to the power of another.
  2. Comparison Operators:
    • == (equal to) – Returns True if two values are equal, and False otherwise.
    • != (not equal to) – Returns True if two values are not equal, and False otherwise.
    • (greater than) – Returns True if one value is greater than the other, and False otherwise.
    • < (less than) – Returns True if one value is less than the other, and False otherwise.
    • = (greater than or equal to) – Returns True if one value is greater than or equal to the other, and False otherwise.
    • <= (less than or equal to) – Returns True if one value is less than or equal to the other, and False otherwise.
  3. Assignment Operators:
    • = (assignment) – Assigns a value to a variable.
    • += (add and assign) – Adds a value to a variable and then assigns the result to the variable.
    • -= (subtract and assign) – Subtracts a value from a variable and then assigns the result to the variable.
    • *= (multiply and assign) – Multiplies a variable by a value and then assigns the result to the variable.
    • /= (divide and assign) – Divides a variable by a value and then assigns the result to the variable.
    • %= (modulo and assign) – Finds the remainder of dividing a variable by a value and then assigns the result to the variable.
    • //= (floor division and assign) – Divides a variable by a value and then rounds down to the nearest integer and assigns the result to the variable.
    • **= (exponentiation and assign) – Raises a variable to the power of another value and then assigns the result to the variable.
  4. Logical Operators: and – Returns True if both values are True, and False otherwise. or – Returns True if either value is True, and False otherwise. not – Negates a value; if the value is True, returns False, and if the value is False, returns True.
  5. Identity Operators: is – Returns True if both values refer to the same object, and False otherwise. is not – Returns True if both values do not refer to the same object, and False otherwise.
  6. Membership Operators: in – Returns True if a value is found in a specified sequence (such as a list or string), and False otherwise. not in – Returns True if a value is not found in a specified sequence, and False otherwise.
  7. Bitwise Operators: & (and) – Compares two values bit by bit and returns a value where each bit is set

These operators can be used to perform operations on values, compare values, assign values, and more. It’s important to understand the basic behavior of each operator and when to use them.