Thanks to Dr. Alan McLeod for the content of this lab.

This lab will give you practice with strings, passing by reference, and lists of lists. Keep your code from this lab as some of the exercises will be used next week to create a single, larger, multi-function program.

Exercise 1 - Palindromes

A palindrome is a sentence or phrase that reads the same from right to left as it does from left to right. Here are a few examples:

As you can see the "definition" of a palindrome includes the assumptions:

Here are some links on palindromes:

Write a function that accepts a string and returns True if the string is a palindrome. Use the main() function to prompt the user for the string and then invoke your palindrome checker. Assume that you will not have to check numeric characters.

To get started, check over the list of string methods provided in lecture to find the ones that will be useful. Here is a sample solution to the exercise: Palindromes.py.

Exercise 2 - Passing by Reference

This concept is most simply put as "If you pass a list into a function, and make elemental changes to that list in the function, then those changes will persist even when the function is finished."

This rule applies to elemental changes to any mutable object, but this exercise will just consider lists. For example, write a program that has a main() function that creates the list [1, 2, 3, 4]. Send the list as a parameter to a function called change(...) that multiplies each list value by 10, accessing each element through the slice operator. Do not return the list; just send it through the parameter list. Print the list before and after invoking change() from the main() function. Has the list changed? Here is what this program could look like.

Now, carry out a few experiments with this program as a starting point:

Don't hesitate to keep experimenting!

Exercise 3 - List of Lists

This is not really a new concept at all. We know that a list can hold anything! Suppose you have a list of lists where each smaller list is of the same size. You can think of a structure like this as a table, where each row is the smaller, or sub-list and the number of columns is the length of the sub-list. The number of rows is the length of the list of lists. You can get at an individual element in the list of lists by using two slice operators back to back, like [row][col]. The first slice operator provides the row sub-list and then the second slice operator pulls out the element at position col from within the sub-list.

As you might expect nested loops are often used with list of lists. For now, don't worry about doing any error checking for the following functions:

Here is what you might have ended up with.

More Exercises

You should now be able to do any of the Programming Exercises at the end of Chapter 8 of the textbook. A few of the string exercises look interesting. See exercises 1, 2, and 3, for example. Exercises 4 and 5 are good ones, but a bit long. Exercise 10 uses a list and is reproduced here:

Design a program that lets the user enter the total rainfall for each of 12 months into a list. The program should calculate and display the total rainfall for the year, the average monthly rainfall and the months with the highest and lowest amounts.

To add to this exercise, make sure that the rainfall is greater than or equal to zero for each month. Try storing the month names in a list of strings. Here is a sample solution.

Here is a a more advanced solution that uses functions, robust input, a dictionary and even saves the data into a file, just for fun!