Chapter 03 : Decision Making

Example 1, Page No.:4.79

In [1]:
#Program using if with multiple statements


#Variable Declaration
i=0

#User Input

i =int(raw_input("Give the numbers less than 10"'\n'))

if i<=10:                  #Checking the given value

#Result       
    print "The given value of", i , "less than 10"
    
Give the numbers less than 10
5
the given value of 5 less than 10

Example: 2, Page No.: 4.79

In [2]:
##Program for interchange two values using if statement

#Variable Declaration

a=m=n=0

#User Input

m=int(raw_input("Give the value of m" '\t'))
n=int(raw_input("Give the value of n" '\t'))

if m>=n :                   #Checking the Given Values
    
    a=m                     # method for swapping            
    m=n
    n=a

#Result

print "The Swapped Values of m and n are ", m,a
Give the value of m	34
Give the value of n	28
The Swapped Values of m and n are  28 34

Example: 3, Page No. 4.81

In [4]:
#determine the given number is odd or even. 

#Variable Declaration
num=rem=0

#User Input
num=input("Enter the value: ")
rem=num%2           
if rem==0 :             #Checking condition

#Result 1
    print "The given number is Even"
else :
    
#Result 2
    print "The given number is Odd"
    
    
Enter the value: 80
The given number is Even

Example: 4, Page No.:4.81

In [7]:
# Find floating point input using if-else statement

#Variable Declaration

years=seconds=0.0
life=0

years=float(raw_input("Give your Age in years: "))
life=years
if life==0:
    print "The given age is not floating value"
    
else :
    seconds="%f"%float(years*365*24*60*60)
    print "You have lived for", seconds, "seconds"
Give your Age in years: 24
You have lived for 756864000.000000 seconds

Example: 5, Page No. :4.82

In [11]:
# find biggest among two numbers 

#Variable Declaration

i=j=big=0
i,j=input("Give two values: ")

big=i

if big<j :
    big=j
    
elif i<j :
    big=j 
    
else:
    big = i

print "Biggest number of two numbers: ",big
print "Biggest of two numbers(using else) is: ",big
Give two values: 45,78
Biggest number of two numbers:  78
Biggest of two numbers(using else) is:  78

Example: 6, Page No. : 4.84

In [12]:
# Program using nested if....else 

#Variable Declaration

n=0

#User Input
n=int(raw_input("Enter the number"))

if n==15:           #Checking Condition
    print "Play Foot ball"
    
elif n==10:
    print "Play Cricket"

else :    
    print "Don't Play" 
Enter the number10
Play Cricket

Example: 7, Page No. :4.85

In [14]:
#print the number between 10 and 20

#Variable declaration

i=0

#User Input

i=input("Enter the number: ")

if (i>10) & (i<20):
    print "The given value is in between 10 and 20"

else :
    print "The given value is greater than 20 and less than 10"    
Enter the number: 15
The given value is in between 10 and 20

Example: 8, Page No. :4.86

In [16]:
# display the types of characterusing if-else statement

#User Input

chr= raw_input("Enter a Single Character: ")

if( ((chr>='a') & (chr<='z')) | ((chr>='A') & (chr<='Z'))):
    print "Given charactered is an Alphabetic"
    
elif ((chr>='0') & (chr <='9')):
    print "Given Character is a digit"
    
else:
    print "Entered Character is a special character"
    
Enter a Single Character: M
Given charactered is an Alphabetic