Python Operators

 Python Operators 

The operator is a symbol that performs a specific operation on the operand. Operators are used in logic building in the programming language.
A sequence of operands and operators is called an expression ex. like a + b - 5.
Python provides a variety of operators, which are described as follows.
 
Types of Operator
Python language supports the following types of operators.
  1. Arithmetic Operators
  1. Comparison (Relational) Operators
  1. Assignment Operators
  1. Logical Operators
  1. Bitwise Operators
  1. Membership Operators
  1. Identity Operators
 
1.  Python Arithmetic Operators
Assume variable a hold 10 and variable b hold 20, then −
 

Operator

Description

Example

+ Addition

Adds values on either side of the operator.

a + b = 30

- Subtraction

Subtracts the right-hand operand from the left-hand operand.

a – b = -10

* Multiplication

Multiplies values on either side of the operator

a * b = 200

/ Division

Divides left-hand operand by right-hand operand

b / a = 2

% Modulus

Divides left-hand operand by right-hand operand and returns the remainder

b % a = 0

** Exponent

Performs exponential (power) calculation on operators

a**b =10 to the power 20

//

Floor Division - The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity) −

9//2 = 4 and 9.0//2.0 = 4.0, -11//3 = -4, -11.0//3 = -4.0

 
Example
Assume variable a holds 21 and variable b holds 10, then −
a = 21
b = 10
c = 0
 
c = a + b
print ("Line 1 - Value of c is ", c)
 
c = a - b
print "Line 2 - Value of c is ", c
 
c = a * b
print "Line 3 - Value of c is ", c
 
c = a / b
print "Line 4 - Value of c is ", c
 
c = a % b
print "Line 5 - Value of c is ", c
 
a = 2
b = 3
c = a**b
print "Line 6 - Value of c is ", c
 
a = 10
b = 5
c = a//b
print "Line 7 - Value of c is ", c
outputs  −
Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2
Line 5 - Value of c is 1
Line 6 - Value of c is 8
Line 7 - Value of c is 2
============================================================================
2.   Python Comparison Operators
These operators compare the values of operands and show relationships among them. They are also called Relational operators.
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 becomes true.

(a == b) is not true.

!=

If the values of two operands are not equal, then the condition becomes true.

(a != b) is true.

<> 

If the values of two operands are not equal, then condition becomes true.

(a <> b) is true. This is similar to != operator.

> 

If the value of the left operand is greater than the value of the right operand, then the condition becomes true.

(a > b) is not true.

< 

If the value of the left operand is less than the value of the right operand, then the condition becomes true.

(a < b) is true.

>=

If the value of the left operand is greater than or equal to the value of the right operand, then the condition becomes true.

(a >= b) is not true.

<=

If the value of the left operand is less than or equal to the value of the right operand, then the condition becomes true.

(a <= b) is true.

 
Example
Assume variable a holds 10 and variable b holds 20, then −
a = 21
b = 10
c = 0
 
if ( a == b ):
   print "Line 1 - a is equal to b"
else:
   print "Line 1 - a is not equal to b"
 
if ( a != b ):
   print "Line 2 - a is not equal to b"
else:
   print "Line 2 - a is equal to b"
 
if ( a <> b ):
   print "Line 3 - a is not equal to b"
else:
   print "Line 3 - a is equal to b"
 
if ( a < b ):
   print "Line 4 - a is less than b"
else:
   print "Line 4 - a is not less than b"
 
if ( a > b ):
   print "Line 5 - a is greater than b"
else:
   print "Line 5 - a is not greater than b"
 
a = 5;
b = 20;
if ( a <= b ):
   print "Line 6 - a is either less than or equal to  b"
else:
   print "Line 6 - a is neither less than nor equal to  b"
 
if ( b >= a ):
   print "Line 7 - b is either greater than  or equal to b"
else:
   print "Line 7 - b is neither greater than  nor equal to b"
outputs:-
Line 1 - a is not equal to b
Line 2 - a is not equal to b
Line 3 - a is not equal to b
Line 4 - a is not less than b
Line 5 - a is greater than b
Line 6 - a is either less than or equal to b
Line 7 - b is either greater than or equal to b
====================================================================
3.   Python Assignment Operators
These operators are used to assign values to operands. Also called compound operators:-
Assume variable a holds 10 and variable b holds 20, then −
 

Operator

Description

Example

=

Assigns values from right side operands to left side operand

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

+= Add AND

It adds the right operand to the left operand and assigns the result to the left operand

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

-= Subtract AND

It subtracts the right operand from the left operand and assigns the result to the left operand

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

*= Multiply AND

It multiplies the right operand with the left operand and assigns the result to the left operand

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

/= Divide AND

It divides the left operand with the right operand and assigns the result to the left operand

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

