Python Basic Operators
• Operators are the constructs which can manipulate the value of
operands.
Types of Operator
Python language supports the following types of operators.
• Arithmetic Operators
• Comparison (Relational) Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators
Python Arithmetic Operators Example
Assume variable a holds 10 and variable b holds 20, then
Operator Description Example
Adds values on either side
+ Addition a + b = 30
of the operator.
Subtracts right hand
- Subtraction operand from left hand a – b = -10
operand.
* Multiplies values on either
a * b = 200
Multiplication side of the operator
Divides left hand operand
/ Division b/a=2
by right hand operand
Divides left hand operand
% Modulus by right hand operand and b%a=0
returns remainder
Performs exponential
** Exponent (power) calculation on a**b =10 to the power 20
operators
Floor Division - The
division of operands where
the result is the quotient in
which the digits after the
decimal point are removed. 9//2 = 4 and 9.0//2.0 = 4.0, -
//
But if one of the operands 11//3 = -4, -11.0//3 = -4.0
is negative, the result is
floored, i.e., rounded away
from zero (towards negative
infinity):
Example
#!/usr/bin/python
a = 21
b = 10
c=0
c=a+b
print "Value of c is ", c
c=a-b
print "Value of c is ", c
c=a*b
print "Value of c is ", c
c=a/b
print "Value of c is ", c
c=a%b
print "Value of c is ", c
a=2
b=3
c = a**b
print "Value of c is ", c
a = 10
b=5
c = a//b
print "Value of c is ", c
Output:
Value of c is 31
Value of c is 11
Value of c is 210
Value of c is 2
Value of c is 1
Value of c is 8
Value of c is 2
Python Comparison Operators Example
Assume variable a holds 10 and variable b holds 20, then −
Operator Description Example
If the values of two operands
== are equal, then the condition (a == b) is not true.
becomes true.
If values of two operands are
!= not equal, then condition
becomes true.
If values of two operands are
(a <> b) is true. This is similar
<> not equal, then condition
to != operator.
becomes true.
If the value of left operand is
greater than the value of right
> (a > b) is not true.
operand, then condition
becomes true.
If the value of left operand is
less than the value of right
< (a < b) is true.
operand, then condition
becomes true.
If the value of left operand is
greater than or equal to the
>= (a >= b) is not true.
value of right operand, then
condition becomes true.
If the value of left operand is
less than or equal to the value
<= (a <= b) is true.
of right operand, then
condition becomes true.
Example
#!/usr/bin/python
a = 21
b = 10
c=0
if ( a == b ):
print "a is equal to b"
else:
print "a is not equal to b"
if ( a != b ):
print "a is not equal to b"
else:
print "a is equal to b"
if ( a <> b ):
print "a is not equal to b"
else:
print "a is equal to b"
if ( a < b ):
print "a is less than b"
else:
print "a is not less than b"
if ( a > b ):
print "a is greater than b"
else:
print "a is not greater than b"
a = 5;
b = 20;
if ( a <= b ):
print "a is either less than or equal to b"
else:
print "a is neither less than nor equal to b"
if ( b >= a ):
print "b is either greater than or equal to b"
else:
print " b is neither greater than nor equal to b"
Output:
a is not equal to b
a is not equal to b
a is not equal to b
a is not less than b
a is greater than b
a is either less than or equal to b
b is either greater than or equal to b
Python Assignment Operators Example
Assume variable a holds 10 and variable b holds 20, then
Operator Description Example
Assigns values from right side c = a + b assigns value of a +
=
operands to left side operand b into c
It adds right operand to the
+= Add c += a is equivalent to c = c +
left operand and assign the
AND a
result to left operand
It subtracts right operand
-=
from the left operand and
Subtract c -= a is equivalent to c = c - a
assign the result to left
AND
operand
It multiplies right operand
*=
with the left operand and c *= a is equivalent to c = c *
Multiply
assign the result to left a
AND
operand
It divides left operand with c /= a is equivalent to c = c /
/= Divide
the right operand and assign ac /= a is equivalent to c = c /
AND
the result to left operand a
%= It takes modulus using two
c %= a is equivalent to c = c
Modulus operands and assign the result
%a
AND to left operand
Performs exponential (power)
**=
calculation on operators and c **= a is equivalent to c = c
Exponent
assign value to the left ** a
AND
operand
It performs floor division on
//= Floor c //= a is equivalent to c = c //
operators and assign value to
Division a
the left operand
Example
#!/usr/bin/python
a = 21
b = 10
c=0
c=a+b
print "Value of c is ", c
c += a
print "Value of c is ", c
c *= a
print "Value of c is ", c
c /= a
print "Value of c is ", c
c =2
c %= a
print "Value of c is ", c
c **= a
print "Value of c is ", c
c //= a
print "Value of c is ", c
Output:
Value of c is 31
Value of c is 52
Value of c is 1092
Value of c is 52
Value of c is 2
Value of c is 2097152
Value of c is 99864
Python Bitwise Operators Example
There are following Bitwise operators supported by Python language
Operator Description Example
Operator copies a bit to the
& Binary
result if it exists in both (a & b) (means 0000 1100)
AND
operands
It copies a bit if it exists in (a | b) = 61 (means 0011
| Binary OR
either operand. 1101)
^ Binary It copies the bit if it is set in (a ^ b) = 49 (means 0011
XOR one operand but not both. 0001)
(~a ) = -61 (means 1100
~ Binary
It is unary and has the effect 0011 in 2's complement
Ones
of 'flipping' bits. form due to a signed binary
Complement
number.
The left operands value is
<< Binary moved left by the number of a << = 240 (means 1111
Left Shift bits specified by the right 0000)
operand.
The left operands value is
>> Binary moved right by the number a >> = 15 (means 0000
Right Shift of bits specified by the right 1111)
operand.
Example
#!/usr/bin/python
a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c=0
c = a & b; # 12 = 0000 1100
print "Value of c is ", c
c = a | b; # 61 = 0011 1101
print "Value of c is ", c
c = a ^ b; # 49 = 0011 0001
print "Value of c is ", c
c = ~a; # -61 = 1100 0011
print "Value of c is ", c
c = a << 2; # 240 = 1111 0000
print "Value of c is ", c
c = a >> 2; # 15 = 0000 1111
print "Value of c is ", c
Output:
Value of c is 12
Value of c is 61
Value of c is 49
Value of c is -61
Value of c is 240
Value of c is 15
Python Logical Operators Example
Operator Description Example
and
If both the operands are true
Logical (a and b) is true.
then condition becomes true.
AND
or If any of the two operands
Logical are non-zero then condition (a or b) is true.
OR becomes true.
not
Used to reverse the logical
Logical Not(a and b) is false.
state of its operand.
NOT
Example
#!/usr/bin/python
a=True
b=False
print “a and b is:”,a and b
print “a or b is:”,a or b
print “not(a and b) is :”,not(a and b)
Output:
a and b is:False
a or b is:True
not(a and b) is:True
Python Membership Operators Example
• Python’s membership operators test for membership in a sequence,
such as strings, lists, or tuples.
• There are two membership operators as explained below
Operator Description Example
Evaluates to true if it finds a
x in y, here in results in a 1 if
in variable in the specified
x is a member of sequence y.
sequence and false otherwise.
Evaluates to true if it does not x not in y, here not in results
not in find a variable in the specified in a 1 if x is not a member of
sequence and false otherwise. sequence y.
Example
#!/usr/bin/python
a = 10
b = 20
list = [1, 2, 3, 4, 5 ];
if ( a in list ):
print "a is available in the given list"
else:
print "a is not available in the given list"
if ( b not in list ):
print "b is not available in the given list"
else:
print "b is available in the given list"
a=2
if ( a in list ):
print "a is available in the given list"
else:
print "a is not available in the given list"
Output:
a is not available in the given list
b is not available in the given list
a is available in the given list
Python Identity Operators Example
• Identity operators compare the memory locations of two objects.
Operator Description Example
Evaluates to true if the
variables on either side of the x is y, here is results in 1 if
is
operator point to the same id(x) equals id(y).
object and false otherwise.
Evaluates to false if the
x is not y, here is not results
variables on either side of the
is not in 1 if id(x) is not equal to
operator point to the same
id(y).
object and true otherwise.
Example
#!/usr/bin/python
a = 20
b = 20
if ( a is b ):
print "a and b have same identity"
else:
print "a and b do not have same identity"
if ( id(a) == id(b) ):
print "a and b have same identity"
else:
print "a and b do not have same identity"
b = 30
if ( a is b ):
print "a and b have same identity"
else:
print "a and b do not have same identity"
if ( a is not b ):
print "a and b do not have same identity"
else:
print "a and b have same identity"
Output:
a and b have same identity
a and b have same identity
a and b do not have same identity
a and b do not have same identity
Python Operators Precedence Example
Operator Description
** Exponentiation (raise to the power)
Complement, unary plus and minus (method names
~+-
for the last two are +@ and -@)
* / % // Multiply, divide, modulo and floor division
+- Addition and subtraction
>> << Right and left bitwise shift
& Bitwise 'AND'td>
^| Bitwise exclusive `OR' and regular `OR'
<= < > >= Comparison operators
<> == != Equality operators
= %= /= //= -= +=
Assignment operators
*= **=
is is not Identity operators
in not in Membership operators
not or and Logical operators
Example
#!/usr/bin/python
a = 20
b = 10
c = 15
d=5
e=0
e = (a + b) * c / d #( 30 * 15 ) / 5
print "Value of (a + b) * c / d is ", e
e = ((a + b) * c) / d # (30 * 15 ) / 5
print "Value of ((a + b) * c) / d is ", e
e = (a + b) * (c / d); # (30) * (15/5)
print "Value of (a + b) * (c / d) is ", e
e = a + (b * c) / d; # 20 + (150/5)
print "Value of a + (b * c) / d is ", e
Output:
Value of (a + b) * c / d is 90
Value of ((a + b) * c) / d is 90
Value of (a + b) * (c / d) is 90
Value of a + (b * c) / d is 50