Chapter 3 Statements

Program Source Code 3.1, page no: 65

In [1]:
numb = input("Enter a number")   #user input

print 'numb<10  is',int(numb < 10)
print 'numb>10  is',int(numb > 10)
print 'numb==10 is',int(numb == 10),'\n'
Enter a number20
numb<10  is 0
numb>10  is 1
numb==10 is 0 

Program Source Code 3.2, page no: 67

In [8]:
a=input( 'Please Enter two numbers, a: ')   #takes input
b=input('b: ')

if a>b:               #prints which is greater
    print a,
    print 'is greater'  
    print b,
    print 'is smaller'
Please Enter two numbers, a: 30
b: 20
30 is greater
20 is smaller

Program Source Code 3.3, page no: 69

In [3]:
a=input('Please Enter two numbers, a: ')   #takes input
b=input('b: ')
if a>b:               
    print a,
    print 'is greater'  
    print b,
    print 'is smaller'
else:
    print b,
    print 'is greater'  
    print a,
    print 'is smaller'
Please Enter two numbers, a: 20
b: 30
30 is greater
20 is smaller

Program Source Code 3.4, page no: 71

In [9]:
print 'Enter three number, a, b, c:'   #user input
a = input()
b = input()
c = input()

if a==b:
    if b==c:
        print 'a, b, and c are equal\n'
   
else:
    print 'a and b are different\n'
Enter three number, a, b, c:
20
30
30
a and b are different

Program Source Code 3.5, page no: 75

In [10]:
marks=input('Please input the marks obtained out of 100: ')       #user input
if marks>=0 and marks<=100:    #prints grade according to marks
    range=marks / 10
    print 'Equivalent letter Grade Award is : ',
    if range<=10 and range>=8:
        print ' A'
       
    elif range<8 and range>=7:
        print ' B'
      
    elif range<7 and range>=6:
        print ' C'
        
    elif range<6:
        print ' D'        
else:
    print 'Incorrect range: '
    print 'Marks should be within 0 to 100'
Please input the marks obtained out of 100: 76
Equivalent letter Grade Award is :   B

Program Source Code 3.6, page no: 80

In [4]:
count=input('Enter the value of count: ')
while count>0:               #while loop      
    print 'count=',count
    count-=1
print 'done'
Enter the value of count: 5
count= 5
count= 4
count= 3
count= 2
count= 1
done

Program Source Code 3.7, page no: 81

In [1]:
count=input('Enter the value of count: ')
#no do while loop in python
while True:
     print 'count=',count
     count-=1
     if count==0:        #fail condition
         break
print 'done'
Enter the value of count: 5
count= 5
count= 4
count= 3
count= 2
count= 1
done

Program Source Code 3.8, page no: 82

In [2]:
for i in range(15):
   print i*i,       #displaying
0 1 4 9 16 25 36 49 64 81 100 121 144 169 196

Program Source Code 3.9, page no: 85

In [4]:
count=input('Enter the value of count: ')       #user input
for x in range(count):         #for loop
    print 'count=',count
    count-=1
print 'done'
Enter the value of count: 5
count= 5
count= 4
count= 3
count= 2
count= 1
done

Program Source Code 3.10, page no: 86

In [8]:
print 'count up'
for i in range(5):                #for loop
     print i
print 'count down'
i+=1
for i in range(5,-1,-1):
    print i

print 'done'
count up
0
1
2
3
4
count down
5
4
3
2
1
0
done

Program Source Code 3.11, page no: 87

In [12]:
for i in range(5):
    print i               #displaying
0
1
2
3
4

Program Source Code 3.12, page no: 88

In [1]:
count=input('Enter the value of count: ')           #user input
while True:                #while loop
   if count<=0:
     break
   print 'count= ',count
   count-=1
print 'done'
Enter the value of count: 5
count=  5
count=  4
count=  3
count=  2
count=  1
done

Program Source Code 3.13, page no: 89

In [3]:
count1 = input("Enter the value of count: ")     #user input

for count1 in range(count1,0,-1):
    if count1 == 3:
        continue
    print 'count =',count1
    
print 'done'
Enter the value of count: 5
count = 5
count = 4
count = 2
count = 1
done
In [ ]: