CMSC 201

Lab 7: Practice

Lists

Part 1

Given the list:

myList = [1, 2, 3]

Write code that swaps the first and second element (this should work on any list, not just [1, 2, 3])

Part 2

Write a short program that loops over the list:

myList = [1, 2, 3, 4, 5, 6]

And prints out the following:

1 2
3 4
5 6
Only use one loop.

Part 3

Write code that uses loops to interchange all the even and odd elements in a list. Start with:

myList = [1, 2, 3, 4, 5, 6, 7, 8]

And swap the 0th element with the first, the second element with the third, and so on. The result should look like this:

[2, 1, 4, 3, 6, 5, 8, 7]

You can assume an even number of elements in the list.