0
0
Pythonprogramming~10 mins

Iteration using range() in Python - Interactive Code Practice

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

Complete the code to print numbers from 0 to 4 using a for loop and range().

Python
for i in [1](5):
    print(i)
Drag options to blanks, or click blank then click option'
Arange
Blist
Ctuple
Dset
Attempts:
3 left
💡 Hint
Common Mistakes
Using list(5) instead of range(5) causes an error.
Using tuple(5) or set(5) is invalid for iteration.
2fill in blank
medium

Complete the code to print numbers from 3 to 7 using range().

Python
for num in [1](3, 8):
    print(num)
Drag options to blanks, or click blank then click option'
Atuple
Blist
Crange
Dset
Attempts:
3 left
💡 Hint
Common Mistakes
Using 7 as the stop value will exclude 7 from the output.
Using list or tuple instead of range causes errors.
3fill in blank
hard

Fix the error in the code to print even numbers from 2 to 10 using range().

Python
for x in range(2, 11, [1]):
    print(x)
Drag options to blanks, or click blank then click option'
A2
B1
C3
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Using step 1 prints all numbers, not just even ones.
Using step 3 or 4 skips some even numbers.
4fill in blank
hard

Fill both blanks to create a dictionary with numbers from 1 to 5 as keys and their squares as values.

Python
squares = {x: x[1]2 for x in [2](1, 6)}
Drag options to blanks, or click blank then click option'
A**
B*
Crange
Dlist
Attempts:
3 left
💡 Hint
Common Mistakes
Using * instead of ** calculates double the number, not square.
Using list instead of range causes errors in dictionary comprehension.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase letters as keys and their positions as values for letters A to E.

Python
letters = [1]: [2] for [3] in range(1, 6)}
Drag options to blanks, or click blank then click option'
Achr(i + 64)
Bi
Dchr(i)
Attempts:
3 left
💡 Hint
Common Mistakes
Using chr(i) gives wrong letters because ASCII codes start at 65 for 'A'.
Using wrong loop variable name causes errors.