0% found this document useful (0 votes)
500 views4 pages

As WS CS G7 CH 7 Python - Conditions and Loops

Uploaded by

nisha.kawale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
500 views4 pages

As WS CS G7 CH 7 Python - Conditions and Loops

Uploaded by

nisha.kawale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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

iii. True iv. False

b. x=25
y=22
z=0
print(x-y!=z)

i. 1 ii. 0

iii. True iv. False

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

e for a in range(1, 4):


if a==1:

Python – Conditions and Loops Pg no. 1


7. Python - Conditions and Loops
continue
print(a)
i. 1 ii. 3
2 4
iii. 2 iv. 0
3 1

Q.2 Answer the following questions.


a. Describe the use of the range function. Write a program to [R]
display the first ten multiples of 13 using the range function.
Ans.  The ‘range()’ function in Python is used to generate a
sequence of numbers.
def getMultiples(num, number_of_multiples):
for i in range(1, (number_of_multiples+1)):
print(num*i)
getMultiples(13, 10);

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.

Python – Conditions and Loops Pg no. 2


7. Python - Conditions and Loops
 For example, while printing odd numbers, if you do not
wish to print beyond a particular number, you can use a
‘break’ statement.

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

Python – Conditions and Loops Pg no. 3


7. Python - Conditions and Loops
i=i+1
print('The factorial of ', num, ' = ',fact)

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)
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Python – Conditions and Loops Pg no. 4

You might also like