# Chapter 3: Operators and Expressions

### Example 3.1, Page number: 22

In :
#Program to demonstrate the use of Comma operator

#Result
print "Addition = ",2+3," \nSubtraction = ",5-4

Addition =  5
Subtraction =  1


### Example 3.2, Page number: 23

In :
#Program to demonstrate the use of Conditional operator with two values

#Result
#in python if..else statement can be written similar to conditional operator
#prints Result = 4 if 2 == 3, otherwise Result = 5

print "Result = ",4 if 2==3 else 5

Result =  5


### Example 3.3, Page number: 23

In :
#Program to use Conditional operator with two statements

#Result
#in python if..else statement can be written similar to conditional operator
#prints True if 3>2, otherwise prints False

print "True" if 3>2 else "False"

True


### Example 3.4, Page number: 25

In :
#Program to use increment operator as prefix

#Variable declaration
x = 10
y = 20

#Calculation
#there is no ++/-- operator in python

z = x * y
y += 1
a = x * y

#Result
print z,"  ",a

200    210


### Example 3.5, Page number: 25

In :
#Program to use increment operator as prefix

#Variable declaration
x = 10
y = 20

#Calculation
#there is no ++/-- operator in python

y += 1
z = x * y
a = x * y

#Result
print z,"  ",a

210    210


### Example 3.6, Page number: 26

In :
#Program to find the size and address of integer and float variables

#import python module
#sys python module is required for getsizeof() function.

import sys

#Variable declaration
#No specific float type variable declaration is available in python. If we
#assign a floating point value to a variable,
#it will be treated as floating point variable
x = 2
y = 2.0

#Result
#In python, integer variable uses 12 bytes and float variable uses 16 bytes of
#memory to store the variables
#sys.getsizeof() function returns the size of variable in bytes which is similar
#to sizeof() operator
#id() function returns the address of variables which is similar to '&' operator

print "Size (x) = ",sys.getsizeof(x)
print "Size (y) = ",sys.getsizeof(y)
print "Address of x = ",id(x),"and y = ",id(y)

Size (x) =  12
Size (y) =  16
Address of x =  5626076 and y =  54761072


### Example 3.7, Page number: 27

In :
#Program to use various relational operators and display their return values

#Result
#int() function is used to convert the boolean return value of the relational
#expression to integer/binary

print "Condition  :   Return Values"
print "10 != 10   :       ",int(10 != 10)
print "10 == 10   :       ",int(10 == 10)
print "10 >= 10   :       ",int(10 >= 10)
print "10 <= 100  :       ",int(10 <= 100)
print "10 != 9    :       ",int(10 != 9)

Condition  :   Return Values
10 != 10   :        0
10 == 10   :        1
10 >= 10   :        1
10 <= 100  :        1
10 != 9    :        1


### Example 3.8, Page number: 28

In :
#Program uses conditional operator to determine the value of 'b'depending the inputted value of 'a'

a = raw_input("Enter Any Integer either above 5 or below 5 :-")
a = int(a)

#In python, raw_input() is used to read values

#Calculation
#Assigns b = 3 if a > 5, otherwise b = 4
b = 3 if a > 5 else 4

#Result
print "Calculated Value of b is :- ",b

Enter Any Integer either above 5 or below 5 :-6
Calculated Value of b is :-  3


### Example 3.9, Page number: 28

In :
#Program to determine the value of 'b' using conditional operator

#Input

a = raw_input("Enter Any Integer either above 5 or below 5 :-")
a = int(a)

#In python, raw_input() is used to read values

#Calculation
#Assigns b = 3 if a == 5, otherwise b = 4
b = 3 if a == 5 else 4

#Result
print "Calculated Value of b is :- ",b

Enter Any Integer either above 5 or below 5 :-3
Calculated Value of b is :-  4


### Example 3.10, Page number: 29

In :
#Read three variables x, y and z and Using conditional statements, evaluate
#values of variables a, b and c.
#Perform the sum with two sets of variables.  Check whether the sums are equal or not
#and print appropriate messages.

#Reads three Input values for x,y and z

x = raw_input("Enter Value of x ")
x = int(x)

y = raw_input("Enter Value of y ")
y = int(y)

z = raw_input("Enter Value of z ")
z = int(z)

#In python, raw_input() is used to read values

#Calculation
#Assigns a = 3 if x >= 5, otherwise a = 4
a = 3 if x >= 5 else 4

#Result
print "Calculated Value of a is :- ",a

#Calculation
#Assigns b = 10 if y <= 8, otherwise b = 9
b = 10 if y <= 8 else 9

#Result
print "Calculated Value of b is :- ",b

#Calculation
#Assigns c = 20 if z == 10, otherwise c = 30
c = 20 if z == 10  else 30

#Result
print "Calculated Value of c is :- ",c

#Calculation
m = x + y + z
n = a + b + c

#Result
print "Addition of x,y,z is ",m,"(m)"
print "Addition of a,b,c is ",n,"(n)"
print "m & n NOT EQUAL" if m != n else "m & n ARE EQUAL"

Enter Value of x 5
Enter Value of y 2
Enter Value of z 7
Calculated Value of a is :-  3
Calculated Value of b is :-  10
Calculated Value of c is :-  30
Addition of x,y,z is  14 (m)
Addition of a,b,c is  43 (n)
m & n NOT EQUAL


### Example 3.11, Page number: 30

