OPERATORS



Python - OPERATORS


Operators are used to perform operations on variables and values.


Types of Operators :


  • Arithmetic operators

  • Assignment operators

  • Comparison/Relational operators

  • Logical operators

  • Identity operators

  • Membership operators

  • Bitwise operators


    ARITHMETIC OPERATORS :

    Operator Name Example
    + Additionx + y
    - Subtractionx - y
    * Multiplicationx * y
    / Divisionx / y
    % Modulusx % y
    ** Exponentiationx ** y
    // Floor divisionx // y
    Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> X=4
    >>> Y=8
    >>> X+Y (Addition)
    12
    >>> X-Y (Subtraction)
    -4
    >>> X//Y (Floor/truncate division - In this it removes digits after decimal and reviews the result)
    0
    >>> X*Y (Multiplication)
    32
    >>> X%Y (Modulus - In this division remainder is displayed as a result)
    4
    >>> X/Y (division)
    0.5
    >>> X**Y (exponential)
    65536
    

    Assignment Operators :

    OperatorDescription
    =It assigns the the value of the right expression to the left operand
    Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    
    >>> x=5
    >>> x
    5
    >>> x=66.5
    >>> print(x)
    66.5
    

    OperatorDescription
    +=It increases the value of the left operand by the value of the right operand and assign the modified value back to left operand
    Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    
    >>> a=25
    >>> b=25
    >>> a+=b
    >>> print(a)
    50
    >>> a=a+b
    >>> print(a)
    75
    

    OperatorDescription
    -=It decreases the value of the left operand by the value of the right operand and assign the modified value back to left operand
    Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    
    >>> a=20
    >>> b=10
    >>> a-=b
    >>> print(a)
    10
    >>> a=a-b
    >>> print(a)
    0
    

    OperatorDescription
    *=It multiplies the value of the left operand by the value of the right operand and assign the modified value back to left operand.
    Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> a=10
    >>> b=20
    >>> a*=b
    >>> print(a)
    200
    >>> a=a*b
    >>> print(a)
    4000
    

    OperatorDescription
    %=It divides the value of the left operand by the value of the right operand and assign the reminder back to left operand.
    Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> a=20
    >>> b=10
    >>> a%=b
    >>> print(a)
    0
    >>> a=a%b
    >>> print(a)
    0
    

    OperatorDescription
    **=a**=b will be equal to a=a**b, for example, if a = 4, b =2, a**=b will assign 4**2 = 16 to a.
    Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> a=4
    >>> b=2
    >>> a**=b
    >>> print(a)
    16
    >>> a=a**b
    >>> print(a)
    256
    

    OperatorDescription
    //=A//=b will be equal to a = a// b, for example, if a = 4, b = 3, a//=b will assign 4//3 = 1 to a.
    Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> a=4
    >>> b=3
    >>> a//=b
    >>> print(a)
    1
    >>> a=a//b
    >>> print(a)
    0
    

    RELATIONAL/COMPARISONAL OPERATORS :

    Comparison operators are used to comparing the value of the two operands and returns boolean true or false accordingly.

    OperatorDescription
    ==If the value of two operands is equal, then the condition becomes true.
    !=If the value of two operands is not equal then the condition becomes true.
    <=If the first operand is less than or equal to the second operand, then the condition becomes true.
    >=If the first operand is greater than or equal to the second operand, then the condition becomes true.
    <>If the value of two operands is not equal, then the condition becomes true.
    >If the first operand is greater than the second operand, then the condition becomes true.
    <If the first operand is less than the second operand, then the condition becomes true.
    Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    
    (Equal condition example):
    >>> a=5
    >>> b=5
    >>> a==b
    True
    >>> a=6
    >>> b=7
    >>> a==b
    False
    
    (Not equal condition):
    >>> a=2
    >>> b=3
    >>> a!=b
    True
    
    (Less than or equals to):
    >>> a=2
    >>> b=4
    >>> a<=b
    True
    >>> a=5
    >>> b=5
    >>> a<=b
    True
    >>> a=8
    >>> b=2
    >>> a<=b
    False
    
    (Greater than or equals to):
    >>> a=8
    >>> b=9
    >>> a>=b
    False
    >>> a=78
    >>> b=58
    >>> a>=b
    True
    >>> a=88
    >>> b=88
    >>> a>=b
    True
    
    (Greater than condition):
    >>> a=28
    >>> b=11
    >>> a>b
    True
    
    (Less than condition):
    >>> a=5
    >>> b=6
    >>> a<b
    True
    

    LOGICAL OPERATORS :

    These operators are used to combine the conditional statements.

    OperatorDescription
    andIf both the expression are true, then the condition will be true. If a and b are the two expressions, a → true, b → true => a and b → true.
    orIf one of the expressions is true, then the condition will be true. If a and b are the two expressions, a → true, b → false => a or b → true.
    notIf an expression a is true then not (a) will be false and vice versa.

    Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 5>6 and 4==4 ('and' condition)
    False
    >>> 5<6 and 5==5
    True
    >>> 2==2 or 5>6 ('or' condition)
    True
    >>> 1.5>1 or 2==2
    True
    >>> 5>9 or 2==3
    False
    >>> not 2==2 ('not' condition)
    False
    >>> not(2>1)
    False
    
    
    
    

    IDENTITY OPERATORS :

    Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location.

    OperatorDescription
    is Returns true if both variables are the same object.
    is notReturns true if both variables are not the same object.

    Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    
    >>> x={1,2,3,4}
    >>> y={1,2,3,4}
    >>> z = x
    >>> print(x is y)
    False
    >>> print(z is x)
    True
    >>> print(x is z)
    True
    >>> print(x is not z)
    False
    >>> print(x is not y)
    True
    >>> print(z is not x)
    False
    

    MEMBERSHIP OPERATORS :

    Membership operators are used to test the membership of value inside a data structure. If the value is present in the data structure, then the resulting value is true otherwise it returns false.


    OperatorDescription
    inIt is evaluated to be true if the first operand is found in the second operand.
    not inIt is evaluated to be true if the first operand is not found in the second operand.
    Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> X={"apple","cherry","kiwi"}
    >>> print("banana" in X)
    False
    >>> print("kiwi"in X)
    True
    >>> print("dragon fruit"not in X)
    True
    >>> print("orange"not in X)
    True
    >>> print("orange"not in X)
    

    BITWISE OPERATORS :

    Bitwise operators are used to compare binary numbers

    OperatorNameDescription
    & ANDSets each bit to 1 if both bits are 1
    ORSets each bit to 1 if one of two bits is 1
    ^XORSets each bit to 1 if only one of two bits is 1
    ~ NOTInverts all the bits
    <<Zero fill left shiftShift left by pushing zeros in from the right and let the leftmost bits fall off
    >>Signed right shiftShift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off

    OPERATOR PRECEDENCE :

    The following table lists all operators from highest precedence to lowest.


    OperatorDescription
    **The exponent operator is given priority over all the others used in the expression.
    ~,-,+ The negation, unary plus and minus.
    * / % // The multiplication, divide, modules, reminder, and floor division.
    + - Binary plus and minus
    >> << Left shift and right shift
    &Binary and.
    ^
    <=, <, >, >=Comparison operators (less then, less then equal to, greater then, greater then equal to).
    <>, ==, !=Equality operators.
    =, %=, /=, //=, -=, +=,*=,**=Assignment operators
    is is notIdentity operators
    in not inMembership operators
    not or andLogical operators


    RegEx SETS

    Python - RegEx SETS

    posted on 2019-11-12 23:06:24 - Python Tutorials


    RegEx_Functions

    Python - RegEx_Functions

    posted on 2019-11-09 06:07:29 - Python Tutorials


    RegEx_Sets

    Python - RegEx_Sets

    posted on 2019-11-09 05:30:54 - Python Tutorials


    Prompt Examples

    ChatGPT Prompt Examples

    posted on 2023-06-21 22:37:19 - ChatGPT Tutorials


    Use Cases

    Chat GPT Key Use Cases

    posted on 2023-06-21 21:03:17 - ChatGPT Tutorials


    Prompt Frameworks

    Prompt Frameworks

    posted on 2023-06-21 19:33:06 - ChatGPT Tutorials