Advanced Operators in Python

Introduction

 
In this article, I will explain advanced operators in Python.
 

Definition

 
In Python, advanced operators use symbols that represent some operations that can be performed on data. They are different types of operators in Python. There are:
  1. Unary operators
  2. Boolean operators
  3. Identity operators
  4. Membership operator
  5. Bitwise operator

Unary operators

  1. + Increment operator
  2. - Decrement operator
These operators use only one operand.so it is called the unary operator. We use keywords (+,-) to increase and decrease the value.
 
operators
operations
++x
Pre-Increment
--x
Pre-Decrement
x++
Post Increment
x--
Post Decrement
 
Syntax
 
Variable Unary operators
 
Example
  1. #Unary operators     
  2. a=10    
  3. print(a)    
  4. a+=2#Increment operator    
  5. print(a)    
  6. a-=5#Decrement operator    
  7. print(a)    
Output
 
 

Boolean operators

 
In Python, to find a given statement is true (or) false to this keyword and, or, not are Boolean operators.
 
There are two types of Boolean operators:
  1. True
  2. False
Syntax
 
Variable 1 any operators variable 2
 
Example
  1. #Boolean operators    
  2. print (True and True)    
  3. print (True and False)    
  4. print (False and True)    
  5. print (False and False)   
Output
 
 

Identity operators

 
In Python, identity operators are used to compare and see if two objects are the same object (or) a different object with the same memory. The using keywords (is, is not) are used to compare two objects.
 
operators
operations
is
Same object
is not
Not object is different
 
Syntax
 
Variable 1 identity operators variable 2
 
Example
  1. #Identity operators    
  2. x = ["red""black"]    
  3. y = ["red""blue"]    
  4. z = x    
  5.     
  6. print(x is z)    
  7. # z is the same object as x    
  8. print(x is not y)    
  9. #x is not the same object as y   
Output
 
 

Membership operator

 
In Python, membership operators are used to give a statement that is present in the object (or) not. We use the keywords (in, in not) to verify the statement.
 
operators
operations
in
Value is present in the object
in not
Value is not present in the object
 
Syntax
 
variable 1 membership operator variable 2
 
Example
  1. #Membership operator    
  2. x = ["red""blue"]    
  3. print("red" in x)    
  4. print("green" not in x)    
  5. #The value "red" is in the list    
  6. #The value "green" is not in the list  
Output
 
 

Bitwise operator

 
In Python, bitwise operators are used to compare binary numbers.
 
operators
operations
&
AND
|
OR

^

XOR
~
NOT
<<
Zero fill left shift
>>
Signed right shift
 
Syntax
 
Variable 1 membership operator variable 2
 
Example
  1. #bitwise operator    
  2. a = 10    
  3. b = -10    
  4. #bitwise AND operation        
  5. print("a & b =", a & b)          
  6. #bitwise OR operation      
  7. print("a | b =", a | b)        
  8. #bitwise NOT operation       
  9. print("~a =", ~a)        
  10. #bitwise XOR operation       
  11. print("a ^ b =", a ^ b)      
  12. #bitwise right shift operator     
  13. print("a >> 1 =", a >> 1)     
  14. print("b >> 1 =", b >> 1)     
  15. #bitwise left shift operator     
  16. print("a << 1 =", a << 1)     
  17. print("b << 1 =", b << 1)   
Output
 
 

Conclusion

 
In this article, we have seen advanced operators in Python. I hope this article was useful to you. Thanks for reading!


Recommended Free Ebook
Similar Articles