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.
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.
login.py
to generate a person's login id. Use the following algorithm:
login
= login.lower() to create an id that has all lowercase letters.
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!
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.
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).
Dictionaries are also useful data structures.
dict_practice.py
. Within this file write commands implement the following algorithm:
scoreDict
that has the names “Joe”, “Tom”,
“Barb”, “Sue”, “Sally”
associated with the scores 10, 23, 13, 19 and 12 respectively.scoreDict, print an
expression that computes Barb’s score.
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:
hideturtle(): Tells the turtle to hide the arrow
that is usually on the screen. Hiding the turtle generally increases
the speed of the animation.goto(): Tells the turtle to go directly to the
specified location. The location given may be a tuple.write(): Tells the turtle to write the specified
string to the screen at its current location. An optional second keyword
argument allows the user to specify characteristics of the font:font=(<font_name>, <font_size>,
<font_descriptor>)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):
|
![]() |
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!