%= Modulus AND

It takes modulus using two operands and assigns the result to the left operand

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

**= Exponent AND

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

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

//= Floor Division

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

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

Example
Assume variable a holds 10 and variable b holds 20, then −

a = 21
b = 10
c = 0
 
c = a + b
print "Line 1 - Value of c is ", c
 
c += a
c=c+a
print "Line 2 - Value of c is ", c
 
c *= a
print "Line 3 - Value of c is ", c
 
c /= a
print "Line 4 - Value of c is ", c
 
= 2
c %= a
print "Line 5 - Value of c is ", c
 
c **= a
print "Line 6 - Value of c is ", c
 
c //= a
print "Line 7 - Value of c is ", c
outputs:-
Line 1 - Value of c is 31
Line 2 - Value of c is 52
Line 3 - Value of c is 1092
Line 4 - Value of c is 52
Line 5 - Value of c is 2
Line 6 - Value of c is 2097152
Line 7 - Value of c is 99864
===========================================================================
4.   Python Bitwise Operators
Bitwise operator works on bits and performs bit by bit operation.
===== concept of bit ======
Concept of bit:-
A bit (Binary digit) is the basic unit of information stored in two states, as ON (1)or OFF(0).
It is used by the BINARY number system which base of 2.
In the decimal number system, a number constructed upon the base of 10.
  231=(2 x 102)+(3 x 101)+(1 x 100)
      =200+30+1=      =231
A binary number can be converted into a decimal number -
1011010=(1 x 26)+(0 x 25)+(1 x 24)+(1 x 23)+(0 x 22)+(1 x 21)+(0 x 20)
          =(1 x 64) +(0 x 32)+(1 x 16)+(1 x 8)+(0 x 4)+(1 x 2)+(0 x 1)
          =64+0+16+8+0+2+0
          =90
So, (1011010)2= (90)10 
 
Byte
A byte is a sequence of eight bits. The maximum value of a byte is 25.
Tabular representation of a byte

1 Byte ( 8 bits )

 

8th

7th

6th

5th

4th

3rd

2nd

1st

Place value

128

64

32

16

8

4

2

1

 
Table show of a byte shows how the maximum value of a byte is 255
 

1 Byte ( 8 bits )

Place Value

128

64

32

16

8

4

2

1

 

 

 

1

1

1

1

1

1

1

1

 

 

 

27

26

25

24

23

22

21

20

 

 

 

128

64

32

16

8

4

2

1

=

255

 
A decimal number 93 can be represented in binary form like bellow –
 

1 Byte ( 8 bits )

Place Value

128

64

32

16

8

4

2

1

 

 

 

0

1

0

1

1

1

0

1

 

 

 

27

26

25

24

23

22

21

20

 

 

 

0

64

0

16

8

4

0

1

=

93

 
=============================================
There are the following Bitwise operators supported by the Python language
 

Operator

Description

Example

& Binary AND

Operator copies a bit to the result if it exists in both operands

(a & b) (means 0000 1100)

| Binary OR

It copies a bit if it exists in either operand.

(a | b) = 61 (means 0011 1101)

^ Binary XOR

It copies the bit if it is set in one operand but not both.

(a ^ b) = 49 (means 0011 0001)

~ Binary One's Complement

It is unary and has the effect of 'flipping' bits.

