Operators in python



Operator is used to perform some operation.In python programming there are nine operators available.



Types of operators



  1. Arithemetic operator
  2. Relational operator
  3. Assignment operator
  4. Logical operator
  5. Membership operator
  6. Identity operator
  7. Bitwise operator







1. Arithemetic operator



Arithmetic operator is used to perform Arithmatic operations like Addition,Subtraction, Multiplication, Division, Modulo operations.




Operator Example Explain
+ x+y Add the number
- x-y Subtract the number
* x*y multiplying the number
/ x/y dividing the number.It's return float value.
// x//y dividing the number.It's return int value.
% x%y remainder of the number
** x**y x**y means x power y.



Example-1
x = 10
y = 5

print("Addition:",x+y)


Output



Addition: 15







2. Relational operator



>, <, >=, <=, !=, == are the operations that are avilable in Relational operator. It is used to compare the values.




operator Description Example
== If the leftside value and the rightside value is equal,then it becomes true. a==b
!= If the leftside value and the rightside value is not equal,then it becomes true. a!=b
< If the leftside value is less than of rightside value,then it becomes true. a<b
> If the rightside value is greater than of leftside value,then it becomes true. a>b
<= If the leftside value is less than or equal to rightside value,then it becomes true. a<=b
>= If the rightside value is greater than or equal to leftside value,then it becomes true. a>=b



Example-1
x = 5
y = 3

print(x>y)


Output



True







3. Assignment operator



"=" is a symbol of assignment operator. It will assign the values to the variable.




Operator Example How assign
+= x+=10 x=x+10
-= x-=10 x=x-10
*= x*=10 x=x*10
/= x/=10 x=x/10
%= x%=10 x=x%10
**= x**=10 x=x**10



Example-1
x = 5
y = 4

x+= y

print(x) 


Output



9







4. Logical operator



It used to perform the logical operation between two or more condition.



Operator Example Explain
and (condition1) and (condition2) and operator will become true , when all the conditions are true.
or (condition1) or (condition2) or operator will become true , when any one condition is true.
not not (condition) not operator perform the opposite of the condition.



AND
x = 5
y = 10

print((x == 5) and (y == 10))


Output



true





OR
x = 5
y = 10

print((x == 5) or (y == 5))


Output



true





NOT
x = 5

print(not(x == 10) )


Output



true







5. Membership operator




  1. in operator
  2. not in operator



1. in operator



"in" operator is used to check the value is present in the string,list,tuple,etc or not.




Example-1
n = [ 1, 2, 3, 4, 5]

if(3 in n):
    print("Yes")

else:
    print("No")


Output



Yes





Example-2
n = "python"

if("p" in n):
    print("Yes")

else:
    print("No")


Output



Yes







2. not in operator



"not in" operator is used to check the value is not present in the string,list,tuple,etc or not.




Example-1
n = [ 1, 2, 3, 4, 5]

if(7 not in n):
    print("Yes")

else:
    print("No")


Output



Yes







Example-2
n = "python"

if("s" not in n):
    print("Yes")

else:
    print("No")


Output



Yes







6. Identity operator



  1. is operator
  2. is not operator



1. is operator



"is" operator return "True" if both the values are equal otherwise it will return "False".




Example
a = 10
b = 10

if a is b:
    print("Equal")

else:
    print("Not Equal")


Output



Equal







2. is not operator



"is not" operator return "True" if both the values are not equal otherwise it will return "False".




Example
a = 5
b = 10

if a is not b:
    print("Not Equal")

else:
    print("Equal")


Output



Not Equal







7. Bitwise operator



Bitwise operator used to perform the Logical operation and shift operation in a binary code.




Operator Example Explain
& x&y To perform the AND operation of x,y binary code.
| x|y To perform the OR operation of x,y binary code.
^ x^y To perform the XOR operation of x,y binary code.
<< x<<y Left shift operator to perform the add the y number of 0's in x binary code in last.
>> x>>y Right shift operator to perform the remove the y number of binary code in last of x binary code.



AND(&) operator




Example
x = 5           # x = 0 1 0 1
y = 6           # y = 0 1 1 0

z = x&y         # z = 0 1 0 0

print(z)


Output




4







OR(|) operator




Example
x = 5             # x = 0 1 0 1
y = 6             # y = 0 1 1 0

z = x|y           # z = 0 1 1 1

print(z)


Output



7







XOR(^) operator




Example
x = 5             # x = 0 1 0 1
y = 6             # y = 0 1 1 0

z = x^y           # z = 0 1 0 0

print(z)


Output



3







Leftshift(<<) operator




Example
x = 5                   # x = 0 1 0 1

y = x<<2                # y = 0 1 0 1 0 0

print(y)


Output



20







Rightshift(>>) operator




Example
x = 5                   # x = 0 1 0 1

y = x>>2                # y = 0 1 

print(y)


Output



1