In :
#Use of logical operators

#Result
#In python, not equal to can be represented using both != and <>.

print "Condition        :   Return Values"
print "5 > 3 && 5 < 10  :   ",int(5 > 3 & 5 < 10)
print "8 > 5 || 8 < 2   :   ",int(8 > 5 | 8 < 2)
print "!(8 == 8)        :   ",int(8 != 8)

Condition        :   Return Values
5 > 3 && 5 < 10  :    1
8 > 5 || 8 < 2   :    0
!(8 == 8)        :    0


### Example 3.12, Page number: 30

In :
#Print logic 1 if input character is capital otherwise 0

#Import
import sys

#Variable initialization
#raw_input() function is used to read a character from the keyboard in Python 2.7
#ord() function converts the character into its corresponding ASCII integer

x = raw_input("Enter a Character : ")
x = ord(x)

#In python, raw_input() is used to read values

#Condition
y = 1 if (x >= 65) and (x <= 90) else 0

#Result
print "Y : ",y

Enter a Character : A
Y :  1


### Example 3.13, Page number: 31

In :
#Display logic 0 if one reads a character through keyboard otherwise logic 1.
#ASCII values for 0 to 9 are 48 to 57 respectively

#Variable initialization
#raw_input() function is used to read a character from the keyboard in Python 2.7
#ord() function converts the character into its corresponding ASCII integer

x = raw_input("Enter The Character or Integer : ")
x = ord(x)

#In python, raw_input() is used to read values

#Condition
y = 1 if (x >= 48) and (x <= 57) else 0

#Result
print "Value of Y = ",y

Enter The Character or Integer : A
Value of Y =  0


### Example 3.14, Page number: 32

In :
#Display 1 if inputted number is between 1-100 otherwise 0.
#Use the logical AND operator

#Variable initialization

x = raw_input("Enter numbers : ")
x = int(x)

#In python, raw_input() is used to read values

#Condition
z = 1 if (x >= 1) and (x <= 100) else 0

#Result
print "Z = ",z

Enter numbers : 5
Z =  1


### Example 3.15, Page number: 32

In :
#Display 1 if inputted number is either 1 or 100 otherwise 0.
#Use the logical OR operator

#Variable initialization

x = raw_input("Enter numbers : ")
x = int(x)

#In python, raw_input() is used to read values

#Condition
z = 1 if (x == 1) or (x == 100) else 0

#Result
print "Z = ",z

Enter numbers : 1
Z =  1


### Example 3.16, Page number: 33

In :
#Display 1 if inputted number is except 100 otherwise 0.
#Use the logical NOT operator

#Variable initialization

x = raw_input("Enter number : ")
x = int(x)

#In python, raw_input() is used to read values

#Condition
z = 1 if (x != 100) else 0

#Result
print "Z : ",z

Enter number : 100
Z :  0


### Example 3.17, Page number: 33

In :
#Shift inputted data by two bits right

#Variable initialization

x = raw_input("Read The Integer from keyboard (x) :- ")
x = int(x)

#In python, raw_input() is used to read values

#bitwise shifting
x >>= 2
y = x

#Result
print "The Right shifted data is = ",y

Read The Integer from keyboard (x) :- 8
The Right shifted data is =  2


### Example 3.18, Page number: 34

In :
#Shift inputted data by two bits to the left

#Variable initialization

x = raw_input("Read The Integer from keyboard (x) :- ")
x = int(x)

#In python, raw_input() is used to read values

#bitwise shifting
x <<= 3
y = x

#Result
print "The Left shifted data is = ",y

Read The Integer from keyboard (x) :- 2
The Left shifted data is =  16


### Example 3.19, Page number: 35

In :
#Use bitwise AND operator between the two integers and display the results

#Variable initialization

a = raw_input("Read The Integers from keyboard (a & b) :- ")
a = int(a)

b = raw_input("Read The Integers from keyboard (a & b) :- ")
b = int(b)

#In python, raw_input() is used to read values

#bitwise operation
c = a & b

#Result
print "The Answer after ANDing is (C) = ",c

Read The Integers from keyboard (a & b) :- 8
Read The Integers from keyboard (a & b) :- 4
The Answer after ANDing is (C) =  0


### Example 3.20, Page number: 37

In :
#operate OR operation on two integers and display the result

#Variable initialization

a = raw_input("Read The Integers from keyboard (a & b) :- ")
a = int(a)

b = raw_input("Read The Integers from keyboard (a & b) :- ")
b = int(b)

#In python, raw_input() is used to read values

#bitwise operation
c = a | b

#Result
print "The Oring operation betwen a & b in c = ",c

Read The Integers from keyboard (a & b) :- 8
Read The Integers from keyboard (a & b) :- 4
The Oring operation betwen a & b in c =  12


### Example 3.21, Page number: 38

In :
#Exclusive OR operation between the two integers and display the result

#Variable initialization

a = raw_input("Read The Integers from keyboard (a & b) :- ")
a = int(a)

b = raw_input("Read The Integers from keyboard (a & b) :- ")
b = int(b)

#In python, raw_input() is used to read values

#bitwise operation
c = a ^ b

#Result
print "The data after Exclusive OR operation is in c = ",c

Read The Integers from keyboard (a & b) :- 8
Read The Integers from keyboard (a & b) :- 2
The data after Exclusive OR operation is in c =  10

In [ ]: