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

DECISION MAKING STATEMENTS

DECISION MAKING STATEMENTS During the execution of the program, when there is a specific application to check the condition and make a proper decision, Decision making statements are used to anticipate specific actions which has to be taken depending upon the condition. Decision making statements are evaluated using TRUE and FALSE values. For example, if a particular condition is TRUE what outcome is expected and when, FALSE what outcome is expected. Depending on the application of program, set of statements are written into the code to perform a particular task. Python provides following types of decision making statements: Ø If statement: It is normal If statement which has condition in terms of Boolean expression, comparison operators, etc. Ø If..else statement: In this case, an If is followed by an else statement. It means ‘IF’ condition turns to be FALSE than ‘ELSE’ will be executed automatically Ø Ladder If.. else if .. else if: This is an extension o...

INPUT FROM USER AND STORE IT IN VARIABLE

INPUT FROM USER AND STORE IT IN VARIABLE We have discussed in the earlier videos and blogs regarding variables and multiple assignments. Now let’s move on with taking real time inputs from user and storing it in a variable. Name = input(“Enter the Name : ”) This function is used to ask real time data from users and store it in the variable defined as Name. This function takes the value in string. This means when you give input as a numeric value, it also considers it as a string input and you can access it as I have explained in the video. To consider this string as numeric value or integer, we need to type cast the taken input as shown. Age = int(input(“Enter your age :”)) This function will type cast your string into an integer value. Type Casting:   In simple words, it means conversion of data type. To make use of programming approach in day to day life, conversion of data type is very useful. As explained earlier input() function considers the value in stri...