# Python Operators

Python programming is provide various operators to deal with mathematical,logical,comparison operation. so that is very important to how to use those operators in our program. operator is work on specified operands. operands can be a value and object.

``````#1 and 2 is operands
print(1+2) #+ Operator
value1=2
value2=3
#value1 and value2 is operands
print(value1*value2) #* Operator``````
##### Output
``````3
6``````

## Arithmetic Operators

Operator Name Example
- Subtract (minus sign) 4-2=2
* Multiplication (* symbol) 4*2=8
/ Divide 4/2=2
% Remainder operator 4%2=0
// Floor Division 4//3=1
** Exponent 4**2=16

For example

``````a=5
b=8
print("a =",a,"b =",b)
print("a-b",a-b)
print("a*b",a*b)
print("a/b",a/b)
print("a**b",a**b) #a power b
print("a//b",a//b) #a power b
print("a%b",a%b)``````
##### Output
``````a = 5 b = 8
a+b 13
a-b -3
a*b 40
a/b 0.625
a**b 390625
a//b 0
a%b 5``````

## Comparison Operators

Operator Name or Meaning Example
== Equal to equal to (4==2)=False
>= Greater than equal to (4>=2)=True
<= Less than equal to (4<=2)=False
!= Not equal to (4!2)=True
<> Not equal to (4<>2)=True
< Less than (4<2)=False
> Greater than (4>3)=True
``````a=4
b=2
print("a =",a,"b =",b)
print("a==b",a==b)
print("a>=b",a>=b)
print("a<=b",a<=b)
print("a!=b",a!=b)
print("ab",a>b)``````
##### Output
``````a = 4 b = 2
a==b False
a>=b True
a<=b False
a!=b True
a<b False
a>b True
``````

some of python 3 interpreter are display syntax error to work with (<>) operator. see this example.

``````a=4
b=2
print("a =",a,"b =",b)
print(a<>b)``````
``````python3 test.py
File "test.py", line 4
print(a<>b)
^
SyntaxError: invalid syntax``````

But It will work on python 2 version.

``````python2 test.py
('a =', 4, 'b =', 2)
True
``````

## Assignment Operators

Operator Name or Meaning Example
+= Add and assign [a=3,b=4] (a+=b)Same as(a=a+b) then a=6,b=4
-= Subtract and assign [a=3,b=4] (a-=b)Same as(a=a-b) then a=-1,b=4
*= Multiplication and assign [a=3,b=4] (a*=b)Same as(a=a*b) then a=12,b=4
/= Divide and assign [a=3,b=4] (a/=b)Same as(a=a/b) then a=0.75,b=4
%= Modulus and assign [a=5,b=4] (a%=b)Same as(a=a%b) then a=1,b=4
**= Exponent and assign [a=3,b=4] (a**=b)Same as(a=a**b) then a=81,b=4
//= Floor division and assign [a=7,b=4] (a//=b)Same as(a=a/b) then a=1,b=4

For example

``````def resetData():
global a
a=4
global b
b=5

a=4
b=5
print("a =",a,"b=",b)

a+=b
print("a+=b","a =",a," b=",b)
resetData()

a-=b
print("a-=b","a =",a," b=",b)
resetData()

a*=b
print("a*=b","a =",a," b=",b)
resetData()

a/=b
print("a/=b","a =",a," b=",b)
resetData()

a%=b
print("a%=b","a =",a," b=",b)
resetData()

a**=b
print("a**=b","a =",a," b=",b)
resetData()

a//=b
print("a//=b","a =",a," b=",b)
resetData()``````
##### Output
``````a = 4 b= 5
a+=b a = 9  b= 5
a-=b a = -1  b= 5
a*=b a = 20  b= 5
a/=b a = 0.8  b= 5
a%=b a = 4  b= 5
a**=b a = 1024  b= 5
a//=b a = 0  b= 5
``````

## Logical Operators

Operator Name
and Logical AND
or Logical OR
not Logical NOT

For example.

``````print(4 and 5)
print(0 and 1)
print(4 or 5)
print(3 or 5)
print(4 not in [1,3,7])
print(7 not in [1,3,7])``````
##### Output
``````5
0
4
3
True
False``````

## Bitwise Operators

Operator Name
& Binary AND
| Binary OR
~ Binary 1s Complement
^ Binary XOR
>> Binary Right Shift
<< Binary Left Shift

## Other operator

// operator: Normal division of two number the resultant of python interpreter is float result. When we skipping the fraction part then we can use // (double forward slash).

``````result=9/2
print(result)
result=9//2
print(result)``````

#### Output

``````4.5
4``````

** operator: This operator are used to calculate power of numeric value.

``````print(3**4) #3^4 = (3X3X3X3) = 81
print(3.2**3)# 3.2^3 = (3.2*3.2*3.2) = 32.768``````

#### Output

``````81
32.76800000000001``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.