Chapter 11 Conditional and Looping Constructs Solutions
Question - 41 : - Explain “elif statement” with example.
Answer - 41 : -
elif statement : The elif statement allows you to check multiple expressions for truth value and execute a block of code as soon as one of the conditions evaluates to true. Like the else, the elif statement is optional. However, unlike else , for which there can be at most one statement, there can be an arbitrary number of elif statements following an if
The syntax of the if…elif statement is
If expression1 :
statement (s)
elif expression2 :
statement(s)
elif expression3 :
statement(s)
else:
statement(s)
Example
# !/usr/bin/py thon
var=100
if var = = 200 :
print “1-Got a true expression value”
print var
elif var==150:
print “2-Got a true expression value”
print var2
elif var ==100:
print “3-Got a true expression value”
print var
else:
print “4-Got a false expression value”
print var
print “Good bye!”
when the above code is executed, it produces the following output :
3- Got a true expression value
100
Good bye !
Question - 42 : - Explain nested if—–else statement
Answer - 42 : -
There may be a situation when you want to check for another condition after a condition resolves to true. In such a situation, you can use the nested if construct. In a nested if construct, you can have an if…elif…else construct inside another if…elif… else construct.
Syntax:
The syntax of the nested if…elif…else construct may be:
if expression1:
statement(s)
if expression2:
statement(s)
elif expression3:
statement(s)
else
statement(s)
elif expression4: statement(s) else:
statement(s)
Example:
# !/usr/bin/python if var < 200:
print “Expression value is less than 200”
if var==150:
print “=150”
elif var = =100:
print “=100”
elif var ==50:
print “=50”
elif var < 50 :
print “Expression value is less than 50”
else :
print “Could not find true expression”
Question - 43 : - Explain Decision making structure with diagram.
Answer - 43 : -
Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
Following is the general from of a typical decision making structure found in most of the programming languages :
Python programminglanguage assumes any non-zero and non null values as true and if it is eitherzero or null then it is assumed as false value.
Python programming language provides follo-wing types of decision makingstatements.
Statement | Description |
if statements | An if statement consists of a boolean expression followed by one or more statements |
if…else statements | An if statement can be followed by an optional else statement,which executes when the boolean expression is false |
nested if statement | You can use one if or else if statement inside another if or else if statement(s) |
Question - 44 : - Write a function to print the Fibonacci Series upto input limit
Answer - 44 : -
def fibonacci (num)
if (num = = 0):
return 0
elif (num = = 1):
return 1
else :
return fibonacci (num-1) + fibonacci
(num-2)
cal = input (“Enter the input limit”)
print fibonacci (val)
Question - 45 : - Print all multiples of 13 that are smaller than 100. Use the range function in the following manner : range (start, end, step) where ‘Start’ is the starting value of the counter, ‘end’ is the end value and ‘step’ is the amount by which the counter is increased each time.
Answer - 45 : -
# program to print multiples of 13 smaller than 100 for counter in range (13,100,13) :
print counter
Question - 46 : - Write a program using while loop that asks the user for a number, and prints a countdown from that number to zero.
Answer - 46 : -
num = input (“Enter the number”)
while (num > = 1) :
print (“num = “, num)
num – num-1
print (“Finish”)
Question - 47 : - Using random module, Simulate tossing a coin N times.
Answer - 47 : -
N = input (“Number of tossing a coin”) from random import choice
heads = 0
tails = 0
coin = [‘head’, ‘tail’]
for n in range (N) :
toss = choice (coin)
if toss = ‘head’ :
heads = heads + 1
if toss = = ‘tail’ :
tails = tails + 1
print (‘Heads:’, heads)
print (‘Tails:’, tails)
Question - 48 : - Write a Python script to generate divisors of a number.
Answer - 48 : -
num = int (raw_input (“Enter an integer:”))
mid = num/2
print “The divisors of”, num, “are:”
for a in range (2, mid +1):
if num %a = = 0:
print a,
else:
print”End”
Question - 49 : - Write a Python script that display 20 Mersenne Prime Numbers ?
Answer - 49 : -
def Mersenne (n):
return 2** n-1
def test prime (n):
mid = n/2 + 1
for t in range (2, mid):
if n%t = = 0:
return false
else :
return true
print “20 Mersenne Prime Numbers are :”
for a in range (1,21):
number = Mersenne (a)
prime = test prime (number)
if prime :
print number, “Prime”
else :
print number
Question - 50 : - Write a Python script to find sum of the series :
s = 1 + x + x2 + x3 + …. + xn
Answer - 50 : -
x = float (raw_input (“Enter value of x :”)
n = int (raw_input (“Enter value of n :”))
s = 0
for a in range (n + 1) :
s + = x**a
print “Sum of series”, s