Skip to main content

COMPARISON, LOGICAL, ASSIGNMENT AND BITWISE OPERATORS




COMPARISON, LOGICAL, ASSIGNMENT AND BITWISE OPERATORS

In this blog we will see about Arithmetic operators and understand how to use it with Python IDLE to perform certain calculations.

PYTHON COMPARISON OPERATORS

OPERATOR
DESCRIPTION
EXAMPLE
       ==
Checks whether LHS is equal to RHS, then the condition becomes true
10==10 is true
       !=
Checks whether LHS is not equal to RHS, then the condition becomes true
2!=3 is true
       >
Checks whether LHS is greater than RHS, then the condition becomes true
6>3 is true
       <
Checks whether LHS is less than RHS, then the condition becomes true
6<10 is true
       >=
Checks whether LHS is greater than or equal to RHS, then the condition becomes true
10>=10 is true
       <=
Checks whether LHS is less  than or equal to RHS, then the condition becomes true
5<=10 is true


   
PYTHON COMPARISON OPERATORS
 
OPERATOR
DESCRIPTION
EXAMPLE
       =
Assigns RHS to LHS
a= 2+3
assigns 5 to a
       +=
It adds the RHS value to LHS value and assigns the final result to LHS operand
c+=a is equivalent to
c = c+a
       -=
It subtracts the RHS value from LHS value and assigns the final result to LHS operand
c-=a is equivalent to
c = c-a
       *=
It multiplies the RHS value with LHS value and assigns the final result to LHS operand
c*=a is equivalent to
c = c*a
       /=
It divides the LHS value with RHS value and assigns the final result to LHS operand
c/=a is equivalent to
c = c/a
       %=
It takes modulus of RHS value from LHS value and assigns the final result to LHS operand
c%=a is equivalent to
c = c%a
       **=
Performs exponential calculations on operators and assigns the final result to LHS operand
c**=a is equivalent to
c = c**a
       //=
It performs floor division on operators and assigns the final result to LHS operand
c//=a is equivalent to
c = c//a



 PYTHON LOGICAL OPERATORS

OPERATOR
DESCRIPTION
EXAMPLE
       and
LOGICAL AND
If both the operands are true, then the condition becomes true
(A and B) is true
       or
LOGICAL OR
If any of the operands are non-zero, then the condition becomes true
(A or B) is true
       not
LOGICAL NOT
It is used to reverse the logical state of operand
not(A or B) is false


PYTHON BITWISE OPERATORS

a = 0000 1111
b = 0000 1010

a&b = 0000 1010
a|b = 0000 1111
a^b = 0000 0101
~a = 1111 0000
a<<2 = 0011 1100
a>>2 = 0000 0011

OPERATOR
DESCRIPTION
       &
BITWISE AND
If both the bits are 1 , then the result is 1
else 0
       |
BITWISE OR
If any of the bits are 1, then the result is 1
else 0
       ~
BITWISE NOT
It is used to reverse the bits from 1 to 0 and vice versa
       ^
BITWISE XOR
If both the bits are different, then the result is 1
else 0
       <<
BITWISE LEFT SHIFT
It shifts the left operand to left by the number of bits specified by right operand
       >>
BITWISE RIGHT SHIFT
It shifts the left operand to right by the number of bits specified by right operand


Comments

Popular posts from this blog

BUILT INS FUNCTIONS IN PYTHON

BUILT INS FUNCTIONS IN PYTHON Every programming languages have certain pre-defined functions. Similarly, for certain tasks python has built in functions which you can access and understand as explained in the video.   dir(__builtins__): This module will return all the built in function and identifiers directly available to the programmer.   help(“function name”): This is the most important function in python. It gives the complete detail regarding the function which you mentioned inside help().

WHILE LOOP IN PYTHON

WHILE LOOP IN PYTHON Loops are used to execute a piece of code or set of statements multiple times in a program as long as the condition is true. While loop is the basic loop structure in Python. Syntax: While (expression):             Statement(s) Here, condition may be any logical expression and statement(s) can be single statement or multiple statements. The loop will iterate or execute until the condition is true. When the condition becomes false, program comes out of the loop and remaining statements are executed. In Python language, during any of the decision making, control statements and looping indentation is very important because, it specifies the set of statements to be a part of single code or block. Since no brackets are used to specify the complete code with in, indentation is used. Flowchart: In While loop, the main important thing is the loop may never be executed if the condition is...