What does these operators mean (** , ^ , %, //) ?

PythonServer Side ProgrammingProgramming

In Python ** is a raised to operator. It returns x raised to y in the expression x**y

>>> x=5
>>> y=3
>>> x**y
125

^ is a bitwise XOR operator. Taking two bits as operands it returns 1 if one is 1 and other is 0

>>> a=10
>>> bin(a)    #0001 1010
'0b1010'
>>> b=20
>>> bin(b)    #0010 0100
'0b10100'
>>> c=a^b
>>> c  
30
>>> bin(c)    #0011 1110
'0b11110'

// is defined as floor division operator. It returns integer part of result of division operation

>>> 10/3
3.3333333333333335
>>> 10//3
3

For negative division, floor rounds towards negative infinity.

>>> -10/3
-3.3333333333333335
>>> -10//3
-4

% symbol is defined as modulo operator and returns remainder of division operation.

>>> 10%3
1
>>> 10%2
0

In case of negative division difference bewen upper multiple and numerator is calculated

>>> -10%3
2
>>> -5%2
1
>>> 73%9
1
>>> -73%9
8
raja
Published on 13-Feb-2018 10:26:40
Advertisements