9 : DECISION MAKING,BREAKING BRANCHING AND LOOPING

9.1.1,page number:156

In [1]:
print "Type an Integer"
numb=int(input())
if numb<=5:
    print "Good Choice!"
print "Thank You!"
Type an Integer
3
Good Choice!
Thank You!

9.2.1,page number:157

In [5]:
print "Enter year : "
year= int(input())
if year%4 == 0 and year%100 != 0 or year%400 == 0:
    print year,"is a leap year"
else:
    print year,"is not a leap year"
Enter year : 
1990
1990 is not a leap year

9.3.1,page number:157

In [5]:
print "Enter Year :"
year=int(input())
if year%4 == 0 :
    if year % 100 != 0:
        print year,"is leap year"
    else:
        if year%400 == 0:
            print year," is leap year"
else:
    print year,"is not a leap year"
Enter Year :
1990
1990 is not a leap year

9.3.2,page number:158

In [6]:
print "Enter the number"
numb =int(input())
if numb<=100:
    if numb>=30:
        print numb,"lies between 30-100"
    else:
        print numb,"is outside 30-100"
Enter the number
40
40 lies between 30-100

9.4.1,page number : 158

In [6]:
print "Enter choice of city(1-4)"
city = int(input())
if city == 1:
    print "Madurai"
elif city == 2:
    print "Chennai"
elif city == 3:
    print "Mumbai"
elif city == 4:
    print "Calcutta"
Enter choice of city(1-4)
3
Mumbai

9.5.1,page number:159

In [10]:
numb=int(input("Enter the number"))
if numb == 0:
    print "prints case0"
elif numb == 1:
    print "prints case1"
elif numb == 2:
    print "prints case2" 
else:
    print
Enter the number2
prints case2

9.6.1,page number : 161

In [7]:
import array
i =20
variable =array.array('i',[])
size = 0

def get_array_size():
    print "Array-size :"
    global size
    size=int(input())
    if size<1 or size>i :
        get_array_size()
    
def input_fetch():
    global size
    global variabless
    for k in range(0,size):
        m=int(input())
        variable.append(m)
    return

def sort_list():
    global variable
    variable= array.array('i',sorted(variable))
    
    return
    
def output():
    for k in range(0,size):
        print variable[k],
    return

def even(number):
    if number/2 == 0:
        return 1
    else:
        return 0

get_array_size()
input_fetch()
sort_list()
print "The sorted list is :"
output()
print "\nMedian : ",

median=round((variable[(size/2)-1]+variable[size/2])/2,3) if even(size)  else float(variable[size/2])
print median
Array-size :
10
7
6
8
-4
0
5
13
17
25
6
The sorted list is :
-4 0 5 6 6 7 8 13 17 25 
Median :  7.0

9.8.1,page number:163

In [8]:
index = 1
while index<12:
    print "This is line ",index
    index+=1
This is line  1
This is line  2
This is line  3
This is line  4
This is line  5
This is line  6
This is line  7
This is line  8
This is line  9
This is line  10
This is line  11

9.9.1,page number:164

In [9]:
print "Enter Integer"
total_sum=0
numb=int(input())
while numb>0:
    digit=numb %10
    total_sum+=digit
    numb/=10
print "Sum is ",total_sum
Enter Integer
1776
Sum is  21

9.9.2,page number:165

In [10]:
print "Enter Integer:"
numb=int(input())
print numb,"is reversed as ",
while numb>0:
    digit=numb%10
    print digit,
    numb/=10
print "\n"
Enter Integer:
1234
1234 is reversed as  4 3 2 1 

9.10.1,page number:166

In [2]:
for index in range(1,11):
    print " line ",index
 line  1
 line  2
 line  3
 line  4
 line  5
 line  6
 line  7
 line  8
 line  9
 line  10

9.10.2,page number:166

In [3]:
print "Result"
print "Index     Index Squared"
print "_____     _____________"
for  index in range(1,11):
    print index,"         ",index*index
Result
Index     Index Squared
_____     _____________
1           1
2           4
3           9
4           16
5           25
6           36
7           49
8           64
9           81
10           100

9.10.3,page number:167

In [4]:
total_sum=0
value=100
for index in range(1,value+1):
    total_sum+=index
print "Sum of 1 to ",value," = ",total_sum
Sum of 1 to  100  =  5050

9.11.1,page number:167

In [6]:
def exceptive():
    for index in range(1,90):
        if index%5 == 0:
            continue
        print index
exceptive()
1
2
3
4
6
7
8
9
11
12
13
14
16
17
18
19
21
22
23
24
26
27
28
29
31
32
33
34
36
37
38
39
41
42
43
44
46
47
48
49
51
52
53
54
56
57
58
59
61
62
63
64
66
67
68
69
71
72
73
74
76
77
78
79
81
82
83
84
86
87
88
89