The focus of this week's lab is on:
if __name__ == '__main__). Please take these tasks seriously, they are designed to prepare you for this week's project.Create a Lab07 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 Lab07 folder.
Create a new Python source code file called lab7_pi.py.
Download pi.txt and place it in your lab folder.
Write code in lab7.py that reads the data from the file and make a single string. Printing it out should yield:3.141592653589793238462643383279502884197169399375105820974
Do this in two ways:
+ operator)join methodHint: 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.
Download digits.txt and place it in your lab folder.
Write code in a new source code file lab7_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
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
Write your string from L2b to a new text file called digits2.txt. There should be one number per line of the file.
Let's practice using dictionaries!
In a new file called lab7_dict.py, make a new dictionary and add the following pairs of keys and values:
key: 'a' value: 'Hi'key: 'b' value: 'there'key: 'c' value: 'CS151!'Print out the values for each of the keys so that your program prints Hi there CS151! on 3 separate lines (or on a single line, if you prefer!).
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:
'a', 'b', etc.'a is for apple', 'b is for banana', etc.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 lab7_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:
Make a new source code file called lab7_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.
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:
Your next task is to create a single string words containing the distinct nouns from each phrase (the last word; e.g. apple, banana, cookie, ...) separated by a space.
Printing your string words should yield:
apple banana cookie donut everyone flipper goat happy igloo jump kook lamb morning noon open potato quiet rooster stamp truck umbrella viking water xylitol yippie zebra
Hints:
Running lab7_dict2.py should print out both abcs and words.
Once you are done with each of the lab tasks, you may start on the project.
© 2021 Oliver Layton