Operators in Python - Python: Programming Language for Beginners - Learn In A Day! (2015)

Python: Programming Language for Beginners - Learn In A Day! (2015)

Chapter 5. Operators in Python

Operator: An operator is a symbolic entity which carries out arithmetic, logical or several other calculations on one or more character or numerical entities.

Operand: An operand is a character or a numerical entity upon which an operator carries out calculations.

Operation: An operation is an action or a calculation carried by an operator on an operand.

Operands

Example: 5 + 5 = 10

Operators

Operators in Python are of the following types.

Arithmetic Operators

Relational Operators (Comparison operators)

Assignment Operators

Logical Operators

Bitwise Operators

Membership Operators

Identity Operators

Each of the operators is discussed in detailed as follows:

1. Arithmetic operators:

Arithmetic operators are used for performing arithmetic calculations. A table listing out the arithmetic operators is as follows:

Command

Name

example

output

+

Addition

4+4

8

-

Subtraction

8-6

2

*

Multiplication

4*6

24

/

Division

22/3

7

%

Remainder

19%3

1

**

Exponent

2**5

32

Now let’s check with a few arithmetic operations.

Example - Operations

>>> 21 + 1

22

>>> 18295+449566

467861

(These are additions)

>>> 7-6

1

(Subtraction)

>>> 2*6

12

(Multiplication)

>>> 5**3

125

(Exponentials e.g. this one is 5 cubed)

>>> print "1 + 2 is an addition"

1 + 2 is an addition

(this print statement, writes the text in the quotes on the screen)

>>> 23.0/3.0

7.6666...

(Division,2nd time ignoring remainder/decimals,

3rd time including decimals )

>>> 23%3

2

The priority of the operators applies to this language as well. The priority list is as follows:

parentheses ()

exponents **

multiplication *, division \, and remainder %

addition + and subtraction-

Ex:

>>> 5 - 40 - 3

-38

>>> 5 - (40 - 3)

-32

2. Relational operators (Comparison operators): Relation operators are used to check if a relationship between two or more operands is true or false. In other words, these operators used to compare two or more operands. The comparison always results in a Boolean value (either true or false). They are generally used with if-else statements. Relational operators used in Python are listed in the following table:

Before going through the table, let us assume that p=10 and q=5

Operator

What it is and what it does

Example expressions

==

It is the ‘equal to’ operator which is used for checking if two operands have equal values or not. If they have equal values, the Boolean value ‘True’ is returned, else ‘False’ is returned.

(p == q) returns false

(Since p=10 and q=5).

!=

It is the ‘not equal to’ operator which is used for checking if two operands have different values or not. If they have different values, the Boolean value ‘True’ is returned, else ‘False’ is returned.

(p != q) returns false

<>

It is similar to the ‘not equal to’ operator which is used for checking if two operands have different values or not. If they have different values, the Boolean value ‘True’ is returned, else ‘False’ is returned

(p <> q) returns false

>

It is the ‘greater than’ operator which is used for checking if the first operand is greater than the second operand or not. If the first operand is greater than the second operand the Boolean value ‘True’ is returned, else ‘False’ is returned.

(p >q) returns true.

<

It is the ‘less than’ operator which is used for checking if the first operand is lesser than the second operand or not. If the first operand is lesser than the second operand, the Boolean value ‘True’ is returned, else ‘False’ is returned

(p < q) returns false.

>=

It is the ‘greater than or equal to ’ operator which is used for checking if the first operand is greater than or equal to the second operand. If the first operand is greater than or equal to the second operand, the Boolean value ‘True’ is returned, else ‘False’ is returned.

(p >= q) returns true.

<=

It is the ‘less than or equal to’ operator which is used for checking if the first operand is greater than or equal to the second operand. If the first operand is less than or equal to the second operand, the Boolean value ‘True’ is returned, else ‘False’ is returned.

(p <= q) returns False.

You will understand the relational operators better, if you take a look at the following example program:

Example:

#!/usr/bin/python

p = 10

q = 5

if ( p == q ):

print " p is equal to q"

else:

print "p is not equal to q"

if ( p != q ):

print "p is not equal to q"

else:

if ( p <> q ):

print "p is not equal to q"

else:

print "p is equal to q"

if ( p < q ):

print " p is less than q"

else:

print " p is not less than q"

if ( p > q ):

print "p is greater than q"

else:

print "p is not greater than q"

if ( p <= q ):

print "p is either less than or equal to q"

else:

print "p is neither less than nor equal to q"

if ( p >= q ):

print "p is either greater than or equal to q"

else:

print "p is neither greater than nor equal to q"

OUTPUT:

p is not equal to q

p is not equal to q

p is not equal to q

p is not less than q

p is greater than q

p is neither less than or equal to q

p is either greater than or equal to b

3. Assignment operators:

The assignment operators are used for storing the value of an operand in another operand. Also, they can be used to assign the result of a calculation as the value of an operand.

Assignment operators used in Python are listed in the following table:

