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:
-
Unary operators
-
Boolean operators
-
Identity operators
-
Membership operator
-
Bitwise operator
Unary operators
-
+ Increment operator
-
- 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
-
- a=10
- print(a)
- a+=2
- print(a)
- a-=5
- 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:
-
True
-
False
Syntax
Variable 1 any operators variable 2
Example
-
- print (True and True)
- print (True and False)
- print (False and True)
- 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
-
- x = ["red", "black"]
- y = ["red", "blue"]
- z = x
-
- print(x is z)
-
- print(x is not 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
-
- x = ["red", "blue"]
- print("red" in x)
- print("green" not in x)
-
-
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
-
- a = 10
- b = -10
-
- print("a & b =", a & b)
-
- print("a | b =", a | b)
-
- print("~a =", ~a)
-
- print("a ^ b =", a ^ b)
-
- print("a >> 1 =", a >> 1)
- print("b >> 1 =", b >> 1)
-
- print("a << 1 =", a << 1)
- 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!