Lab 7A: Strings and dictionaries

The focus of this week's lab is on:



Tasks

Notes

L0. The setup

Create a Lab07A folder on your computer where you store CS151 projects and labs, hopefully somewhere that is backed up (e.g. in Dropbox, Google Drive, Microsoft One Drive, etc.)

Open VS Code. Open up a Terminal (e.g. Control+` in VS Code) and change the directory (cd) to the Lab07A folder.


L1. Reading Files, manipulating strings and writing to a file

Download digits.txt and place it in your lab folder.

L1a. Read from file

Write code in a new source code file lab7A_digits.py that reads in the data and stores it as one single-line string. When you print out the string, you should get:
1 2 3 4 5 6 7 8 9 10 11 12

Try doing this two different ways:

  1. concatenation (+ operator)
  2. using lists and the join method

Hint: Each line ends with an invisible new line character. You probably want to discard each one. Recall: you can use either indexing or the strip string method to help with this.

L1b. Split and join

Starting from your string from L2a, create a new string using both split and join methods (without using loops) that when printed, gives (again, not in a loop: print(yourString)):

1
2
3
4
5
6
7
8
9
10
11
12

L1c. Write to file

Write your string from L1b to a new text file called digits2.txt. There should be one number per line of the file.

Look at the python documentation for an example of how to write a string to a file.

This can be done by creating a new file in write mode ("w")

newFile = open("digits2.txt", "w")

Use the file object's write method with a string as the argument, to add that string to your file.

Make sure to close the file using the close method.


L2. Dictionaries

Let's practice using dictionaries!

L2a: Warm up

In a new file called lab7A_dict.py, make a new dictionary and add the following pairs of keys and values:

Print out the values for each of the keys so that your program prints Hi there CS151! on 3 separate lines.

L2b: Create a dictionary based on text from a file

Comment out your code from the previous part. Download abcs.txt and place it in your lab folder.

Open up the text file in VS Code. For each of the 26 letters in the alphabet, there is a phrase for each of the 26 letters of the alphabet. For example:

a is for apple
b is for banana

Your goal is to create a dictionary with:

Your dictionary should have 26 keys — one for every lowercase letter in the alphabet — and the associated value should be that letter's phrase.

After you create the dictionary, assuming it is called letterDict, add the following test code. Running lab7A_dict.py should produce the expected output below:

print(len(letterDict.keys()))  # 26
print(list(letterDict.keys())[:5])  # ['a', 'b', 'c', 'd', 'e']
print(letterDict['a'])  # a is for apple
print(letterDict['b'])  # b is for banana
print(letterDict['z'])  # z is for zebra

Hints:

L2c: Build ABCs string from phrases

Now that you have your phrase dictionary set up, your next goal is to create a single-line string called abcs with the 26 letters of the alphabet (all next to each other, without spaces) using the values of your dictionary. The letter you are looking for is the 1st character of each value in the dictionary.

Printing your string abcs should yield:

abcdefghijklmnopqrstuvwxyz

Hints:

L2d: Create a dictionary based on multi-line text from a file

Make a new source code file called lab7A_dict2.py. Download the file abcs2.txt to your lab directory.

Open up the text file in VS Code. The contents are the same as in the previous subtask, but each phrase spans 3 lines. For example:

a
is for
apple

are the first 3 lines of the file and the text belongs together as a single phrase a is for apple. The next 3 lines make up the text for b is for banana, and so on.

Your goal is to create a dictionary in the same format as the previous subtask. Use the same above tests involving letterDict, they should give you the same outputs.

Tip: We know that the file contains text for 26 letters of the alphabet and that there are 3 lines for each letter's phrase. While we have tended to use a while loop to read from a file, it would be easier to use (nested) for loops here.

After you create the dictionary, assuming it is called letterDict, add the following test code. Running lab7A_dict2.py should produce the expected output below:

print(len(letterDict.keys()))  # 26
print(list(letterDict.keys())[:5])  # ['a', 'b', 'c', 'd', 'e']
print(letterDict['a'])  # a is for apple
print(letterDict['b'])  # b is for banana
print(letterDict['z'])  # z is for zebra

© 2021 Max Bender