Operator

What it is and what it does

Example expressions

=

Assignment operator used between two operands. When used, it assigns the value of the right operand to the left operand.

a=6 (Assigns the value 6 to a)

b=a (Assigns the value of a to b)

p= q+r (Assigns the sum value of q+r to p)

+=

It is the add AND assignment operator and does both addition and assignment. Adds right and left operands and stores the sum in the left operand

p+=q (Adds p and q and then stores the sum in p)

The above expression is equivalent to the expression: p=p+q

-=

It is the subtract AND assignment operator and does both subtraction and assignment. It works by subtracting right operand from the left operand and then storing the difference in the left operand.

p-=q (Subtracts q from p and then stores the difference in p)

The above expression is equivalent to the expression: p=p-q

*=

It is the multiply AND assignment operator and does both multiplication and assignment. It works by multiplying right operand with the left operand and then storing the product in the left operand.

p*=q (Multiplies q with p and then stores the product in p)

The above expression is equivalent to the expression: p=p*q

/=

It is the divide AND assignment operator which performs both division and assignment. It works by dividing left operand with the right operand and then storing the quotient in the left operand.

p/=q (Divides p with q and then stores the quotient in p)

The above expression is equivalent to the expression: p=p/q

%=

It is the modulus AND assignment operator and performs both modulus(remainder) and assignment. It works by dividing left operand with the right operand and then storing the remainder in the left operand.

p%=q (Divides p with q and then stores the remainder in p)

The above expression is equivalent to the expression: p=p%q

**=

It is the exponent AND assignment operator and performs both exponentiation and assignment. It raises the left operand to the power of the right operand and then stores the result in the left operand.

p**=q (Raises p to the power of q and then stores the result in p)

The above expression is equivalent to the expression: p=p**q

//=

Performs floor division and assignment operations between the operands and stores the result in the left operand.Floor division is equivalent to the general division except that the quotient is rounded down in floor division.

p//=q (Divides p with q and then stores the rounded quotient in p)

The above expression is equivalent to the expression: p=p//q

You will understand the relational operators better, if you take a look at the following example program:

Example:

#!/usr/bin/python

p = 10

q = 2

r = 0

r = p + q

print " The value of r is ", r

r += p

print "After add AND assignment, value of r is ", r

r *= p

print "After multiply AND assignment, value of r is ", r

r /= p

print "After divide AND assignment, value of r is ", r

r = 2

r%= p

print "After modulus AND assignment, value of r is ", r

r **= p

print "After exponentiation AND assignment, value of r is ", r

r //= p

print "After floor division AND assignment, value of r is ", r

OUTPUT:

The value of r is 12

After add AND assignment, value of r is 22

After multiply AND assignment, value of r is 220

After divide AND assignment, value of r is 22

After modulus AND assignment, value of r is 2

After exponentiation AND assignment, value of r is 1024

After floor division AND assignment, value of r is 102

4. Bitwise operators

The operators which operate upon bits(binary digits 0’s and 1’s) are called bitwise operators. Bitwise operators used in Python are listed in the following table:

Operator

What it is and what it does

Example

&

Binary AND operator.It checks each bit of the two operands and copies ‘0’ to the result if atleast one ‘0’ is present in either of the operands.

Illustration:

p q p&q

0 0 0

0 1 0

1 0 0

1 1 1

p=2, q=5

In binary form,

p=0010

q= 0101

-----------------

p&q=0000

|

Binary OR operator.It checks each bit of the two operands and copies ‘1’ to the result if atleast one ‘1’ is present in either of the operands.

Illustration:

p q p|q

0 0 0

0 1 1

1 0 1

1 1 1

p=0010

q= 0101

-----------------

p|q=0111

^

Binary XOR operator (Exclusive OR). It is similar to the Binary OR operator but the difference is that, it copies ‘1’ to the result if a ‘1’ is present in only one of the operands and not in both.

Illustration:

p q p^q

0 0 0

0 1 1

1 0 1

1 1 0

p=2 and q=3

In binary form:

p=0010

q= 0111

-----------------

p|q=0101

~

Binary Ones Complement operator.

It is a unary operator which means it operates upon only a single bit. It flips the bit to its complementary bit.

It means 0’s are replaced by 1’s and 1’s are replaced by 0’s.

p=2

In binary form

p= 0010

-----------------

~p=1101

<<

Binary Left Shift operator. This operator moves the bits of the left operand to the left. The number of bits that are to be moved and lost to the left is stated by the right operand. After the bits are lost from the left, same number of 0’s get filled in the right.

p=0010

p << 2 = 1000

>>

Binary Right Shift operator. This operator moves the bits of the left operand to the right. The number of bits that are to be moved and lost to the right is stated by the right operand. After the bits are lost from the right, same number of 0’s get filled in the left.

p=0010

p >> 2 = 0000

The Binary left and right shift operators can be easily understood through the following illustration:

You will understand the Bitwise operators better, if you take a look at the following example program:

