6 Dec 2020

Operator precedence

           When an expression contains more than one operator, the order of evaluation depends on the order of operations.

        

  The acronym PEMDAS (Parentheses, Exponentiation, Multiplication, Division, Addition, Subtraction) is a useful way to remember the rules:

            * Parentheses have the highest precedence and can be used to force an expression to evaluate in the order you want. Since expressions in parentheses are evaluated first, 2 * (3-1)is 4, and (1+1)**(5-2) is 8.You can also use parentheses to make an expression easier to read, as in (minute* 100) / 60, even if it doesn’t change the result.
            * Exponentiation has the next highest precedence, so 1 + 2**3 is 9, not 27, and 2 *3**2 is 18, not 36.
            * Multiplication and Division have higher precedence than Addition and Subtraction. So 2*3-1 is 5, not 4, and 6+4/2 is 8, not 5.
            *Operators with the same precedence are evaluated from left to right (except exponentiation).

Boolean values


                                           Boolean data type have two values. They are 0 and 1. 0 represents False, 1 represents True. True and False are keyword . Boolean expressions is an expression that is either true or false. It use the operator == to compare two operands which produces true if the both operands are equal ,false  otherwise.

Program:

>>> 3==5
 False
>>> 6==6 
True
>>> True+True
2
>>> False+True
1
>>> False*True
0

5 Dec 2020

Identity Operators

            They are used to check if two values (or variables) are located on the same part of the memory.

                         


Example:
x = 5 
y = 5 
print(x is not y)
print(x is y)

Output:
false
true


Membership Operators

                            Evaluates to find a value or a variable is in the specified sequence of string, list, tuple, dictionary or not.To check particular item in list or not, in and not in operators are used.
 
                         

Example :
x=[5,3,6,4,1]
>>> 5 in x True
>>> 5 not in x False

Bitwise operators

               A bitwise operation operates on one or more bit patterns at the level of individual bits.

                    

Example :
a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c = 0
c = a & b; # 12 = 0000 1100
print ("Line 1 - Value of c is ", c)
c = a | b; # 61 = 0011 1101
print ("Line 2 - Value of c is ", c) 
c = a ^ b;  # 49 = 0011 0001
print ("Line 3 - Value of c is ", c)
c = ~a; # -61 = 1100 0011
print ("Line 4 - Value of c is ", c)
c = a << 2; # 240 = 1111 0000
print ("Line 5 - Value of c is ", c)
c = a >> 2; # 15 = 0000 1111
print ("Line 6 - Value of c is ", c)

Output:
Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4- Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15


Logical Operators

           Logical operators are the and, or, not operators.

             


 Example :   

a = True
b = False
print('a and b is',a and b)
print('a or b is',a or b)
print('not a is',not a)

Output:
x and y is false
x or y is true
not x is false

Assignment Operators

                  Assignment operators are used in Python to assign values to variables.

                       

Example :
a = 21
b = 10
c = a + b
print("Line 1 - Value of c is ", c)c += a
print("Line 2 - Value of c is ", c)c *= a
print("Line 3 - Value of c is ", c)c /= a
print("Line 4 - Value of c is ", c)c = 2 c %= a
print("Line 5 - Value of c is ", c) c **= a
print("Line 6 - Value of c is ", c) c //= a
print("Line 7 - Value of c is ", c)

output:
Line 1 - Value of c is 31
Line 2 - Value of c is 52
Line 3 - Value of c is 1092
Line 4 - Value of c is 52.0
Line 5 - Value of c is 2
Line 6 - Value of c is 2097152 
Line 7 - Value of c is 99864

Operator precedence

           When an expression contains more than one operator, the order of evaluation depends on the order of operations.            The ac...