Python Operator

Python operator is used to performing different types of operations (arithmetic, logical, or types of computation) on variables or values. The variables or values are called operands.

For example

`x=10y=20add=x+yHere + is operator which carried arithmetic operation and x and y are called operands.`

Python supports the following types of operators

1. Arithmetic operators
2. Logical (Boolean) operators
3. Comparison(relational) operators
4. Bitwise operators
5. Assignment operators
6. Special operators
Arithmetic operators

The arithmetic operator performs basic mathematical operations like addition, subtraction, multiplication, division, and others.

 Operators Description Example Addition(+) The addition operator add either side of operands x+y Subtraction(-) Subtract right operand from left operand x-y Multiplication(*) Multiply either side of the operands x*y Divison(/) Divide left operand from right operand x/y Modulus(%) divide left operand from the right and return the remainder x%y Exponent(**) Perform exponent(power) operation on left operand with power raised by right operand x**y

Example

`x=20y=9add=x+yprint("Addition",add)sub=(x-y)print("Subtraction",sub)mul=xyprint("Multiplication",mul)div=(x-y)print("Division",div)pow=(x*y)print("Exponent",pow)floor=(x//y)print("FloorDiv",floor)OutputAddition 29 Subtraction 11 Multiplication 180 Division 11 Exponent 512000000000 FloorDiv 2 `
Logical Operator

The logical operator is used for performing logical AND, OR, and NOT operations.

 Operator Description Example AND Truth;- If both operands are TRUE x and y OR True:- If either of the operands is True x or y NOT True:- If the operand is False not x

Example

`x=Truey=Falseresult_and=x and yprint("AND",result_and)result_or= x or yprint("OR",result_or)result_not=not xprint("NOT",result_not)OutputAND FalseOR True NOT False `
Comparison(relational) operators

These types of operators are uses to comparing either side of the operand values. It returns output True or False.

 Operator Description Example Equal (==) if the value of both operands is equal then the condition becomes True else False x==y Not Equal (!=) if the value of both operands is not equal then the condition becomes True else False x!=y Greater than (>) If the left operand is greater than the right operand then the condition become True x>y Less than (<) If the left operand is less than the right operand then the condition become True x=) If the left operand is greater than and equal to the right operand then the condition become True x>=y Greater than equal to (<=) If the left operand is less than and equal to the right operand then the condition become True x<=y

Example

`equal=(x==y)notequal=(x!=y)greater=(x>y)less=(x=y)less_equal=(x<=y)print("Equal to", equal)print("Not equal to", notequal)print("Greater than", greater)print("Less than", less)print("Less than equal to", greater_equal)print("Less than equal to", less_equal)OutputEqual to False Not equal to True Greater than True Less than False Less than equal to True Less than equal to False `
Bitwise operator

Bitwise operator performs operations bit by bit, First, it converts integer value into binary format and then performs a logical operation on each bit of binary value.

 Operator Description Example & Bitwise AND x&y | Bitwise OR x|y ~ Bitwise NOR x~y ^ Bitwise XOR x^y << Bitwise left shift x<> Bitwise right shift x>>y

Example

`x=20y=15bitand=x&yprint("bitwise AND", bitand)bitor=x|yprint("bitwise OR", bitor)bitnor=~xprint("bitwise NOT", bitnor)bitxor=x^yprint("bitwise XOR", bitxor)bitleft=x<>yprint("bitwise Right", bitright)Outputbitwise AND 4 bitwise OR 31 bitwise NOT -21 bitwise XOR 27 bitwise Left 655360 bitwise Right 0 `
Assignment operator

Assignment operators are used in assigning a value to the variable but it can be in different ways.

 Operator Description Equivalent = Assing value to right side operand to left size operand x=10 += It calculates addition first and then assigns value to the left operand x=x+10 -= It calculates subtraction first and then assigns value to the left operand x=x-10 *= It calculates multiplication first and then assigns value to the left operand x=x*10 /= It calculates division first and then assigns value to the left operand x=x/10 %= It takes modules first and thn assigns value to left operand x=x%10 **= It calculates exponent first and then assigns value to the left operand x=x**10 //= It calculates floor division first and then assigns value to the left operand x=x//10
Special operators

There are also different types of operator called Membership operator and Identity operator.

 Operator Description Example in(Membership operator) Return True if element found in the specified sequence 10 in x not in(Membership operator) Return True if element not found in the specified sequence 10 not in x is(Identity operator) Return True if operands are identical x is True is not(identity operator) Return True if operand are not identical x is not True