Chapter 4, Some More Data Types

Prgoram 4-1 , Page number: 47

In [1]:
# An illustration of the use of floating point numbers

# There is no loss of precision in python
a = 246.8
b = 135.79

answer = a + b
print "%f + %f = %f"%(a,b,answer)

answer = a - b
print "%f - %f = %f"%(a,b,answer)

answer = a * b
print "%f * %f = %f"%(a,b,answer)

answer = a / b
print "%f / %f = %f"%(a,b,answer)
246.800000 + 135.790000 = 382.590000
246.800000 - 135.790000 = 111.010000
246.800000 * 135.790000 = 33512.972000
246.800000 / 135.790000 = 1.817512

Program 4-2 , Page number: 49

In [2]:
# Illustration of mixed mode expressions

a = 2
b = 12.34

result = a * b
print "%d * %f = %f"%(a,b,result)

print "%f in scientific notation is %e"%(result,result)

answer = a * b
print "%d * %f = %d"%(a,b,answer)
2 * 12.340000 = 24.680000
24.680000 in scientific notation is 2.468000e+01
2 * 12.340000 = 24

Program 4-3 , Page number: 52

In [3]:
ltr_1 = "C"
ltr_2 = " "
ltr_3 = "i"
ltr_4 = "s"
ltr_5 = "g"
ltr_6 = "r"
ltr_7 = "e"
ltr_8 = "a"
ltr_9 = "t"
ltr_10 = "!"

print "%c%c%c%c%c%c%c%c%c%c%c"%(ltr_1,ltr_2,ltr_3,ltr_4,ltr_2,ltr_5,ltr_6,ltr_7,ltr_8,ltr_9,ltr_10)
C is great!