Newby Coder header banner

Python Operators

Python Operators

Operators are used to perform operations on variables and values

The variables or objects on either side of an operator can be referred as operands

Operators are special symbols that carry out computation which may be arithmetic, logical, assignment etc

Operand : it the value that the operator operates on

For eg, in a = 3 + 10 , + is the operator and 3 and 10 are operands


Operators in Python can be divided into :

Python Arithmetic Operators

Arithmetic operators are used with numeric values to perform common mathematical operations:

Assume following variables are declared -

>>>a=10
>>>b=3
>>>c=50
Operator Description Example
+

Addition - Adds values of either side of the operator

>>> a + b
13
-

Subtraction - Subtracts right operand from left operand

>>> a - b
-20
*

Multiplication - Multiplies values of either side of the operator

>>> a * 2.4
24
/

Division - Divides left operand by right operand

>>> c / b
16.666666666666668
%

Modulus - Divides left operand by right operand and returns remainder

>>> c % b
2
**

Exponent - Performs exponential (raise to power) calculation on operators

>>> a ** b
1000
>>> a ** 1.5
31.622776601683793
//

Floor Division - Divides left operand by right and returns the nearest integer whose value is less than or equal to the quotient

a//b is same as floor(a/b) where floor() can be imported from math

>>> a // b
3
>>> -10 // b
-4

Python Assignment Operators

Assignment operators are used to assign value to the left operand by evaluating the expression on the right

Operator Description Example
=

Assigns values from right side operands to left side operand

c = a + b assigns value of a + b into c

>>> x = 4 * 3.4525
13.81
+=

Adds right operand to the left operand and assign the result to left operand

c += a is equivalent to c = c + a

>>> x = 10
>>> x += 5
>>> x
15
-=

Subtracts right operand from the left operand and assign the result to left operand

c -= a is equivalent to c = c - a

>>> x = 10
>>> x -= 4
>>> x
6
*=

Multiplies right operand with the left operand and assign the result to left operand

c *= a is equivalent to c = c * a

>>> x = 10
>>> x *= -3
>>> x
-30
/=

Divides left operand with the right operand and assign the result to left operand

c /= a is equivalent to c = c / a

>>> x = 10
>>> x /= 5
>>> x
2.0
%=

Takes modulus using two operands and assign the result to left operand

c %= a is equivalent to c = c % a

>>> x = 10
>>> x %= 7
>>> x
3
**=

Performs exponential (power) calculation on operators and assign value to the left operand

c **= a is equivalent to c = c ** a

>>> x = 10
>>> x **= 2
>>> x
100
//=

It performs floor division on operators and assigns value to the left operand

c //= a is equivalent to c = c // a

>>> x = 10
>>> x //= 9
>>> x
1
>>> x = 10
>>> x //= -9
>>>
>>> x
-2

Python Comparison Operators

These operators compare the values on either sides of them and decide the relation among them

They are also called Relational operators

Assume following variables are declared -

>>>a=10
>>>b=3
>>>c=50
Operator Description Example
==

Returns true if values of the two operands are equal

>>> a == b
False
>>> a == 10
True
!=

Returns true if values of the operands are not equal

>>>
>>> a != b
True
>

Returns true if the value of left operand is greater than the value of right operand

>>> a > b
True
<

Returns true if the value of left operand is less than the value of right

>>> c < b
False
>>>
>>> b < a
True
>=

Returns true if value of left operand is greater than or equal to the value of right operand

>>> a >= 10
True
<=

Returns true if the value of left operand is less than or equal to the value of right operand

>>> a <= c
True

Python Logical Operators

Logical operators are used to combine conditional statements or expressions :

Assume following variables are declared -

>>>a=10
>>>b=3
>>>c=50
Operator Description Example
and

Returns True if both expressions are true

>>> a > b and a < c
True
or

Returns True if one of the expressions is true

>>> a > 20 or a % 2 == 0
True
not

Returns the boolean opposite of the right operand

