Week 6 Practice


Concepts Reviewed

These questions are optional supplements that ideally help you get prepared for the quizzes ahead of time. (Some of them are also designed for elaboration purposes as you can see from the tag. Don’t worry them if you just want to get prepared for the quizzes) Before working through this week’s practice questions, be sure to get familiar with the for-in loop syntax


Questions

For-In Loop

  1. Which of the following loops can be put on the blank and can iterate over each character in the string s = “Python” and print them? (Choose all that apply)
for _______________:
    print(s[i])
  1. for i in range(0, s):
  2. for i in range(0, len(s)):
  3. for i in s:
  4. for i in range(s):
  1. (Continue on Q1) What will be the correct option if we change the body of the loop to:
print(i)
  1. Given a list lst = [10, 20, 30, 40, 50], what will the following code snippet print?
for i in range(0, len(lst)):
    print(i)
  1. It will print numbers from 0 to 4.
  2. It will print each element in lst.
  3. It will result in a TypeError.
  4. It will print numbers from 1 to 5.
  1. (Continue on Q3) Given a list lst = [10, 20, 30, 40, 50], what will the following code snippet print?
for i in lst:
    print(lst[i])
  1. It will print numbers from 0 to 4.
  2. It will print each element in lst.
  3. It will result in a TypeError.
  4. It will print numbers from 1 to 5.
  1. What is the difference between for item in list: and for i in range(len(list)): when iterating over a list named list?
  1. The first iterates over each index of the list, while the second iterates over each item.
  2. The first iterates over each item of the list, while the second iterates over each index.
  3. There is no difference; both iterate over the list items.
  4. The first can only be used with lists, while the second can be used with any iterable.
  1. (For elaboration) Which of the following is the correct way to double each element in a list nums = [1, 2, 3, 4, 5] using a for loop? A).
for n in nums:
    n = n * 2

B). ~~~ for i in range(len(nums)): nums[i] = nums[i] * 2 ~~~

C). for i in nums: nums[i] = i * 2

D). Both Option A and Option B are correct

  1. (For elaboration - range() function) Which of the following for loops will print all the even numbers from 2 to 10, inclusive?
  1. for i in range(2, 11, 2): print(i)
  2. for i in range(2, 10): if i % 2 == 0: print(i)
  3. for i in range(1, 6): print(i * 2)
  4. Both A and C are correct.
  1. Compare the following two codes and tell their difference (if any)
for x in range(10):
    print(x, end=" ")
for x in range(0, 10):
    print(x, end=" ")
  1. The first loop prints 0 to 9, the second loop prints 0 to 10

  2. The first loop prints 1 to 10, the second loop prints 0 to 9

  3. The first loop prints 0 to 9, the second loop prints 1 to 10

  4. There is no difference, both loops print 0 to 9

  1. Given that nums = [0, 1, 2, 3, 4, 5]. Write a for-in loop that doubles each element in the list. (numsshould be [0, 2, 4, 6, 8, 10] after the loop)

  2. Write a for-in loop that can print out the number from 2 to 10.

  3. (For elaboration) If you are confident with range()function, can you write a for-in loop that can print out the even number from 2 to 10


List Memory Diagram

  • Work through the Example 0 to Example 3 (110 site > resources > Practice Memory Diagrams > Topic: Lists) Feel Free to bring those exercises to tutoring for help and discussion

Solutions

For-In Loop

  1. B, C

  2. C

  3. A

  4. C

  1. The first iterates over each item of the list, while the second iterates over each index.
  1. B

  2. D

  3. D

  4. One possible answer can be: (Notice that here for i in nums and double i will not work)

for i in range(len(nums)):
    nums[i] = nums[i] * 2
  1. One possible answer can be:
for i in range(2, 11):
    print(i)
  1. One possible answer can be:
for i in range(2, 11, 2):
    print(i)
Contributor(s): Vincent Li