0
0
Pythonprogramming~10 mins

Why loop control is required in Python - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print numbers from 1 to 5 using a loop.

Python
for i in range(1, [1]):
    print(i)
Drag options to blanks, or click blank then click option'
A6
B5
C10
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 5 as stop will print 1 to 4 only.
2fill in blank
medium

Complete the code to stop the loop when i equals 3.

Python
for i in range(1, 6):
    if i == [1]:
        break
    print(i)
Drag options to blanks, or click blank then click option'
A1
B5
C3
D6
Attempts:
3 left
💡 Hint
Common Mistakes
Using break with wrong value stops loop too early or too late.
3fill in blank
hard

Fix the error in the loop to skip printing number 3.

Python
for i in range(1, 6):
    if i == [1]:
        continue
    print(i)
Drag options to blanks, or click blank then click option'
A3
B2
C5
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Using continue with wrong value skips wrong number.
4fill in blank
hard

Fill both blanks to create a dictionary of squares for numbers greater than 2.

Python
squares = {x: x[1]2 for x in range(1, 6) if x [2] 2}
Drag options to blanks, or click blank then click option'
A**
B+
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of ** changes meaning.
Using < instead of > filters wrong numbers.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values greater than 0.

Python
result = [1]: [2] for k, v in data.items() if v [3] 0}
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using k instead of k.upper() keeps keys lowercase.
Using < instead of > filters wrong values.