HOUR 6 : Manipulating Data

Example 6.1, Page No 94

In [8]:
x = 1
y = 3
z = 10
print "Given " ,x,y,z,"\n"
x = x + y
print "x = x + y assigns " + str(x) + "to x\n"
x = 1
x += y
print "x += y assigns " +  str(x) + "to x\n"
x = 1
z = z * x + y
print "z = z * x + y assigns" + str(z) + "to z\n"
z = 10
z = z * (x + y)
print "z = z * (x + y) assigns" + str(z) + "to z\n"
z = 10
z *= x + y
print "z *= x + y assigns" + str(z)  + "to z\n"
Given  1 3 10 

x = x + y assigns 4to x

x += y assigns 4to x

z = z * x + y assigns13to z

z = z * (x + y) assigns40to z

z *= x + y assigns40to z

Example 6.2, Page No 97

In [5]:
#there is no concept of pre-increment or post-increment in Python.
w=x=y=z=1
print "Given w = ",w,"x = ",x,"y = ",y,"z = ",z,"\n"
w+=1
result=w
print "++w evaluates to " ,result,"and w is now ",w,"\n"
result = x
x = x + 1
print "x++ evaluates to ",result," and x is now ",x,"\n"
y-=1
result = y
print "--y evaluates to ",result," and y is now ",y,"\n"
result = z
z -= 1
print "z-- evaluates to ",result," and z is now ",z,"\n"
Given w =  1 x =  1 y =  1 z =  1 

++w evaluates to  2 and w is now  2 

x++ evaluates to  1  and x is now  2 

--y evaluates to  0  and y is now  0 

z-- evaluates to  1  and z is now  0 

Example 6.3, Page No 99

In [9]:
x = 7
y = 25
z = 24.46
print "Given x = ",x,"y = ",y," and z = ",z,"\n"
print "x >= y produces: ",x >= y,"\n"
print "x == y produces: ",x == y,"\n"
print "x < z produces: ",x < z,"\n"
print "y > z produces: ",y > z,"\n"
print "x != y - 18 produces: ",x != y - 18,"\n"
print "x + y != z produces: ",x + y != z
Given x =  7 y =  25  and z =  24.46 

x >= y produces:  False 

x == y produces:  False 

x < z produces:  True 

y > z produces:  True 

x != y - 18 produces:  False 

x + y != z produces:  True

Example 6.4, Page No 101

In [10]:
x = 7
y = 5
print "Given x = ",x,"y = ",y,"\n"
print "x / y produces: ",x/y,"\n"
print "(float)x / y produces: ", float(x)/y
Given x =  7 y =  5 

x / y produces:  1 

(float)x / y produces:  1.4