Since strings and numbers except 0 are considered as True, not operator returns False for a string or number which is not 0

>>> not(10 > 4 and b <4 )
False
>>> not(a == 15)
True

Python Membership Operators

in and not in are the membership operators in Python

They are used to test whether a value or variable is found in a sequence (string, list, tuple, set and dictionary)

Operator Description Example
in

Evaluates to true if a value/variable is found in the specified sequence

>>> x = range(1, 100)
>>> 55 in x
True
>>> 55.5 in x
False
>>> string = "Some letters"
>>> "me" in string
True
>>> "some" in string
False
not in

Evaluates to true if a value/variable is not found in the specified sequence

>>> x = range(1, 100)
>>> 100 not in x
True
>>> 1 not in x
False
>>> string = "Some letters"
>>> "more" not in string
True
>>> "me" not in string
False

In case these are used for a dictionary object, then presence of key is tested, not the value

Although, presence can be tested in its list of values which can be retrieved by using dictionary.values() method

Python Identity Operators

is and is not are the identity operators in Python

They are used to check if two values (or variables) are located on the same part of the memory

Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object with the same memory location:

Operator Description Example
is

Evaluates to true if the variables on either side of the operator point to the same object

>>> x = "axe"
>>> y = "axe"
>>> x is y
True
>>> a = [3, 10, 25]
>>> b = [3, 10, 25]
>>>
>>> a is b
False
>>> a[1] is b[1]
True
>>> [] is []
False
is not

Evaluates to false if the variables on either side of the operator point to the same object

>>> x = [3, 10, "twenty"]
>>> y = [3, 10, "twenty"]
>>> z = x
>>> y is not z
True
>>> x is not z
False
>>> dict() is not dict()
True

In python, id() function returns the object id of an object and can be used to check if two variables refer the same object

Example

>>> a = "test"
>>> id(a)
140585882204680
>>> id("test")
140585882204680 

Python Bitwise Operators

Bitwise operator works on bits and performs bit by bit operation

Following examples use bin() method to print binary value of an integer

bin(2) returns '0b10', where binary value of 2 is 10 and '0b' in the string denotes that it is binary

Assume following variables are declared -

>>>a=17
>>>b=13
>>> bin(a)
'0b10001'
>>> bin(b)
'0b1101'

Following Bitwise operators are supported by Python

Operator Description Example
&

Binary And: compares corresponding bits of two integers and returns 1 if both are 1 else returns 0

>>> 17 & 13
1
>>> bin(17 & 13)
'0b1'
|

Binary Or: compares corresponding bits of two integers and returns 0 if both are 0 else returns 1

>>> bin(17|13)
'0b11101'
>>> 17 | 13
29
^

Binary XOR: compares corresponding bits of two integers and returns 0 if both are same

>>> bin(17^13)
'0b11100'
>>> 17 ^ 13
28
~ Binary One's Complement: It is unary and returns the one's complement of a number
>>> bin(~17)
'-0b10010'
>>> ~17
-18
<<

Binary Left Shift: The left operand's value is moved left by the number of bits specified by the right operand

>>> bin(13)
'0b1101'
>>> bin(13<<1)
'0b11010'
>>> bin(13<<2)
'0b110100'
>>> bin(13<<3)
'0b1101000'
>>> 13<<2
52
>>

Binary Right Shift: The left operands value is moved right by the number of bits specified by the right operand

'0b10001'
>>> bin(17>>1)
'0b1000'
>>> bin(17>>2)
'0b100'
>>> bin(17>>3)
'0b10'
>>> 17>>1
8

Assume if a = 17; and b = 13; then they can be interpreted in binary format as -

a =       10001
b =       01101
a&b =     00001
a|b =     11101
a^b =     11100
~a  =    -10010 

Python Operators Precedence

The following table lists all operators from highest precedence to lowest

Sr.No Operator Description
1

**

Exponentiation (raise to the power)

2

~

One's complement

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