Example 2.1, page no. 17¶

In [1]:
flag=False
print "flag = %r" % flag
flag = True
print "flag = %r" % flag

flag = False
flag = True


Example 2.2, page no. 19¶

In [2]:
c='A'
print "c = " + c + ", int(c) = %d" % ord(c)
c='t'
print "c = " + c + ", int(c) = %d" % ord(c)
c='\t' # the tab character
print "c = " + c + ", int(c) = %d" % ord(c)
c='!'
print "c = " + c + ", int(c) = %d" % ord(c)

c = A, int(c) = 65
c = t, int(c) = 116
c = 	, int(c) = 9
c = !, int(c) = 33


Example 2.3, page no. 19¶

In [3]:
import sys
# defines the constants SHRT_MIN, etc.
print 'maximum limit int : ',
print sys.maxint
print 'float info'
print sys.float_info

maximum limit int :  2147483647
float info
sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)


Example 2.4, page no. 21¶

In [1]:
m=54
n=20
print "m = %d and n = %d" %(m,n)
print "m+n = %d" % (m+n) # 54+20 = 74
print "m-n = %d" % (m-n)  # 54-20 = 34
print "m*n = %d" % (m*n)# 54*20 = 1080
print "m/n = %d" % (m/n) # 54/20 = 2
print "m modulo by n = %d" % (m%n)  # 54%20 = 14

m = 54 and n = 20
m+n = 74
m-n = 34
m*n = 1080
m/n = 2
m modulo by n = 14


Example 2.5, page no. 21¶

In [5]:
m = 44
m += 1
n = m
print "m = %d , n = %d" %(m,n)
m = 44
n = m # the post-increment operator is applied to m
m += 1
print "m = %d , n = %d" %(m,n)

m = 45 , n = 45
m = 45 , n = 44


Example 2.6, page no. 22¶

In [6]:
n=22
print  "n = %d" % n
n += 9 # adds 9 to n
print  "After n += 9, n = %d" % n
n -= 5 # subtracts 5 from n
print  "After n -= 5, n = %d" % n
n *= 2 # multiplies n by 3
print  "After n *= 2, n = %d" % n
n /= 3 # divides n by 9
print  "After n /= 3, n = %d" % n
n %= 7 # reduces n to the remainder from dividing by 4
print 'After n modulo by 7   n = %d' %n

n = 22
After n += 9, n = 31
After n -= 5, n = 26
After n *= 2, n = 52
After n /= 3, n = 17
After n modulo by 7   n = 3


Example 2.7, page no. 23¶

In [2]:
x=54.0
y=20.0
print  "x = %f and y = %f" %(x,y)
print  "x+y = %f"  %(x+y) # 54.0+20.0 = 74.0
print  "x-y = %f" % (x-y) # 54.0-20.0 = 34.0
print  "x*y = %f" %( x*y) # 54.0*20.0 = 1080.0
print  "x/y = %f" % (x/y) # 54.0/20.0 = 2.7

x = 54.000000 and y = 20.000000
x+y = 74.000000
x-y = 34.000000
x*y = 1080.000000
x/y = 2.700000


Example 2.8, page no. 23s¶

In [8]:
import sys

print "Number of bytes used:\n"

print " char: %d " % sys.getsizeof('a')
print " int : %d " % sys.getsizeof(int(1))
print " string : %d " % sys.getsizeof(str('hellololdei'))
print "float : %d" % sys.getsizeof(float(1.1))

Number of bytes used:

char: 22
int : 12
string : 32
float : 16


Example 2.9, page no. 24¶

In [9]:
import sys

fbits = 8*sys.getsizeof(float(123))

# each byte contains 8 bits
print "float uses : %d bits:\n\t" %  fbits

float uses : 128 bits:



Example 2.10, page no. 25¶

In [10]:
v = 1234.56789
n = int(v);
print "v = %f, n = %d" %(v,n)

v = 1234.567890, n = 1234


Example 2.11, page no. 26¶