Example:

#!/usr/bin/python

p = 60 # 60 = 0011 1100

q = 13 # 13 = 0000 1101

r = 0

r = p & q; # 12 = 0000 1100

print " Binary AND of p and q gives: ", r

r = p | q; # 61 = 0011 1101

print "Binary OR of p and q gives: ", r

r = p ^ q; # 49 = 0011 0001

print "Binary XOR of p and q gives: ", r

r = ~p; # -61 = 1100 0011

print "The value of p after one's complement is ", r

r = p << 2; # 240 = 1111 0000

print "The value of p after binary left shift is ", r

r = p >> 2; # 15 = 0000 1111

print "The value of p after binary right shift of is ", r

OUTPUT:

Binary AND of p and q gives:12

Binary OR of p and q gives: 61

Binary XOR of p and q gives:49

The value of p after one's complement is -61

The value of p after binary left shift is 240

The value of p after binary right shift of is 15

5. Logical operators

Python supports the following logical operators:

Operator

What it does

Example

and

The logical AND operator between two or more expressions returns false, even if at least one of the given expressions is false.

a=10, b=20, c=0

The expression a>b returns false

The expression b>c returns true

Thus, the expression (a>b and b>c) returns false

or

The logical OR operator between two or more expressions returns true, even if at least one of the given expressions is true.

a=10, b=20, c=0

The expression a>b returns false

The expression b>c returns true

Thus, the expression (a>b or b>c) returns true

not

The logical NOT operator is used to reverse the logical state of an expression

a=10, b=20, c=0

The expression a>b returns false

The expression b>c returns true

Thus, the expression (a>b or b>c) returns true

And thus the expressions not(a>b) returns true

not(b>c) returns false

not(a>b or b>c) returns false

You will understand the logical operators better, if you take a look at the following example program:

Example:

#!/usr/bin/python

p=10

q=20

r=0

if(p>q):

print'(p>q) is true'

else:

print'(p>q) is false'

if(q>r):

print'(q>r) is true'

else:

print'(q>r) is false'

if(p>q and q>r):

print 'Hence,(p>q and q>r) is true'

else:

print 'Hence,(p>q and q>r) is false'

if(p>q or q>r):

print 'Hence,(p>q or q>r) is true'

else:

print 'Hence,(p>q or q>r) is false'

a= not(p>q and q>r)

print 'not(p>q and q>r) is', a

b= not(p>q or q>r)

print 'not(p>q or q>r) is', b

OUTPUT:

(p>q) is false

(q>r) is true

Hence,(p>q and q>r) is false

Hence,(p>q or q>r) is true

not(p>q and q>r) is True

not(p>q or q>r) is False

6. Membership operators

Membership operators check whether a variable is a member of a sequence or not. The sequence could be of strings or lists.

Python supports the following membership operators:

Operator

What it does

Example expression

in

The expression using this operator returns true if a given variable is found to be a member of a specific sequence. It returns false if the variable cannot be found in the sequence.

a in b. Here the ‘in’ operator returns true if a is found in the sequence b.

not in

The expression using this operator returns true if a given variable is not found to be a member of a specific sequence. It returns false if the variable can be found in the sequence.

a not in b. Here the ‘not in’ operator returns true if a is not found in the sequence b.

You will understand the membership operators better, if you take a look at the following example program:

Example:

#!/usr/bin/python

p = 10

q = 20

r = 20-10

Powers_of_10 = [10,100,1000,10000];

if ( p in Powers_of_10 ):

print "p is a member of the given list"

else:

print "p is not a member of the given list"

if ( q not in Powers_of_10 ):

print "q is not a member of the given list"

else:

print "q is a member of the given list"

if ( r in Powers_of_10 ):

print "r is a member of the given list"

else:

print "r is not a member of the given list"

OUTPUT:

p is a member of the given list

q is not a member of the given list

r is a member of the given list

7. Identity operators

Identity operators compare the memory locations of two objects. There are two Identity operators explained below:

Operator

What it does

Example

is

The expression using this operator returns true, if the variables on which the operator is operating upon, are pointing to the same object.

a is b, here ‘ is’ operator returns true, if id(a) equals id(b).

is not

The expression using this operator returns true, if the variables on which the operator is operating upon, are not pointing to the same object.

a is not b, here is not operator returns true, if id(a) is not equal to id(b).

You can understand identity operators better if you go through the following program:

#!/usr/bin/python

p = 20

q = 20

if ( p is q ):

print "p and q have the same identity"

else:

print "p and q do not have the same identity"

if ( id(p) == id(q) ):

print "p and q have the same identity"

else:

print "p and q do not have the same identity"

q = 30

if ( p is q ):

print "p and q have the same identity"

else:

print "p and q do not have the same identity"

if ( p is not q ):

print "p and q do not have the same identity"

else:

print "p and q have the same identity"

OUTPUT:

p and q have the same identity

p and q have the same identity

p and q do not have the same identity

p and q do not have the same identity.