0
0
Pythonprogramming~10 mins

For loop execution model 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 each number from 1 to 3.

Python
for num in [1]:
    print(num)
Drag options to blanks, or click blank then click option'
Arange(1, 4)
B[1, 2]
Clist(3)
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using a single number instead of a range.
Using a list with fewer numbers than needed.
2fill in blank
medium

Complete the code to sum all numbers from 1 to 5 using a for loop.

Python
total = 0
for i in range(1, 6):
    total += [1]
print(total)
Drag options to blanks, or click blank then click option'
Atotal
Bi
Crange(1, 6)
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Adding the total to itself instead of the loop variable.
Using the range function inside the loop body.
3fill in blank
hard

Fix the error in the code to print each character in the string.

Python
word = "hello"
for [1] in word:
    print(letter)
Drag options to blanks, or click blank then click option'
Achar
Bword
Cletter
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name in the loop header and body.
Using the string variable name as the loop variable.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths as values for words longer than 3 letters.

Python
lengths = { [1]: [2] for word in words if len(word) > 3 }
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
Cword.upper()
Dlen
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method like word.upper() as the key.
Using the function len without calling it.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys, their lengths as values, only for words longer than 4 letters.

Python
result = { [1]: [2] for word in words if [3] }
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
Clen(word) > 4
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the original word as key instead of uppercase.
Forgetting to call len(word) in the condition.
Using wrong comparison operators.