Logical, Identity , Membership and Unary Operators in Python


                                                
Logical, Identity , Membership and Unary  Operators in Python

1 Logical operator:


Python supports Three Logical Operators i.e. "Logical  AND (&&)","Logical  OR(||) ", "Logical NOT(!)" .As in case of arithmetic expression, the logical operator will execute from left to right .
Logical Operator are the operators which includes Logical operations that Manipulate Boolean Values 

Boolean Values are either True or False .

    
Logical AND (&&):

Logical  AND operator is used to check two conditions at the same time .If both conditions are True then the whole expression becomes true.


Logical OR(||):

Logical OR operator is used to execute two expressions at the same time with a relational operator .If one of the conditions is true then the whole expression becomes True. 



Logical NOT(!): 

The logical NOT operator takes a single value and just reverses that value . It means if the value is non zero then the NOT operator converts it into zero and for zero it will convert it to one .
Example :
x=4,y
y=!x
print(y)

Output _ 0(Zero)

Operator name 

Description

Syntax

AND(&&)

True only when both expression are True

Exp1 and Exp2

OR(||)

True when any one of the expression is true

Exp1 or Exp2

NOT(!)

Reverse the value

not(a)



NOTE

Truth tables for logical and binary operators are the same .


Logical operators operate in an  alternate fashion . It means in Logical and if first expression is false then  compiler will not check for the second one on similar ground in OR operator if the first expression is true then it will not execute the second one .

 EXAMPLE : Program to illustrate concepts of logical operators  
                                                           
Logical, Identity , Membership and Unary  Operators in Python



2 . Python Identity Operator :

 Identity operators are used to compare object and it will return true if the objects are same ,and               false if they are different .Identity operator compare Memory locations of two objects .
There are two Identity operators which Python supports i.e. " is " , " is not "


Operator name 

Description

Example 

is

Returns true if values on both sies of the operators points to the same object.false otherwise.

if a is b 

is not

Return true if values on both sides of the operator points to different object , false otherwise 

if a is not b 


Example : To illustrate concept of Identity Operator 
                                                   
Logical, Identity , Membership and Unary  Operators in Python


Python : Membership Operator:
Python supports two types of Membership operators .in and not in .These  operators, as the name suggest test for membership in a sequence such as string ,lists and sets (data types)


Operator name 

Description 

in

Returns true if the member is present in given sequence ,false otherwise

not in 

Returns true if the member is not present in the given sequence ,false otherwise.


Example : To illustrate concept of  Membership Operator 

                                          
Logical, Identity , Membership and Unary  Operators in Python

  Unary Operator :             

      Unary  operators are operate on a single Operand. Python supports       unary minus operator.
unary minus is totally different than the minus operator in arithmetic operators .When an operand is preceded by a minus sign the unary operator negates the value .In short if the number is positive then 
it gets converted into a negative number after applying this operator and wise versa. 
Example:

x=5
y=-(x)
print(y)

OUTPUT : -5       #Negative Number
      

NOTE

Python does not support postfix and prefix increment and decrement operators .