# Chapter 2 : Performing operations¶

## Example 2.1, Page No : 27¶

In [2]:
a = 8
b = 4
print "Substraction result: ",(a-b)
print "Multiplication result: ",(a*b)
print "Division result: ",(a/b)
print "Modulus result: ",(a%b)
print "Postfix increment: ",a+1
print "Postfix result: ",a
print "Prefix increment: ",b+1
print "Prefix result: ",b

Addition result:  12
Substraction result:  4
Multiplication result:  32
Division result:  2
Modulus result:  0
Postfix increment:  9
Postfix result:  8
Prefix increment:  5
Prefix result:  4


## Example 2.2, Page No : 29¶

In [8]:
a = 8
b = 4
print "Assigned values: "
print "a = ",a
print "b = ",b
a = a + b
print "a += b (8 += 4 ) a = ",a
print "Subtract & assign: "
a = a - b
print "a -= b (12 -= 4 ) a = ",a
print "Multiply & assign: "
a = a * b
print "a *= b (8 *= 4 ) a = ",a
print "Divide & assign: "
a = a / b
print "a /= b (32 /= 4 ) a = ",a
print "Modulus & assign: "
a = a % b
print "a %= b (8 %= 4 ) a = ",a

Assigned values:
a =  8
b =  4
a += b (8 += 4 ) a =  12
Subtract & assign:
a -= b (12 -= 4 ) a =  8
Multiply & assign:
a *= b (8 *= 4 ) a =  32
Divide & assign:
a /= b (32 /= 4 ) a =  8
Modulus & assign:
a %= b (8 %= 4 ) a =  0


## Example 2.3, Page No 31¶

In [10]:
nil= 0
num= 0
max1 = 1
cap= 'A'
low= 'a'
print "Equality comparisons: "
print "(0 == 0) ",(nil == num),"(true)"
print "(A == a) ",(cap == low),"(false)"
print "Inequality comparison: "
print "(0 != 1) ",(nil != max1),"(true)"
print "Greater comparison: "
print "(0 > 1)",(nil > max1),"(false)"
print "Lesser comparison: "
print "(0 < 1) ",(nil < max1),"(true)"
print "Greater or equal comparison: "
print "(0 >= 0) ",(nil >= num),"(true)"
print "Lesser or equal comparison: "
print "(1 <= 0) ",(max1 <= num),"(false)"

Equality comparisons:
(0 == 0)  True (true)
(A == a)  False (false)
Inequality comparison:
(0 != 1)  True (true)
Greater comparison:
(0 > 1) False (false)
Lesser comparison:
(0 < 1)  True (true)
Greater or equal comparison:
(0 >= 0)  True (true)
Lesser or equal comparison:
(1 <= 0)  False (false)


## Example 2.4, Page No : 33¶

In [13]:
a = 1
b = 0
print "AND logic: "
print "(a && a) ",(a & a),"(true)"
print "(a && b) ",(a & b),"(false)"
print "(b && b) ",(b & b),"(false)"
print "OR logic: "
print "(a || a) ",( a | a ),"(true)"
print "(a || b) ",( a | b ),"(true)"
print "(b || b) ",( b | b ),"(false)"
print "NOT logic: "
print "a = ",a,"!a = ",not a
print "b = ",b,"!b = ",not b

AND logic:
(a && a)  1 (true)
(a && b)  0 (false)
(b && b)  0 (false)
OR logic:
(a || a)  1 (true)
(a || b)  1 (true)
(b || b)  0 (false)
NOT logic:
a =  1 !a =  False
b =  0 !b =  True


## Examle 2.5, Page No : 35¶

In [15]:
a = 1
b = 2
print "Variable a value is: "
if a != 1:
print "not one"
else:
print "one"
if a % 2 != 0:
print "odd"
else:
print "even"
print "Variable b value is: "
if b != 1:
print "not one"
else:
print "one"
if b % 2 != 0:
print "odd"
else:
print "even"
if a > b:
max1 = a
else:
max1 = b
print "Greater value is ",max1

Variable a value is:
one
odd
Variable b value is:
not one
even
Greater value is  2


## Example 2.6, Page No 37¶

In [2]:
# there are no few datatypes in ipython so i assigned manually to get idea about size
import sys
num = 1
nums = range(50)
number = 0
max1 = 100
pi = 3.14
decimal = 10.10
letter = 'a'
letters = "hii"
isTrue = "true"
print "int size: ",sys.getsizeof(num)
print "50 int size: ",sys.getsizeof(nums)
print "short int size: ",sys.getsizeof(number)
print "unsigned int size: ",sys.getsizeof(max1)
print "double size: ",sys.getsizeof(pi)
print "float size: ",sys.getsizeof(decimal)
print "char size: ",sys.getsizeof(letter)
print "50 char size: ",sys.getsizeof(letters)
print "bool size: ",sys.getsizeof(isTrue)

 int size:  24
50 int size:  464
short int size:  24
unsigned int size:  24
double size:  24
float size:  24
char size:  34
50 char size:  36
bool size:  37


## Example 2.7, Page No 39¶

In [4]:
num = 1 + 4 * 3
print "Default order: ",num
num = (1 + 4) * 3
print "Forced order: ",num
num = 7 - 4 + 2
print "Default direction: ",num
num = 7 - (4 + 2)
print "Forced direction: ",num

Default order:  13
Forced order:  15
Default direction:  5
Forced direction:  1


## Example 2.8, Page No 41¶

In [9]:
num= 7
factor= 2
letter= 'A'
result= 0.0;
print "Integer division: ",(num/factor)
result = float(num) / factor
print "Cast division float: ",result
num =  ord(letter)
print "Cast character int: ",num
letter = chr(70)
print "Cast integer char: ",letter

Integer division:  3
Cast division float:  3.5
Cast character int:  65
Cast integer char:  F