(~a ) = -61 (means 1100 0011 in 2's complement form due to a signed binary number.

<< Binary Left Shift

The left operands value is moved left by the number of bits specified by the right operand.

a << 2 = 240 (means 1111 0000)

>> Binary Right Shift

The left operands value is moved right by the number of bits specified by the right operand.

a >> 2 = 15 (means 0000 1111)

 
Bitwise AND (&)
According to truth table:-
 

VALUE 1

VALUE 2

AND

0

0

0

0

1

0

1

0

0

1

1

1

 
Example of bitwise AND with one shared bit
x=13
y=22
print ( x & y)
 
The output of the example 
4
 
Explanation
 

1 Byte ( 8 bits )

Place Value

128

64

32

16

8

4

2

1

 

 

x

0

0

0

0

1

1

0

1

=

13

y

0

0

0

1

0

1

1

0

=

22

x & y

0

0

0

0

0

1

0

0

=

04

 
In the above table, the value set for x(13) is in 1st, 3rd, and 4th place. The place value respectively is 1,4 and 8 and the value set for y(22) in the 2nd, 3rd and 5th place with the corresponding place value 2, 4, and 16.
x and y answer is  4 is returned
 
Example of bitwise AND with two shared bit
x=77
y=198
print ( x & y)
Output
68
 
Explanation

1 Byte ( 8 bits )

Place Value

128

64

32

16

8

4

2

1

 

 

x

0

1

0

0

1

1

0

1

=

77

y

1

1

0

0

0

1

1

0

=

198

x & y

0

1

0

0

0

1

0

0

=

68

In the above table, the value set for x(77) is in the 1st, 3rd, 4th and 7th place. The place values are 1, 4, 8, and 64, and the value is set for y(198) in the 2nd, 3rd, 7th, and 8th place with the corresponding place values 2, 4, 64, and 128.
The answer of these 3rd and 7th bits. So 64 + 4 = 68 returns.
 
Bitwise OR ( | )
According to a truth table
TRUTH TABLE

VALUE 1

VALUE 2

OR

0

0

0

0

1

1

1

0

1

1

1

1

 
Example of bitwise OR
x=5;
y=11;
print ( x | y)
 
Output
15
 
Explanation

1 Byte ( 8 bits )

Place Value

128

64

32

16

8

4

2

1

 

 

x

0

0

0

0

0

1

0

1

=

5

y

0

0

0

0

1

0

1

1

=

11

x | y

0

0

0

0

1

1

1

1

=

15

 
In the above table, the value set for x(5) is in 1st and 3rd place. The place value respectively is 1 and 4, and the value set for y(11) in the 1st, 2nd, and 4th place with the corresponding place value 1, 2, and 8.
The table shows that the x and y set together either 1st or 2nd or 3rd or 4th bits. So return value is the addition of place value of the sets bits that is 8+4+2+1=15.
 
 
Bitwise XOR (^)
The Xor operator also performs a bitwise comparison in two numeric expressions and sets the corresponding bit in the result. When one and only one of the expressions evaluates to true the result is true.
 
TRUTH TABLE

VALUE 1

VALUE 2

XOR

0

0

0

0

1

1

1

0

1

1

1

0

 
The below table shows how an XOR operation performs.

Expression1

Expression2

Result

False

False

False

False

True

True

True

False

True

True

True

False

 
Example of bitwise XOR
x=12
y=11
print ( x ^ y)
 
Output
7
 
Explanation
 

1 Byte ( 8 bits )

Place Value

128

64

32

16

8

4

2

1

 

 

x

0

0

0

0

1

1

0

0

=

12

y

0

0

0

0

1

0

1

1

=

11

x ^ y

0

0

0

0

0

1

1

1

=

07

In the above table, the value set for x(12) is in 3rd and 4th place. The place value respectively is 4 and 8, and the value sets for y(11) in the 1st, 2nd, and 4th place with the corresponding place value 1, 2, and 8.
The x and y set together either 1st or 2nd or 3rd or 4th bits but they shared together only a 4th bit. So, return value is the addition of the place value of the set bits but not the bit shared together that is 4+2+1=7.
 
 
Bitwise NOT
The below table will display how the NOT operator performs on $x and $y and returns true when a set bit of one expression is not set in another expression.
Example of bitwise NOT using after AND
x=12
y=10
print ( x & ~ y)
 
Output
4
 
Explanation

1 Byte ( 8 bits )

Place Value

128

64

32

16

8

4

2

1

 

 

x

0

0

0

0

1

1

0

0

=

12

y

0

0

0

0

1

0

1

0

=

10

~y

0

0

0

0

0

1

0

1

=

05

x & ~y

0

0

0

0

0

1

0

0

=

04

In the above table, the value set for x(12) is in 3rd and 4th place. The place value respectively is 4 and 8, and the value sets for y(10) in the 2nd and 4th place with the corresponding place value 2 and 8.
So you can see from the above table that, the $x and $y set together either 1st or 2nd or 3rd or 4th bits but they shared together only a 4th bit. So return value is the 4, because of only bit sets in $x but not in $y.
 
Example of bitwise NOT using before AND
x=12;
y=10;
print ( ~x & y)
 
Output
2
 
 
Bit Shifting

0

0

1

1

1

1

0

0

A= 60
B=3
Left shift

1

1

1

0

0

0

0

0

Right shift

0

0

0

0

0

1

1

1

 
If a and b are two numbers, BIT SHIFTING shifts a bits b number of steps. each step refers to multiply by two if it is BIT SHIFT LEFT. If it is BIT SHIFT RIGHT, then each step refers to division by two.
Example of Bit Shifting ( left shift )
x=8;
y=3;
print (x << y)
 
Output
64
 
 
Explanation

1 Byte ( 8 bits )

Place Value

128

64

32

16

8

4

2

1

 

 

x

0

0

0

0

1

0

0

0

=

8

Output

0

1

0

0

0

0

0

0

=

64

 
In the above example, the value of x that is 8 is taken and a BIT SHIFT LEFT operation is performed. So, 8 is multiplied by 2 thrice. Thus we get 8 x 2 x 2 x 2 = 64.
 
Example of Bit Shifting ( right shift )
x=8
y=3
print (x >> y)
 
Output
1
 
Explanation

1 Byte ( 8 bits )

Place Value

128

64

32

16

8

4

2

1

 

 

x

0

0

0

0

1

0

0

0

=

8

Output

0

0

0

0

0

0

0

1

=

1

In the above example, value of x that is 8 is taken and a BIT SHIFT RIGHT operation is performed. So, 8 is divided by 2 three times. Thus we get 8/2=4/2=2/2 = 1.
Example
a = 60            # 60 = 0011 1100
b = 13            # 13 = 0000 1101
c = 0
 
c = a & b;        # 12 = 0000 1100
print "Line 1 - Value of c is ", c
 
c = a | b;        # 61 = 0011 1101
print "Line 2 - Value of c is ", c
 
c = a ^ b;        # 49 = 0011 0001
print "Line 3 - Value of c is ", c
 
c = ~a;           # 61 = 1100 0011
print "Line 4 - Value of c is ", c
 
c = a << 2;       # 240 = 1111 0000
print "Line 5 - Value of c is ", c
 
c = a >> 2;       # 15 = 0000 1111
print "Line 6 - Value of c is ", c
OUTPUTS
Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15
==============================================================
5.   Python Logical Operators
There are the following logical operators supported by the Python language.
Assume variable a holds 10 and variable b holds 20 then
 

Operator

Description

Example

and Logical AND

If both the operands are true then condition becomes true.

(a and b) is true.

or Logical OR

If any of the two operands are non-zero then condition becomes true.

(a or b) is true.

not Logical NOT

Used to reverse the logical state of its operand.

Not(a and b) is false.

 
TRUTH TABLE

VALUE 1

VALUE 2

AND

OR

XOR

0

0

0

0

0

0

1

0

1

1

1

0

0

1

1

1

1

1

1

0

 

VALUES

NOT

0

1

1

0

 
Example :-
a = 10
b = 10
c = -10
if a > 0 and b > 0:
            print("The numbers are greater than 0")
if a > 0 and b > 0 and c > 0:
            print("The numbers are greater than 0")
else:
            print("At least one number is not greater than 0")
 
output :-
The numbers are greater than 0
At least one number is not greater than 0
 
===============================================================================Python Membership Operators
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

in

Evaluate to true if it finds a variable in the specified sequence and false otherwise.

x in y, herein results in a 1 if x is a member of sequence y.

not in

Evaluate to true if it does not find a variable in the specified sequence and false otherwise.

x not in y, here not in results in a 1 if x is not a member of sequence y.

 
Example
a = 10
b = 20
list = [1, 2, 3, 4, 5 ];
 
if ( a in list ):
   print "Line 1 - a is available in the given list"
else:
   print "Line 1 - a is not available in the given list"
 
if ( b not in list ):
   print "Line 2 - b is not available in the given list"
else:
   print "Line 2 - b is available in the given list"
 
a = 2
if ( a in list ):
   print "Line 3 - a is available in the given list"
else:
   print "Line 3 - a is not available in the given list"
outputs −
Line 1 - a is not available in the given list
Line 2 - b is not available in the given list
Line 3 - a is available in the given list
========================================================================
6.   Python Identity Operators
Identity operators compare the memory locations of two objects. There are two Identity operators explained below −

Operator

Description

Example

is

Evaluate to true if the variables on either side of the operator point to the same object and false otherwise.

x is y, here it results in 1 if id(x) equals id(y).

is not

Evaluate to false if the variables on either side of the operator point to the same object and true otherwise.

x is not y, here are no results in 1 if id(x) is not equal to id(y).

 
Example
a = 20
b = 20
 
if ( a is b ):
   print "Line 1 - a and b have same identity"
else:
   print "Line 1 - a and b do not have same identity"
 
if ( id(a) == id(b) ):
   print "Line 2 - a and b have same identity"
else:
   print "Line 2 - a and b do not have same identity"
 
b = 30
if ( a is b ):
   print "Line 3 - a and b have same identity"
else:
   print "Line 3 - a and b do not have same identity"
 
if ( a is not b ):
   print "Line 4 - a and b do not have same identity"
else:
   print "Line 4 - a and b have same identity"
outputs: -
Line 1 - a and b have same identity
Line 2 - a and b have same identity
Line 3 - a and b do not have same identity
Line 4 - a and b do not have same identity
==============================================================
7.    Increment and Decrement Operators in Python
Python not support increments ( + + ) and decrements (--) operators.
====================================================================
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

~ + - Complement, unary plus and minus (method names for the last two are +@ and -@)

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

 
==================================================================  

Post a Comment

0 Comments