The purpose of today's lab is to practice using strings, lists, dictionaries and tuples. There are 4 components:

Begin by creating a new folder for today's lab called lab03.

Slice

This exercise will make use of Python string and list slicing. For the purposes of this exercise, you can use simple character indexing and the following for character range access.

my_string[start_index: end_index] — This gets the substring from position start_index up to (but not including) position end_index. E.g., 'test'[1:3] returns 'es'.

Slicing is covered in some detail in Chapter 7.

Exercise 3.1
Years ago, a Calvin login id was made up of the first letter of a student's first name, the first 5 letters of their last name, and the last 2 digits of their student number. Write an interactive program in a file called login.py to generate a person's login id. Use the following algorithm:
  1. Prompt for and store the first name
  2. Prompt for and store the last name (assume that last names have no spaces)
  3. Prompt for and store the student number. For our purposes, should this be an int or a string?
  4. Compute the login id. Note: you can use login = login.lower() to create an id that has all lowercase letters.
  5. Print the login id.

Your solution must involve at least 2 slice operations: One to compute the appropriate piece of the last name, and another to compute the last 2 digits of the student id number. Be sure to test your program using last names with fewer and more than 5 letters!

Basic Lists

A python list is a very powerful data structure with an extensive API. However, at this early juncture of the course, we want to solidify a few basic operations instead of spending our time perusing the API looking for methods and/or functions that we hope exist.

Exercise 3.2
Create a file called sorting.py that allows the user to enter 4 numbers and stores these numbers in a list. The program will then create and print a new list with the same numbers in sorted order (from min to max). Begin by writing an algorithm as a comment in your file. Then implement your algorithm using valid Python commands.

Working with dictionaries

Dictionaries are also useful data structures.

Exercise 3.3
Create a file called dict_practice.py . Within this file write commands implement the following algorithm:
  1. Create a dictionary called scoreDict that has the names “Joe”, “Tom”, “Barb”, “Sue”, “Sally” associated with the scores 10, 23, 13, 19 and 12 respectively.
  2. Using scoreDict, print an expression that computes Barb’s score.
  3. Update the score for Sally to be 13.
  4. Tom just dropped the course. Delete Tom and his score from scoreDict.
  5. Print the resulting dictionary.

User described line (with tuples!)

At this point, we don't have a terribly compelling reason to use tuples, but we can certainly practice using the syntax!

There are 3 new turtle methods that will be helpful today:

Exercise 3.4
In this exercise you will use turtle graphics to draw a line with end points specified by the user. You will also show the values of the coordinates and the length of the line. Create a file called turtleLine.py, updated the documentation appropriately, and then proceed roughly as follows (you'll need to fill in some details):
  1. import the turtle and math modules.
  2. Prompt for and store values for x1, y1, x2 and y2 values
  3. Create tuples point1 and point2 that represent the two points entered by the user.
  4. Create a window and turtle with which to draw, and hide the arrowhead of the turtle.
  5. Use goto() to move the turtle to the first point.
  6. Use write() to describe the first point. Use an italic Arial font with size 20.
  7. Draw a line from the first point to the second.
  8. Describe the second point.
  9. Draw the caption. Note: The caption should always be aligned with the left-most point and below the lowest point, left justified and must include the length of the line.

Checking In

We will grade these exercises according to the following criteria:

Submit your solutions to these lab exercises using your appropriate submission environment.

If you worked with a partner, make sure you both have a copy of the files.

If you’re working on a lab computer, don’t forget to log off of your machine when you are finished!