This page contains the CBSE Computer Science with Python class 11 Unit-3 chapter 3 Conditional And Looping Construct. You can find the solutions for chapter 3 of CBSE class 11 Computer Science with Python Exercise. So is the case if you are looking for CBSE class 11 Computer Science with Python related topic Conditional And Looping Construct questions and answers for the Exercise.
Exercise
Question 1
1) Mark True/False:
(i)
While statements get executed at least once.
(ii)
The break statement allows us to come out of a loop.
(iii)
The continue and break statements have the same effect.
(iv)
We can nest loops.
(v)
We cannot write a loop that can execute forever.
(vi)
Checking condition in Python can be done using the if-else statement.
Answer 1
(i)
While statements gets executed at least once
Answer: False
A
while loop checks its condition before the loop starts. If the condition is false at the beginning, the loop body will not execute even once. Therefore, it is not necessary that a while loop runs at least one time.(ii)
The break statement allows us to come out of a loop
Answer: True
The
break statement is used to stop a loop immediately. As soon as break is executed, control comes out of the loop and moves to the next statement after the loop. It can be used in both for and while loops.(iii)
The continue and break statement have same effect
Answer: False
These two statements are different.
•
breakends the loop completely.•
continue only skips the rest of the current iteration and sends control back to the beginning of the loop for the next iteration.(iv)
We can nest loops
Answer: True
Yes, one loop can be written inside another loop. This is called nesting of loops. Nested loops are useful when one repeated task contains another repeated task inside it.
(v)
We cannot write a loop that can execute forever
Answer: False
It is possible to write a loop that runs forever. Such a loop is called an infinite loop. This happens when the condition of the loop always remains true and there is no proper stopping condition.
(vi)
Checking condition in Python can be done by using the if-else statement
Answer: True
The
if-else statement is used to test a condition and then choose one block of statements based on whether the condition is true or false. It is one of the main decision-making statements in Python.Question 2
2) What is the difference between the following two statements:
(i)
if n > 2:if n < 6:print "OK"else:print "NG"(ii)
if n > 2:if n < 6:print "OK"else:print "NG"Answer 2
Difference between the two statements
The difference is based on which
if statement the else belongs to. In Python, indentation decides the structure of the blocks.•
In the first statement, the
else is attached to the outer if n > 2.So,
"NG" is printed only when n is not greater than 2.If
n > 2 but n >= 6, then nothing is printed.•
In the second statement, the
else is attached to the inner if n < 6.So, when
n > 2:•
if
n < 6, it prints "OK"•
otherwise, it prints
"NG"Thus, both statements look similar, but their outputs are different because the
else belongs to a different if block.Question 3
3) Mark the correct option(s):
(i)
If there are two or more options, then we can use:
a)
Simple if statement
b)
If-elif statement
c)
While
d)
None of these
(ii)
A loop that never ends is called:
a)
Continue loop
b)
Infinite loop
c)
Circle loop
d)
None of these
Answer 3
(i)
If there are two or more options, then we can use
Answer: (b) If elif statement
When there are multiple choices, the
if-elif-else statement is used. It checks the conditions one by one in order. As soon as one condition becomes true, its block is executed. If none of the conditions is true, the else block is executed.(ii)
A loop that never ends is called a:
Answer: (b) Infinite loop
A loop that continues without stopping is called an infinite loop. This happens when its condition always remains true.
Question 4
4) Construct a logical expression to represent each of the following conditions:
(i)
Score is greater than or equal to 80 but less than 90.
(ii)
Answer is either “N” or “n”.
(iii)
N is between 0 and 7 but not equal to 3.
Answer 4
(i)
Score is greater than or equal to 80 but less than 90
score >= 80 and score < 90
This uses
and because both conditions must be true together.(ii)
Answer is either ‘N’ or ‘n’
answer == 'N' or answer == 'n'
This uses
or because either one of the two conditions can be true.(iii)
N is between 0 and 7 but not equal to 3
n >= 0 and n <= 7 and n != 3
This means
n should be within the given range, but the value 3 must be excluded.These expressions are formed using relational operators and logical operators.
Question 5
5) Which of the following loop will continue infinitely:
(i)
Select the infinite loop.
a)
while 0:b)
while 1:c)
while :1:d)
while False:(ii)
We can go back to the start of the loop by using:
a)
loop
b)
back
c)
start
d)
continue
Answer 5
(i)
Which of the following loop will continue infinitely?
Answer: (b)
while 1:The loop
while 1: runs infinitely because 1 is treated as True in Python. Since the condition always remains true, the loop keeps running until it is stopped manually or by using a break statement.(ii)
We can go back to the start of the loop by using __________
Answer: (d) continue
The
continue statement skips the remaining part of the current iteration and sends control back to the beginning of the loop for the next iteration.Question 6
6) What is the difference between selection and repetition?
Answer 6
The following are the differences between selection and repetition.
Basis
Selection
Repetition
Meaning
Selection means choosing one block of statements from two or more alternatives based on a condition.
Repetition means executing the same block of statements again and again while a condition remains true.
Purpose
It is used for decision making in a program.
It is used for repeated execution of statements.
Condition use
A condition is checked to decide which option should be executed.
A condition is checked to decide how long the loop should continue.
Statements used
It is done using statements like
if, if-else, and if-elif-else.It is done using loops like
while and for.Execution
Only one suitable block is selected and executed.
The same block may execute many times.
Thus, selection is used when the program has to make a choice, whereas repetition is used when the program has to repeat a task multiple times.
Question 7
7) Explain use of if statement with example.
Answer 7
Use of if statement with example
The
if statement is used when a block of statements should execute only when a condition is true. It is the simplest decision-making statement in Python. If the condition is true, the statements inside the if block are executed. If the condition is false, those statements are skipped.Example
n = input("Enter a number: ")
if n > 0:
print "Number is positive"
In this example:
•
the condition
n > 0 is checked•
if it is true, the message “Number is positive” is displayed
•
if it is false, nothing is printed
Thus, the
if statement is used whenever the execution of a statement depends on a condition.LAB EXERCISE
Question 1 (Lab)
1)
answer = raw_input("Do you like Python? ")if answer == "yes":print "That is great!"else:print "That is disappointing!"Modify the program so that it answers “That is great!” if the answer was “yes”, “That is disappointing” if the answer was “no” and “That is not an answer to my question.” otherwise.
Answer 1 (Lab)
This program should check the user’s answer and respond differently for three cases:
•
when the answer is “yes”
•
when the answer is “no”
•
when the user enters anything else
For this, the
if-elif-else statement is suitable because it allows more than two choices to be checked.Program
# Program to respond properly to different answers
answer = raw_input("Do you like Python? ")
if answer == "yes":
print "That is great!"
elif answer == "no":
print "That is disappointing!"
else:
print "That is not an answer to my question."
Sample Output (Condition: yes)
Do you like Python? yes
That is great!
Sample Output (Condition: no)
Do you like Python? no
That is disappointing!
Sample Output (Condition: other)
Do you like Python? maybe
That is not an answer to my question.
Explanation
•
The program first takes input from the user using
raw_input().•
If the answer is
"yes", it prints a positive response.•
If the answer is
"no", it prints a different response.•
If the answer is something else, the
else block handles it.Thus, this program is a good example of using conditional statements to handle different kinds of input properly.
Question 2 (Lab)
2) Write a function to find whether given number is odd or even.
Answer 2 (Lab)
This function checks whether the given number is divisible by 2 or not.
•
If the remainder is 0, the number is even.
•
Otherwise, the number is odd.
The modulo operator
% is used for this purpose.Program
# Function to check whether a number is odd or even
def oddEven(n):
if n % 2 == 0:
print "Even"
else:
print "Odd"
Sample Output
oddEven(7)
Odd
Explanation
•
n % 2 gives the remainder when n is divided by 2.•
If the remainder is
0, the number is even.•
If the remainder is not
0, the number is odd.Thus, this is a simple function that uses a selection statement to decide whether a number is odd or even.
Question 3 (Lab)
3) Print all multiples of 13 that are smaller than 100. Use the function
range(start, end, step), where start is the starting value, end is the ending value, and step is the increment.Answer 3 (Lab)
This program prints only those numbers that are multiples of 13 and less than 100.
Instead of checking every number one by one, it uses a
for loop with step value 13.Program
# Program to print multiples of 13 less than 100
for i in range(13, 100, 13):
print i
Sample Output
13
26
39
52
65
78
91
Explanation
•
The loop starts from
13.•
It increases by
13 each time.•
It stops before
100.So, the program directly generates multiples of
13 without unnecessary checking.Question 4 (Lab)
4) Write a program using a while loop that asks the user for a number and prints a countdown from that number to zero. Note: Decide what your program should do if the user enters a negative number.
Answer 4 (Lab)
This program takes a number from the user and prints all numbers from that value down to 0.
A
while loop is suitable because the value keeps changing until it reaches zero.Program
# Program to print countdown from a number to zero
n = input("Enter a number: ")
if n < 0:
print "Please enter a non-negative number"
else:
while n >= 0:
print n
n = n - 1
Sample Output (Non-negative input)
Enter a number: 5
5
4
3
2
1
0
Sample Output (Negative input)
Enter a number: -3
Please enter a non-negative number
Explanation
•
The program first takes a number as input.
•
If the number is negative, it prints a message.
•
Otherwise, the
while loop starts.•
In each iteration, the current value of
n is printed.•
Then
n is reduced by 1.This continues until
n becomes smaller than 0.Question 5 (Lab)
5) Using a for loop, write a program that prints the decimal equivalent of fractions like 1/2, 1/4, and so on.
Answer 5 (Lab)
This program prints decimal values of fractions whose denominators are powers of 2.
•
1/2•
1/4•
1/8•
1/16•
1/32A
for loop can generate this pattern easily.Program
# Program to print decimal values of 1/2, 1/4, 1/8, 1/16, 1/32
d = 2.0
for i in range(5):
print 1 / d
d = d * 2
Sample Output
0.5
0.25
0.125
0.0625
0.03125
Explanation
•
The denominator starts from
2.0.•
In each iteration,
1 / d is printed.•
Then the denominator is doubled.
This creates the required sequence in decimal form.
Question 6 (Lab)
6) Write a function to print the Fibonacci Series up to an Input Limit.
Answer 6 (Lab)
The Fibonacci series is a sequence where each term is the sum of the previous two terms.
It starts as
0, 1, 1, 2, 3, 5, 8, 13, .... This function prints terms up to a given limit.Program
# Function to print Fibonacci series up to a limit
def fibonacci(limit):
a = 0
b = 1
while a <= limit:
print a
c = a + b
a = b
b = c
Sample Output
fibonacci(20)
0
1
1
2
3
5
8
13
Explanation
•
a and b store the first two terms.•
The current value of
a is printed.•
The next term is found by adding
a and b.•
Then values are updated for the next iteration.
The loop continues until the value exceeds the given limit.
Question 7 (Lab)
7) Write a function to generate and print factorial numbers up to
n (provided by the user).Answer 7 (Lab)
A factorial is the product of all positive integers up to that number.
•
1! = 1•
2! = 2•
3! = 6•
4! = 24This function prints factorial values from
1 to n.Program
# Function to print factorial values up to n
def factorialSeries(n):
fact = 1
i = 1
while i <= n:
fact = fact * i
print "Factorial of", i, "=", fact
i = i + 1
Sample Output
factorialSeries(5)
Factorial of 1 = 1
Factorial of 2 = 2
Factorial of 3 = 6
Factorial of 4 = 24
Factorial of 5 = 120
Explanation
•
fact stores the running factorial value.•
i starts from 1.•
In each step,
fact is multiplied by i.•
The result is printed, and
i is increased.This continues until all factorial values up to
n are printed.Question 8 (Lab)
8) Write a program using a for loop that calculates exponentials. Your program should ask for base and exponent value from the user. Note: Do not use the
** operator or the math module.Answer 8 (Lab)
This program calculates power of a number without using
** or the math module.So, the result is found by repeated multiplication.
Program
# Program to calculate power without using ** operator
base = input("Enter base: ")
exp = input("Enter exponent: ")
result = 1
for i in range(exp):
result = result * base
print "Result =", result
Sample Output
Enter base: 2
Enter exponent: 5
Result = 32
Explanation
•
result starts from 1.•
The loop runs
exp times.•
In each iteration,
result is multiplied by base.So, if base is
2 and exponent is 5, multiplication becomes 1 x 2 x 2 x 2 x 2 x 2 = 32.Question 9 (Lab)
9) Write a program using a loop that asks the user to enter an even number. If the number entered is not even, display an appropriate message and ask again. Do not stop until an even number is entered. Print a congratulatory message at the end.
Answer 9 (Lab)
This program repeatedly asks the user to enter a number until an even number is entered.
It is a good example of input validation using a loop.
Program
# Program to keep asking until an even number is entered
n = input("Enter an even number: ")
while n % 2 != 0:
print "This is not an even number. Try again."
n = input("Enter an even number: ")
print "Congratulations! You entered an even number."
Sample Output
Enter an even number: 7
This is not an even number. Try again.
Enter an even number: 10
Congratulations! You entered an even number.
Explanation
•
The program first takes a number as input.
•
It checks whether the number is odd using
n % 2 != 0.•
If the number is odd, the loop continues and asks again.
•
When the user enters an even number, the loop stops.
Thus, the program keeps repeating until the required condition is satisfied.
Question 10 (Lab)
10) Using the random module, simulate tossing a coin N times. Hint: you can use 0 for head and 1 for tails.
Answer 10 (Lab)
This program simulates coin tosses N times using random numbers.
•
Head
•
Tail
A simple mapping is:
•
0 for Head•
1 for TailProgram
# Program to simulate tossing a coin N times
import random
n = input("How many times to toss the coin? ")
for i in range(n):
toss = random.randint(0, 1)
if toss == 0:
print "0 Head"
else:
print "1 Tail"
Sample Output
How many times to toss the coin? 5
0 Head
1 Tail
1 Tail
0 Head
0 Head
Explanation
•
The
random module is imported to generate random values.•
random.randint(0, 1) gives either 0 or 1.•
If the value is
0, the result is printed as Head.•
If the value is
1, the result is printed as Tail.Since the loop runs
N times, one result is printed for each toss.