Python operators

In this tutorial. we will see about Python operators.


Python operators are nothing but special symbols which performs arithimatic or logical or assignment operations,

Let’s go through all Python operators one by one.


Arithmetic operators

x = 15 y = 10 # + operator is used to add two operands #Output: x + y = 25 print('x + y =',x+y) # - operator is used to substract two operands # Output: x - y = 5 print('x - y =',x-y) # * operator is used to multiply two operands print('x * y =',x*y) # / operator is used to divide two operands # Output: x / y = 1. print('x / y =',x/y) # // operator is used to floor divide two operands # Output: x // y = 1 print('x // y =',x//y) # ** operator is used to find power of two operands # Output: x ** y = 576650390625 print('x ** y =',x**y)

Output of above program:

x + y = 25
x – y = 5
x * y = 150
x / y = 1.5
x // y = 1
x ** y = 576650390625

Comparison operators in Python

Comparison operators is used to compare two values. It either returns True or False.

Output of above program:

x > y is False
x < y is True
x == y is False
x != y is True
x >= y is False
x <= y is True

Logical operators in Python

Logical operators are and, or and not.

x=True y=False z= x and y print("X and Y: ",z) z= x or y print("X or Y: ",z) z= not x print("X not Y: ",z)

Output of above program:

X and Y: False
X or Y: True
X not Y: False

Bitwise operators

Bitwise operators works on bits of operands.It works on bitwise presentation of bits.

x & y is 0
x | y is 6
~x is -3
x ^ y is 6
x >> y is 0
x << y is 32

Assignment operators

Assignment operators are used to assign values to variables.
For example You can assign add and assign the value to same variable using x=x+5.

Operator Example Assignment
= x = 5 x = 5
+= x += 5 x = x + 5
-= x -= 5 x = x – 5
*= x *= 5 x = x * 5
/= x /= 5 x = x / 5
%= x %= 5 x = x % 5
//= x //= 5 x = x // 5
**= x **= 5 x = x ** 5
&= x &= 5 x = x & 5
|= x |= 5 x = x | 5
^= x ^= 5 x = x ^ 5
>>= x >>= 5 x = x >> 5
<<= x <<= 5 x = x << 5


Python specific operators

There are some python specific operators which use only in python.


Identity opetators

is and is not both are identity operators and are used to check if both operators point to same memory location.

s1='code2master' s2='code2master' print('If s1 is s2:',s1 is s2) x1=2 x2=2 print('If x1 is not x2:',x1 is not x2) list1=[9,8,7] list2=[9,8,7] print('If list1 is list2:',list1 is list2)

Output:

If s1 is s2: True
If x1 is not x2: False
If list1 is list2: False

Memebership opetators

in and not in operators are used to check if a value is present in the sequence or not. It is often is used to check value in list, dict or tuple.

list1=[9,8,7] print('If 9 is present in list1: ',9 in list1) str1='code2master' str2='code' print('If code is present in code2master: ',str2 in str1) str1='code2master' str2='java' print('If java is not present in code2master: ',str2 not in str1)

Output:

If 9 is present in list1: True
If code is present in code2master: True
If java is not present in code2master: True

That’s all about Python operators.

Leave a Reply

Your email address will not be published. Required fields are marked *