Skip to main content

Python Operators Intro - Part 2

Operators are used to do action or process on variables and operands. The operator usually represents  as symbol for doing an action. Python has different type of operators like Arithmetic, Assignment, Relational / Comparison , Logical, Bitwise, Identity and Membership operators. Every operators are using  to do different kind of operations.

In this tutorial we will learn about  Logical Operators,  Bitwise Operators, Identity Operators, Membership Operators and Ternary operator with an exmple and an output. We will learn one by one.

Python Type of Operators

Python operators were classifying the  following  categories. We  learned  Arithmetic, Assignment, Relational / Comparison Oprators on previously.

  1. Arithmetic Operators
  2. Relational Operators
  3. Assignment Operators
  4. Logical Operators
  5. Bitwise Operators
  6. Identity Operators
  7. Membership Operators

We  will see the  following on this tutorial

  1. Logical Operators
  2. Bitwise Operators
  3. Identity Operators
  4. Membership Operators

Python Logical Operators

Python  Logical Operators are used to perform the  logical  AND, OR and NOT Operations.

 
Operator Description Syntax Example
and Logical  AND Operation between two conditions  Both conditions should be true cond1 and cond2 a and b
or Logical  OR Operation between two conditions  Any one should be true cond1 or cond2 a or b
not Logical NOT Operation return True Or False  not cond not a

Python Bitwise Operators

Python Bitwise operators are used to perform the bitwise logical AND and OR  operations, They execute the bit by bit opearions within two operands.

 
Operator Description Syntax Example
& Bitwise AND Operator. Bit level AND operation between two values var1 & var2 6 & 2
| Bitwise  OR Operator. Bit level  OR operation between two values var1 | var2 6 | 2
~ Bitwise NOT Operator. Bit level NOT operation  ~ var1 ~2
^ Bitwise XOR Operator. Bit level XOR operation between two values var1 ^ var2 6 ^ 2
>> Bitwise right shift opeator. Shift the n number of bits by move one position towards Right var1 >> n 6 >> 2
<< Bitwise left shift operator. Shift the n on  number of bits  by move one position towards Left var1 << n 6 << 2

Python  Identity Operators

Python Identity Operators are special operators used to check values and memory location of those variables.

 
Operator Description Syntax Example
is Check whether the two values are equals and use the same memory location var1 is var2 a is b
is not Check whether the two values or not equal and not use same memory location var1 is not var2 a is not b

Python  Membership Operators

Python Membership Operators are special operators used to check the sequence or available of values in String, List, Tuples and Set.

 
Operator Description Syntax Example
in Check whether the two values are in the sequence val1 in [val1,val2,val3] a in [a,b,c]
not in Check whether the two values are not in the sequence val4 not in [val1,val2,val3] d not in [a,b,c]