Loading samples...
Loading samples...
py-sample-7
Master iteration with for loops, while loops, and control statements
What does range(5) produce?
Which loop is used when iterations are known?
What does break do?
What does continue do?
What is range(2, 10, 2)?
Can for loop have else block?
When does while-else else block execute?
What creates an infinite loop?
What is enumerate([a,b,c])?
How to iterate over two lists simultaneously?
Differentiate between for loop and while loop with examples.
[3 marks]Explain the difference between break and continue statements.
[3 marks]What is the use of range() function? Explain its three forms.
[3 marks]Explain for-else and while-else with practical examples.
[3 marks]What is nested loop? When is it useful? Give example.
[3 marks]Explain enumerate() function with an example.
[3 marks]What is zip() function? How to iterate multiple sequences?
[3 marks]How to reverse a list using a loop? Show two methods.
[3 marks]Explain list comprehension vs regular for loop.
[3 marks]What is an infinite loop? How to prevent it?
[3 marks]Write a program to print the multiplication table of a given number.
[5 marks]Create a program to calculate factorial using while loop.
[5 marks]Write a program to print Fibonacci series up to n terms.
[5 marks]Create a program to check if a number is prime using for-else.
[5 marks]Write a program to print patterns: pyramid, inverted pyramid, diamond.
[5 marks]Create a program to find sum of digits and reverse of a number.
[5 marks]Predict output: for i in range(3): print(i, end=' ')
[2 marks]Explanation:range(3) produces 0, 1, 2. end=' ' adds space after each instead of newline.
Find error: while x < 10: print(x)
[2 marks]Explanation:x is not initialized before use. Need x = 0 before while loop.
Predict output: for i in range(5): if i == 3: break print(i)
[2 marks]Explanation:break exits loop when i==3. So only 0, 1, 2 are printed.
Find error: for i in range(5): if i == 2: continue print(i)
[2 marks]Explanation:print(i) after continue is never executed. Indentation or logic is wrong.
Predict output: i = 0 while i < 3: print(i) i += 1 else: print('Done')
[2 marks]Explanation:while-else: else executes when loop completes normally (no break).
Find error: for i in range(5, 0): print(i)
[2 marks]Explanation:range(5, 0) produces nothing because default step is +1. Use range(5, 0, -1) for reverse.
Predict output: count = 0 while count < 5: if count == 3: count += 1 continue print(count) count += 1
[3 marks]Explanation:When count==3, we skip printing due to continue. Output is 0,1,2,4 (3 is skipped).
Find error: my_list = [1, 2, 3] for i in range(len(my_list)): print(my_list[i]) my_list.pop(i)
[3 marks]Explanation:Modifying list while iterating causes index issues. List shrinks but index increases.