# Python Operator

Operators in a programming language is a special symbol for a specific action or operation. For example, if we want to perform an addition on two numbers we can use plus (+) sign or symbol. 1 + 2 equals to three. Similarly, Python supports multiple operators for different operations. Following are the type of operators in Python Programming Language.

## Types of Operator

Arithmetic Operators (Ex: +,-,*,/,%,**,//)
Relational or Comparison operators (Ex: ==,!=,>,>=,<,<=)
Assignment Operators (Ex: =,+=,-=,*=,/=,%=,**=,//=)
Logical Operators (Ex: and,or,Not)
Membership Operators (Ex: in,not in)
Identity Operators (Ex: is,is not)
Bitwise Operators (Ex: &,|,^,~,<<,>>)

## Python Arithmetic Operators

For the demonstration of arithmetic operators, we assumed that we have two variables a and b with values 100 and 10.
Following are the example for arithmetic operators.

Operator Description Example Result
+ Addition Add values on either side of the operator. 100+10 110

- Subtraction

Subtracts right-hand operand from the left-hand operand. The result may be positive or negative based on left and right-hand operand.

100-10

10-100

90

-90

* Multiplication

Multiplies values on either side of the operator 100*10 1000

/ Division

Divides left-hand operand by right-hand operand. Right-hand operand must be greater than 0.

100/10

100/3

0

33.333333333333336

% Modulus

Divides left-hand operand by right-hand operand and returns the remainder. Right-hand operand must be greater than 0.

100%10

100%3

0

1

** Exponent

Performs exponential (power) calculation on operators. a**b It means a to the power b. 2**5 32

// Floor Division

The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity) 100//3
-100//3
100//-3
33
-34
-34

## Python Comparison Operators

Python Comparison Operators or Relational compare the values on either side of them and decide the relation among them.

Following are the example of the comparison or relational operators. For all the comparison we are going to use 100 and 10.

 Operator Description Example Result == Equals to If the values of two operands are equal, then the result becomes true 100==10 false != Not Equals to If the values of two operands are not equal, then the result becomes true. 100!=10 true > Greater Than If the value of left operand is greater than the value of right operand, then result becomes true. 100>10 true < Less Than If the value of left operand is less than the value of right operand, then result becomes true. 100<10 false >= Greater Than or Equals to If the value of left operand is greater than or equals to the value of right operand, then result becomes true. 100>=10 true <= Less Than or Equal to If the value of left operand is less than or equal to the value of right operand, then result becomes true. 100<=10 false

## Python Assignment Operators

It is used to assign values of operands to variables.
Following are the example for Assignment Operators. For all the examples we assigning the final result inside "result" variable.

= Assigns values from right side operands to left side operand result = 10 10 Operator Description Example Result += Add AND It adds right operand to the left operand and assign the result to left operand result+=10. It is equivalent to result=result+10 20 -= Subtract AND It subtracts right operand from the left operand and assign the result to left operand result-=10. It is equivalent to result=result-10 10 *= Multiply AND It multiplies right operand with the left operand and assign the result to left operand result*=10. It is equivalent to result=result*10 100 /= Divide AND It divides left operand with the right operand and assign the result to left operand result/=10. It is equivalent to result=result/10 10 %= Modulus AND It takes modulus using two operands and assign the result to left operand result%=10. It is equivalent to result=result%10 0 **= Exponent AND Performs exponential (power) calculation on operators and assign value to the left operand result=10 (Reseting result variable value) result**=10. It is equivalent to result=result**10 10000000000 //= Floor Division AND It performs floor division on operators and assign value to the left operand result//=10. It is equivalent to result=result//10 1000000000

## Python Logical Operators

Following are the example for Logical operators

 Operator Description Example Result and Logical AND If all operands combined with and are true then result becomes true. True and True False and True True and True and Ture True and False and True True False True False or Logical OR If anyone operands combined with or are true then result becomes true. True or True True or False False or False True True False not Logical NOT Used to reverse the logical state of its operand. not(True or False) False

## Python Membership Operators

Membership operators in python are used to check the existence of values inside List, Sequence, String, or Tuples. We have two membership operators in and not in.
"in" is used to check the existence. "not in" used to check nonexistence.
Following are the example for in
"Hello" in "Hello, vLemonn"
1 in [1,2,3,4]

Following are the example for not in
"World" in "Hello, vLemonn"
5 in [1,2,3,4]

## Python Identity Operators

In Python are used to determine whether a value is of a certain class or type. They are usually used to determine the type of data a certain variable contains.

is:valuates to true if the variables on either side of the operator point to the same object and false otherwise.
type(5) is int

is not: Evaluates to false if the variables on either side of the operator point to the same object and true otherwise.
type(5) is float.

## Python Bitwise Operators

Bitwise operator works on bits and performs the bit-by-bit operation. Assume if a = 10; and b = 12; Now in binary format they will be as follows −

a = 1010
b = 1100
a&b 1000

 a b & 1 1 1 0 1 0 1 0 0 0 0 0

a|b 1110

 a b & 1 1 1 0 1 1 1 0 1 0 0 0

a << y
Returns a with the bits shifted to the left by y places (and new bits on the right-hand-side are zeros). This is the same as multiplying a by 2**y.
a >> y
Returns a with the bits shifted to the right by y places. This is the same as //'ing a by 2**y.
~ a
Returns the complement of a - the number you get by switching each 1 for a 0 and each 0 for a 1. This is the same as -a - 1.
a ^ b
Does a "bitwise exclusive or". It copies the bit, if it is set in one operand but not both.

## Python Operators Precedence

Following table lists all operators from highest precedence to the lowest.

1. ** Exponentiation (raise to the power)
2. ~ + – complement, unary plus and minus (method names for the last two are [email protected] and [email protected])
3. * / % // Multiply, divide, modulo and floor division
4. + – Addition and subtraction
5. >> << Right and left bitwise shift
6. Bitwise 'AND'
7. ^ | Bitwise exclusive `OR' and regular `OR'
8. <= < > >= Comparison operators
9. <> == != Equality operators
10. = %= /= //= -= += *= **= Assignment operators
11. is is not Identity operators
12. in not in Membership operators
13. not or and Logical operators

Example: What is the output for this 10+20*5/2+6

##### Leave A Comment

Please login to post your valuable comments.