Lecture 3: Conditionals, basic lists, for loopsΒΆ

Based on the lecture and the readings (R&S Control Flow) you should be able to:

  • Use conditionals as control flow statements

  • Determine when and how to use for loops to reduce the length of code

  • Understand how to index a list

Below, please find some practice problems for this topic. These problems should be completed by hand, that is, without the use of a computer. (You can use ipython3 to check your answers.)

  1. What is the output of the code fragment below?

for i in range(10):
    print(i)
  1. What is the value of result in the code fragment below?

mystery = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
mystery2 = [i * 2 for i in mystery]
result = mystery2[3]
  1. What is the output of the code fragment below?

for i in range(10):
    if i // 2 == 0:
        print(i)