Python Operators

python operators enable manipulation on variables and value(operands)

  1. Arithmetic Operators
  2. Assignment Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Membership Operators
  6. Identity Operators

    1. Arithmetic Operators
    OperatorDescriptionSyntaxExample
    +Additionx + y1 + 2 = 3
    Subtractionx - y3 - 1 = 2
    *Multiplicationx * y2 * 3 = 6
    /Divisionx / y6 / 3 = 2.0
    %Modulus (returns the remainder of division)x % y7 % 3 = 1
    **Exponentiation (raise to the power of)x ** y2 ** 3 = 8
    //Floor division (rounds down to the nearest whole number)x // y7 // 3 = 2

    2. Assignment Operators
    OperatorDescriptionSyntaxExample
    ==Equal tox == y2 == 2 returns True
    !=Not equal tox != y2 != 3 returns True
    >Greater thanx > y3 > 2 returns True
    <Less thanx < y2 < 3 returns True
    >=Greater than or equal tox >= y3 >= 3 returns True
    <=Less than or equal tox <= y2 <= 2 returns True

    3. Logical Operators
    OperatorDescriptionSyntaxExample
    andLogical ANDx and yTrue and False returns False
    orLogical ORx or yTrue or False returns True
    notLogical NOTnot xnot False returns True

    4. Bitwise Operators
    Bitwise OperatorsDescriptionSyntaxExample
    &Bitwise ANDx & y5 & 3 returns 1
    Bitwise OR`x
    ^Bitwise XORx ^ y5 ^ 3 returns 6
    ~Bitwise NOT~x~5 returns -6
    <<Bitwise Left Shiftx << y5 << 2 returns 20
    >>Bitwise Right Shiftx >> y5 >> 2 returns 1

    5. Membership Operators
    Membership OperatorsDescriptionSyntaxExample
    inReturns True if a value is found in a sequence (e.g. list, tuple, string, etc.)x in y2 in [1, 2, 3] returns True
    not inReturns True if a value is not found in a sequence (e.g. list, tuple, string, etc.)x not in y2 not in [4, 5, 6] returns True

    6. Identity Operators
    Identity OperatorsDescriptionSyntaxExample
    isReturns True if two variables refer to the same object in memoryx is ya = [1, 2, 3] and b = [1, 2, 3] and a is b returns False
    is notReturns True if two variables do not refer to the same object in memoryx is not ya = [1, 2, 3] and b = [1, 2, 3] and a is not b returns True

    These operators can be used in various combinations to perform more advanced operations in Python.

    Example code:

    Leave a Comment

    Your email address will not be published. Required fields are marked *

    Scroll to Top