Chapter 4: Constants, Variables & Datatypes

example 4.1, page no. 50

In [1]:
intnum = int(raw_input("Enter an Integer: "))
floatnum = float(raw_input("Enter a Float: "))

print "intnum = ", intnum
print "floatnum = ", floatnum
Enter an Integer: 3
Enter a Float: 6.9
intnum =  3
floatnum =  6.9

example 4.2, page no. 54

In [1]:
print "Variables Created"
c = "x"
b = 50
s = 1996
i = 123456789
l = 1234567654321L
f1 = 3.142
f2 = 1.2e-5
d2 = 0.000000987

print "c = ", c
print "b = ", b
print "s = ", s
print "i = ", i
print "l = ", l
print "f1 = ", f1
print "f2 = ", f2
print "d2 = ", d2

print "Types Converted"
#there is no short type in python
s1 = b
s2 = i
n1 = float(l)
m1 = int(f1)

print "s1 = ", s1
print "s2 = ", s2
print "n1 = %.5e"% n1
print "m1 = ", m1
Variables Created
c =  x
b =  50
s =  1996
i =  123456789
l =  1234567654321
f1 =  3.142
f2 =  1.2e-05
d2 =  9.87e-07
Types Converted
s1 =  50
s2 =  123456789
n1 = 1.23457e+12
m1 =  3

example 4.3, page no. 56

In [2]:
for i in range(1, 10):
    for j in range(1, i+1):
        print i,
    print ""
print "Screen Display Done"
1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 
6 6 6 6 6 6 
7 7 7 7 7 7 7 
8 8 8 8 8 8 8 8 
9 9 9 9 9 9 9 9 9 
Screen Display Done
In [ ]: