# PYTHON PROGRAMMING (2010)

### Basic Operators

The basic operators for python language are as follows.

· Arithmetic Operators

· Comparison Operators

· Logical Operators

· Membership Operators

· Identity Operators

*Arithmetic operators*

*Addition-*** **Adds values on either side of the operator. Represented by ‘+’.

*Example*

a + b will give 30.

** Subtraction-** This subtracts right hand operand from left hand operand. Represented by ‘-‘.

*Example*

a - b will give -10.

** Multiplication-** Multiplies values on either side of the operator. Represented by ‘*‘.

*Example*

a * b will give 200.

*Division-** *Divides left hand operand by right hand operand. Represented by ‘/’.

*Example*

b / a will give 2.

** Modulus-** Divides left hand operand by right hand operand and returns remainder. It is represented by ‘%’.

*Example*

b % a will give 0.

*Exponent***-** It performs power calculation on operators. It is represented by ‘**’.

*Example*

a**b will give 10 to the power 20.

*Floor Division***- **This is a kind of division of operands where the result will be the quotient where the digits after the decimal point are deducted.

*Example*

9//2 is equal to 4 and

9.0//2.0 is equal to

4.0.

*Program*

#!/usr/bin/python

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

*Output*

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

*Comparison operator*

**==- **Checks if the value of two operands is equal or not, if yes then only the condition becomes true.

*Example*

(a == b) is not true.

**!=- **Checks if the value of two operands is equal or not, if values are not equal. Then only the condition becomes true.

*Example*

(a != b) is true.

**<>- **Checks whether the value of two operands is same or not, if values are not same then only condition becomes true.

*Example*

(a <> b) is true.

**>=- **Checks if the value of left operand is larger than or equal to the value of right operand, if yes then condition becomes true.

*Example*

(a >= b) is not true.

**<=-** Checks if the value of left operand is smaller than or equal to the value of right operand, if yes then condition becomes true.

*Example*

(a <= b) is true.

*Program*

#!/usr/bin/python

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"

*Output*

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.

*Logical operator*

*And-*** **Termed Logical AND operator, both the values are true,therefore the condition becomes true.

*Example*

(a and b) is true.

*Or-*** **Termed Logical OR Operator any of the two values are non zero and then the condition becomes true.

*Example*

(a or b) is true.

*Not-*** **Termed as Logical NOT Operator that function is to reverse the logical situation of its operand.

*Example*

not(a and b) is false.

*Membership operator*

*In-** *Its evaluation is true only if it finds a particular variable in the specified sequence and falseif not.

*Example*

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

*Not in-** *If it’s evaluates is true and it does not finds a variable in the specified sequence and false if not.

*Example*

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

*Identify operator*

** Is-** Its evaluation is true if the variables on either side of the operator do point to the same object and falseif not.

*Example*

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

*Is not-*** **its evaluation is false if the variables on either side of the given operator points to the same object and true if not.

*Example*

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

*Program*

#!/usr/bin/python

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"

*Output*

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.