Chapter 3 : Making statements

Example 3.1, Page No 45

In [3]:
num = 8
letter = 'A'
if num > 5:
     print "Number exceeds five"
else: 
    "Number is five or less"
if letter == 'A':
    print "Letter is A"
Number exceeds five
Letter is A

Example 3.2, Page No 47

In [4]:
num = 3
if num == 1:
    print num," : Monday"
elif num == 2:
    print num," : Tuesday"
elif num == 3:
    print num," : Wednesday"
elif num == 4:
    print num," : Thursday"
elif num == 5:
    print num," : Friday"
else:
    print "Weekend day"
3  : Wednesday

Example 3.3, Page No 48

In [11]:
for i in range(1,4):
    print "Loop iteration: ",i
    for j in range(1,4):
        print "      Inner loop iteration: ",j
Loop iteration:  1
      Inner loop iteration:  1
      Inner loop iteration:  2
      Inner loop iteration:  3
Loop iteration:  2
      Inner loop iteration:  1
      Inner loop iteration:  2
      Inner loop iteration:  3
Loop iteration:  3
      Inner loop iteration:  1
      Inner loop iteration:  2
      Inner loop iteration:  3

Example 3.4, Page No 50

In [17]:
vec = range(10)
i = 0
print 
while i < len(vec):
    i =  i + 1
    if i == 3:
        print "| Skipped",
        continue
    if i == 8:
        print "Done"
        break
    vec[i-1] = i
    print "|",vec[i-1],
| 1 | 2 | Skipped | 4 | 5 | 6 | 7 Done

Example 3.5, Page No 53

In [18]:
def bodyTempC():
    temperature = 37.0
    return temperature
def bodyTempF():
    temperature = 98.6
    return temperature
print "Centigrade: ",bodyTempC()
print "Fahrenheit: ",bodyTempF()
Centigrade:  37.0
Fahrenheit:  98.6

Example 3.6, Page No 54

In [21]:
def fToC(degreesF = 32.0):
    degreesC = ((5.0/9.0) * (degreesF - 32.0))
    return degreesC
fahrenheit = float(raw_input("Enter a Fahrenheit temperature:\t"))
centigrade = fToC(fahrenheit)
print fahrenheit,"F is ",centigrade,"C"
print "Freezing point: ",fToC(),"C"
Enter a Fahrenheit temperature:	98.6
 98.6 F is  37.0 C
Freezing point:  0.0 C

Example 3.7, Page No 56

In [4]:
def computeArea(diameter):
    radius = (diameter/2)
    return (3.141593 * (radius * radius))
def computeArea(width,height):
    return width * height
def computeArea(letter,width,height):
    return ((width/2) * height)
num = float(raw_input("Enter dimension in feet: "))
# python does not support overriding in this way
#area = computeArea(num)
#print "Circle: Area = ",area," sq.ft."
#area= computeArea(num,num)
#print"Square: Area = ",area," sq.ft."
area = computeArea('T',num,num)
print "Triangle: Area = ",area," sq.ft."
Enter dimension in feet: 4
 Triangle: Area =  8.0  sq.ft.

Example 3.8, Page No 58

In [28]:
def computeFactorials(num,max1):
    print "Factorial of ",num,":",
    print factorial(num)
    num = num + 1
    if num > max1:
        return 0
    else :
        computeFactorials(num,max1)
def factorial(n):
    if n == 1:
        result = 1
    else:
        result = (factorial(n - 1) * n)
    return result
computeFactorials( 1, 8) ;
Factorial of  1 : 1
Factorial of  2 : 2
Factorial of  3 : 6
Factorial of  4 : 24
Factorial of  5 : 120
Factorial of  6 : 720
Factorial of  7 : 5040
Factorial of  8 : 40320