As WS CS G7 CH 7 Python - Conditions and Loops
As WS CS G7 CH 7 Python - Conditions and Loops
Q.1. Select the correct output for the given Python code. [An]
a. x=22
y=34
print(x==y)
i. 0 ii. 1
b. x=25
y=22
z=0
print(x-y!=z)
i. 1 ii. 0
c. counter=2
while counter:
print('$')
counter=counter-2
ii. $
i. $$$
$
iii. $ iv. $
$
$
d. x=[20, 40]
for i in x:
print(i+1)
ii. 20
i. 20
40
iii. 21
iv. 40
41
b. What is a list? Explain with an example how you can create a list [U
and display the items of a list.
Ans. In Python, a list is an ordered collection of elements
enclosed in square brackets [ ].
For example:
num = [35, 45, 55,65]
print(num)
It can contain elements of different data types, such as
integers, floats, strings, or other lists.
It is a data structure that allows you to store and
organise multiple items in a single variable.
c. Explain the use of the ‘break’ statement with the help of an [U]
example.
Ans. The ‘break’ statement is used to exit a loop during its
execution based on a condition.
When a program comes across a ‘break’ statement
inside a loop, the current iteration will stop and control
is transferred to the statement that appears
immediately after the loop.
d. Explain the difference between the ‘while’ loop and ‘for’ loop. [An]
Ans. The ‘while’ loop allows you to repeat a set of instructions
as long as a specific condition remains true. It is used
when you do not know how many times the loop will
execute.
The ‘for’ loop is used when you need to repeat the same
instructions for each value in a sequence. It is generally
used in situations where you know the number of times
a loop will execute.
Q.4 Observe the Python code given below. to find the factorial of a [An]
number. The factorial of a number ‘n’ is the product of all numbers
smaller than or equal to ‘n’. The given program is not displaying the
correct output. Modify the code to get the correct output.
Code:
num=int(input('Enter the number for calculating the
factorial:'))
fact=1
i=1
while(i<=fact):
fact=fact*i
i=i+1
print('The factorial of ', num, ' = ',num)
Output 1 of the code:
Enter the number for calculating the factorial:6
The factorial of 6 = 720
Output 2 of the code:
Enter the number for calculating the factorial:7
The factorial of 7 = 5040
Ans. num=int(input('Enter the number for calculating the
factorial:'))
fact=1
i=1
while(i<=num):
fact=fact*i
Lab Activity
Q.1 Write a program in Python to print the Fibonacci series between 0 to 50.
Definition: The Fibonacci sequence is a sequence in which each number
is the sum of the two preceding ones.
For example: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144
Q.2 Write a program in Python that accepts a word from the user and then
reverses it. Display the reversed word.
For example – Original word: Yummy
Reversed word-ymmuy
Q.3 Write a program in Python to accept a number from the user and display
whether it is a prime number. A prime number is a number not divisible
by any number other than itself and 1. (Hint: Use a loop and break
statement)
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>