Complete the code to print numbers from 0 to 4 using a for loop and range().
for i in [1](5): print(i)
The range(5) function generates numbers from 0 up to 4. Using it in the for loop lets us print these numbers one by one.
Complete the code to print numbers from 3 to 7 using range().
for num in [1](3, 8): print(num)
range(3, 8) generates numbers starting at 3 up to 7 (8 is excluded). This lets the loop print numbers 3 to 7.
Fix the error in the code to print even numbers from 2 to 10 using range().
for x in range(2, 11, [1]): print(x)
The third argument in range is the step. Using 2 steps moves from 2 to 4, 6, 8, 10, printing even numbers.
Fill both blanks to create a dictionary with numbers from 1 to 5 as keys and their squares as values.
squares = {x: x[1]2 for x in [2](1, 6)}Using range(1, 6) generates numbers 1 to 5. The expression x**2 calculates the square of each number. Together, they create the dictionary of squares.
Fill all three blanks to create a dictionary with uppercase letters as keys and their positions as values for letters A to E.
letters = [1]: [2] for [3] in range(1, 6)}
chr(i) gives wrong letters because ASCII codes start at 65 for 'A'.The chr(i + 64) converts numbers 1 to 5 into letters A to E. The key is the letter, and the value is the number i. The loop variable is i.