In [11]:
c='A'
print "char c = " + c
k=c;
print "k = " + k
m=k;
print "m = " + m
n=m
print "n = " + n
x=m
print "x = " + x
y=x
print "y = " + y

char c = A
k = A
m = A
n = A
x = A
y = A


Example 2.12, page no. 27¶

In [12]:
n=1000
print "n =  %d" % n
n *= 1000 # multiplies n by 1000
print "n =  %d" % n
n *= 1000 # multiplies n by 1000
print "n =  %d" % n
n *= 1000 # multiplies n by 1000
print "n =  %d" % n

n =  1000
n =  1000000
n =  1000000000
n =  1000000000000


Example 2.13, page no. 27¶

In [13]:
x=1000.0
print "x = %f" % x
x *= x # multiplies n by itself; i.e., it squares x
print "x = %f" % x
x *= x # multiplies n by itself; i.e., it squares x
print "x = %f" % x
x *= x # multiplies n by itself; i.e., it squares x
print "x = %f" % x
x *= x # multiplies n by itself; i.e., it squares x
print "x = %f" % x

x = 1000.000000
x = 1000000.000000
x = 1000000000000.000000
x = 999999999999999983222784.000000
x = 1000000000000000043845843045076197354634047651840.000000


Example 2.14, page no. 28¶

In [14]:
x = 1000/3.0
print "x = %f" %x # x = 1000/3
y = x - 333.0
print "y = %f" % y # y = 1/3
z = 3*y - 1.0
print "z = %f" %z # z = 3(1/3) - 1
if (z == 0):
print "z == 0.\n"
else:
print "z does not equal 0.\n" # z != 0

x = 333.333333
y = 0.333333
z = -0.000000
z does not equal 0.



Example 2.15, page no. 28¶

In [15]:
import math

a = float(raw_input("Enter the coefficients of a quadratic equation:\n a : "))
b = float(raw_input('b : '))
c = float(raw_input('c : '))

print "The equation is: ",
print a,
print "*x*x + ",
print b,
print "*x + " ,
print c,
print  " = 0"

d = b*b - 4*a*c # discriminant
sqrtd = math.sqrt(d)
x1 = (-b + sqrtd)/(2*a)
x2 = (-b - sqrtd)/(2*a)
print "The solutions are:"
print "\tx1 = %f" % x1
print "\tx2 = %f" % x2
print "Check:"
print "\ta*x1*x1 + b*x1 + c = %f" %( a*x1*x1 + b*x1 + c)
print "\ta*x2*x2 + b*x2 + c =  %f" %( a*x2*x2 + b*x2 + c)

Enter the coefficients of a quadratic equation:
a : 2
b : 1
c : -3
The equation is:  2.0 *x*x +  1.0 *x +  -3.0  = 0
The solutions are:
x1 = 1.000000
x2 = -1.500000
Check:
a*x1*x1 + b*x1 + c = 0.000000
a*x2*x2 + b*x2 + c =  0.000000


Example 2.17, page no. 31¶

In [16]:
x = float(raw_input("Enter float: "))
print "Its reciprocal is: ",
print  1/x

Enter float: 234.567e89
Its reciprocal is:  4.2631742743e-92


Example 2.18, page no. 31¶

In [17]:
x = 11
# ERROR: this is not in the scope of x
if True:
x = 22 # OK: this is in the scope of x
y = 33 # ERROR: this is not in the scope of y
x = 44 # OK: this is in the scope of x
y = 55 # OK: this is in the scope of y
x = 66 # OK: this is in the scope of x
y = 77 # ERROR: this is not in the scope of y


Example 2.19, page no. 32¶

In [18]:
# this x is global
x = 11

if True:
# illustrates the nested and parallel scopes:
x = 22
# begin scope of internal block
if True:
x = 33
print "In block inside main(): x =  %d " % x
# end scope of internal block
print "In main(): x =  %d" %x
print "In main(): x = %d "% x

In block inside main(): x =  33
In main(): x =  33
In main(): x = 33