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)
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
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 "
Example : To illustrate concept of Identity Operator
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)
Example : To illustrate concept of Membership Operator
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