Hour 9: Working with data and Math functions

Example 9.1, Page No.144

In [17]:
#Value in the textbook for 0xFF is wrong
ch = "0xFF"
x = "0xFFFF"
y = "0xFFFF"
print "The decimal of signed 0xFF is ", int(ch, 16)
print "The decimal of signed 0xFFFF is ", int(x, 16)
print "The decimal of unsigned 0xFFFF is ", int(y, 16)
print "The hex of decimal 12345 is 0x%x" %(12345)
#answer in textbook for -12345 is wrong
print "The hex of decimal -12345 is 0x%x" %(-12345)
The decimal of signed 0xFF is  255
The decimal of signed 0xFFFF is  65535
The decimal of unsigned 0xFFFF is  65535
The hex of decimal 12345 is 0x3039
The hex of decimal -12345 is 0x-3039

Example 9.2, Page No.146

In [21]:
import sys
print "The size of short int is:",sys.getsizeof(65535)
print "The size of long int is:",sys.getsizeof(65535*65535)
print "The size of float is:",sys.getsizeof(0.0)
# As python has no datatype like double, so that portion of program has not writeen here.
The size of short int is: 12
The size of long int is: 18
The size of float is: 16

Exmaple 9.3, Page No.147

In [30]:
import array
x=0xFFFF
y=0xFFFF
s=0xFFFFFFFFl
t=0xFFFFFFFFL
print "The short int of 0xFFFF is {:d}".format(x)
print "The unsigned int of 0xFFFF is {:d}".format(y)
print "The long int of 0xFFFFFFFFl is {:d}".format(s)
print "The long int of 0xFFFFFFFFl is {:d}".format(t)
The short int of 0xFFFF is 65535
The unsigned int of 0xFFFF is 65535
The long int of 0xFFFFFFFFl is 4294967295
The long int of 0xFFFFFFFFl is 4294967295

Example 9.4, Page No.150

In [33]:
import math
x=45.0
x=x*3.141593/180.0
print "The sine of 45 is: ",math.asin(x)
print "The sine of 45 is: ",math.acos(x)
print "The tangent of 45 is: ",math.atan(x)
The sine of 45 is:  0.903339250676
The sine of 45 is:  0.667457076119
The tangent of 45 is:  0.665773803591

Example 9.5, Page No.151

In [36]:
import math
x=64.0
y=3.0
z=0.5
print "pow(64.0,3.0) returns {:7.0f}".format(math.pow(x,y))
print "sqrt(64.0) returns {:2.0f}".format(math.sqrt(x))
print "pow(64.0,0.5) returns {:2.0f}".format(math.pow(x,z))
pow(64.0,3.0) returns  262144
sqrt(64.0) returns  8
pow(64.0,0.5) returns  8