0% found this document useful (1 vote)
2K views3 pages

Class 8 CH 6 Loops, Coding in Python (Solved)

Uploaded by

santoshghuriya12
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 (1 vote)
2K views3 pages

Class 8 CH 6 Loops, Coding in Python (Solved)

Uploaded by

santoshghuriya12
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/ 3

Answer-D1:

• Iterative statements (loops) repeat a set of instructions while a condition is true.


• Real Life Example: Printing a table of a number entered by the user.
• Python Code Example:
n = int(input("Enter a number: "))
i=1
while i <= 10:
print(n, "*", i, "=", n*i)
i += 1
Answer-D2:
Membership operators check if a value is present in a sequence (like string, list).
• in → output will be True , if value exists in the sequence. Else, output will be False
• not in → output will be True, if value does not exist in the sequence. Else, output will be False

Answer-D3:
• The range() function creates a sequence of numbers from the start value to the end (i.e end-1)
value.
• It is used for loops to run a specific number of times.

Answer-D4:
For Loop While Loop
• Number of iterations is known. • Runs until a condition is false.
• Called a definite loop. • Called a pre-tested loop.
• Works on sequence of numbers. • Has 4 steps: Initialise, Test, Loop body, Step value.

Yes, by using a loop with a condition that is always true.


Python Code:
while 1>0:
print("Infinite Loop")

Answer-E1:
• Prateek should use an if statement
• Python Code:
num = int(input("Enter a number: "))
if num % 7 == 0:
print("The number is divisible by 7")
else:
print("The number is not divisible by 7")

Answer-E2:
• Kriti can use a for loop with range()
• Python Code:
for i in range(1, 101):
print(i)
Answer-F1:
Steps of a while loop:
1) Start with a variable (initialisation).
2) Write a condition to check.
3) Run the loop body (instructions).
4) Change the variable (to avoid infinite loop).
Python Code:
i=1 # Step 1: Initialisation
while i <= 5: # Step 2: Condition
print(i) # Step 3: Loop body
i += 1 # Step 4: Update variable

Answer-F2:
evens = range(2, 21, 2) # Even numbers from 2 to 20
for num in range(2, 21):
if num in evens:
print(num)

You might also like