The goal of this exercise is to practice Python in IDLE: aim for an hour or so. You should also try the two problem sets below. Where a command is obscured, it is your job to figure out the missing part that will produce the output shown. Where an output is blocked out, make a guess at it first, and then execute the command to see if you were correct.
Then, continue to review what we learned in class. Feel free to explore and try your own commands. Don't worry about mistakes and error messages -- you should be making lots of them!
When you're done, submit two things through the "Exercise #1" assignment link on CourseWeb:
Answers to the two problem sets. Write it in the text box.
Your IDLE shell session saved as a text file. Name it "ex1.Your-last-name.txt". Submit as a file attachment.
That's it! If you are looking to learn more, try A Beginner's Python Tutorial, Lesson 2 and Lesson 3.
Exercise 2
In this exercise, you will write your own Python script file. The file should be named "ex2.Your-last-name.py". (NOTE: Do NOT do this exercise as an IDLE shell session. You should be writing and executing a Python script file.)
Start your script by declaring the variable sent as follows:
sent = 'Roses are red, violets are blue.'
And then follow it with these two print commands:
print sent print sent.upper()
Save your script and execute it in the IDLE shell to make sure that it works. You should have an output like this. Now, your task is to compose seven additional print commands, such that your script will produce the following nine lines of output when executed:
Roses are red, violets are blue.
ROSES ARE RED, VIOLETS ARE BLUE.
Roses-are-red,-violets-are-blue.
Rosxxs arxx rxxd, violxxts arxx bluxx.
Roses are very red, violets are very blue.
She said: Roses are red, violets are blue.
['Roses', 'are', 'red,', 'violets', 'are', 'blue.']
6
The sentence is 6 words long.
Note that you should not just print the lines above verbatim (that wouldn't make much of an exercise now, would it?). Instead, each print command of yours should produce the desired output by manipulating the string content of sent. That is, each print command must include a reference to sent, utilizing string operations we learned in class today. You shouldn't need any additional commands or statements: basically, your script should look like this.
Hint: the best approach is to make sure each print command works as intended before moving on to the next one. That is, proceed in cycle: compose a print command -> save -> execute script -> verify output -> repeat.
If you have trouble, go back and review the course slides from today. When you are done, upload your "ex2.Your-last-name.py" file onto Exercise #2 item on CourseWeb.
She said: ROSES-ARE-RED,-VIOLETS-ARE-BLUE.
Roses Are Red, Violets Are Blue.
Exercise 3
For this exercise, review what we covered in class today in IDLE shell. Aim for an hour or so. Go over the examples covered in class, and also try your own examples. Feel free to explore! When you feel you've learned enough, save your IDLE shell session as a text file and upload it.
What you need to use is the while keyword we learned in class today. Note that you will need to adjust the indentation of many of your code blocks; be careful with them!
Also, make your palindrome program begin with a greeting when executed:
Hello! Let's find a palindrome.
and end with the following:
Goodbye.
Note that these two should be outside the while loop.
When you're done, upload your script to the Exercise 4 item on CourseWeb.
2. Trying Out Scripts
For each of the scripts below, do the following.
Script C (<-- What is everything? Everything of what?)
Examine it first and figure out what it does just by looking. Write down how you think the process works, and what the output will be (pre-execution guess). Then, type up the script and execute it. Examine the output and write down: if your guess was correct/incorrect, and additional explanations on where your guess went wrong and why the program does what it does (post-execution summary).
Submit a word document (PDF also works) containing the following items, per script:
The script itself. You may use screenshot* OR copy-and-paste the text from the IDLE editor window.
The program output. Again, you can use screenshot or copy-and-paste the text from the IDLE shell window.
Your pre-execution guess
Your post-execution summary
*Crop your image properly when using screenshot -- no gigantic image of your entire desktop please! To find out how cropping works in MS Word, see this image or this video tutorial.
There are a total of 8 points of edit, marked with [1] -- [8]. Start with the first one and proceed to the next. Pay attention to the function definitions and also the instructions embedded as comments. Make sure your script output matches the intended correct output.
When you're done, save your script as Ex5.tale.YOUR-LAST-NAME.py and upload it on CourseWeb.
Your getFreq() function should work like this. I won't show you the output for the Gettysburg Address, but for your reference, here's a sample output file produced when the script is run on this file instead.
There are a total of 8 points of edit, marked with [1] -- [8]. Start with the first one and proceed to the next. Pay attention to the function definitions and also the instructions embedded as comments.
When you're done, upload two files on CourseWeb: your script saved as Ex6.gettysburg.YOUR-LAST-NAME.py, and the output file.
STEP 3
Download this file: words.txt. This is an open-source list of English word types. Open it in your IDLE shell and then read in the LINES in the file as a list of strings, using .readlines() method. Close up your file.
>>> f = open(r'text-processing\words.txt')
>>> wlist = f.readlines()
>>> f.close()
Now answer the following questions. With the exception of Question 2, every solution must involve use of list comprehension. In a document (.txt, .docx, or .pdf), write up your answers to each question and your Python solution (paste the portion of your IDLE shell that produced the answers -- see *Note below).
Question 1: Remove line breaks from words
You will notice that the words in the list have a line break '\n' character at the end. Compose a one-liner list comprehension that creates a new list of words without line breaks, e.g.:
Question 2: Explore the word list
How many words are in this list? What's the first word? What's the 1000th word? What's the last word? You probably realized that the list is not in fact alphabetically ordered. Sort the list, and then find the last word: what is it?
Question 3: Words with certain substrings
What are the words that have 'vv' in them? How many words start with 'un' and end in 'able'? What's the longest 'un...able' word?
Question 4: With vowels and without
How many words have all 5 English vowels? How many have no vowels?
Make sure you count both 'E' and 'e' as a vowel.
Question 5: Length
How many words are 15 letters or longer? How long is the longest word? What's the average length of all words?
Question 6: Palindrome
Find all palindromes that are 5 characters or longer. How many are there?
Question 7: Word-initial characters
What are the top 3 most common letters beginning a word? What are their counts?
You should first make a list of word-initial characters. (We tried this in class.) Make sure to fold case, i.e., turn upper case into lower case, while doing so. Then use getFreq().
Question 8: No repeat
What is the longest word where each letter occurs only once?
Question 9: Maximum variety
What's the largest # of different alphabetic characters present in a single word? What's the shortest word to have that many?
Again, use set() and len(). Additionally, you might find max() useful:
>>> max([1, 1, 3, 9, 5])
9
Question 10: Your own question
Anything else you want to investigate? Pick your own question and find the answer.
When you're done, upload your write-up document to CourseWeb.
*Note: Python code in a document looks better in fixed-width fonts such as Consolas or Courier, which keep characters neatly aligned. Alternatively, you may paste in screen-shots of your IDLE shell window in a MS-Word document.
Exercise 8
For this exercise, study Python for 1+ hour and turn in evidence of your work. The evidence can be in the form of: saved IDLE shell sessions, your scripts, your notes, and others. Suggested activities:
Try out Python commands we covered in class
Compile a summary note on Python commands and functions