Python

Constant dripping wears away a stone

Operators


There are different types of operators:

  • Value assignment operator =

    Please note that this operator has a different meaning from the equal sign in math. For example:

    • x = 1: x is assigned 1. It's not correct to say that x is equal to 1 for the statement.
    • x = x + 1: Add the value of x and 1, and assign the result to variable x. In other words, this statment will increase variable x's value by one.
  • Arithmetic Operators
    • Addition + (a = b + c): Add the value of b and the value of c and assign the result to a.
    • Subtraction - (a = b - c): Subtract the value of c from the value of b and assign the result to a.
    • Multiplication * (a = b * c): Multiply the value of b and the value of c and assign the result to a.
    • Division / (a = b / c): Divide the value of c from the value of b and assign the result to a.
    • Modulus % (a = b % c): Divide the value of c from the value of b and assign the remainder to a.
    • Floor Division // (a = b // c): Divide the value of c from the value of b, round the result down to the nearest whole number and assign it to a.
    • Exponentiation ** (a = b ** c): Calculate the value of b to the power of the value of c and assign the result to a.
  • Compound Assignment Operators
    • Addition assignment += (a += b): Add the value of a and the value of b and assign the result back to a.
    • Subtraction assignment -= (a -= b): Subtract the value of b from the value of a and assign the result back to a.
    • Multiplication assignment *= (a *= b): Multiply the value of a and the value of b and assign the result back to a.
    • Division assignment /= (a /= b): Divide the value of b from the value of a and assign the result back to a.
    • Modulus assignment %= (a %= b): Divide the value of b from the value of a and assign the remainder back to a.
    • Floor Division assignment //= (a //= b): Divide the value of a from the value of b, round the result down to the nearest whole number and assign it back to a.
    • Exponentiation assignment **= (a **= b): Calculate the value of a to the power of the value of b and assign the result back to a.
  • Boolean Operators:
    • and: returns True if both operands are true; otherwise, returns False.
    • or: returns True if either of the operands is true; otherwise, return False.
    • in: returns True if the left operand exists in the right operand, which is usually an iterable object, such as a tuple, a list, etc.
    • is: returns True if both operands refer to the same object; otherwise, returns False.
    • not: It is a singular operator. NOT expressionreturns True if expressionis False; otherwise, returns False.
    • ==: returns True if both operands have the same values; otherwise, returns False.
    • !=: returns True if both operands have different values; otherwise, returns False.
    • <: returns True if the left operand is less than the right operand; otherwise, returns False.
    • <=: returns True if the left operand is less than or equal to the right operand; otherwise, returns False.
    • >: returns True if the left operand is greater than the right operand; otherwise, returns False.
    • >=: returns True if the left operand is greater than or equal to the right operand; otherwise, returns False.