Anonymous user
Shakespeare: Difference between revisions
→Lists and Dictionaries Exercises
imported>Zanwenhuahao |
imported>Ynasser |
||
(23 intermediate revisions by 3 users not shown) | |||
Line 5:
==Setup==
# Download https://csclub.uwaterloo.ca/~ehashman/pwfb/ShakespearePlay.zip
# Extract the contents of the folder, as per [https://openhatch.org/wiki/Python_Workshops_for_Beginners/Friday_September_26th_setup_and_tutorial#Goal_.236:_Download_the_Saturday_projects Goal 6] yesterday
# Change directory to inside the folder, for instance:
<pre>cd Downloads/ShakespearePlay</pre>
# Open up the python shell and import some modules:
<pre>
from RomeoJuliet import RomeoJuliet
from summerNight import SummerNight
from summerChar import SummerChar
</pre>
==Goals==
Line 63 ⟶ 72:
=====<font color="navy">Using the word "love"</font>=====
* We will use the play <b> Romeo and Juliet </b>
1.
2. Import RomeoJuliet
3. The variable RomeoJuliet is the play - don't output it, as it's too long. It's in a list.
4. Now create a variable that represents the string "love", for example:
Line 77 ⟶ 86:
>>> lv_counter = 0;
</pre>
6. Use a for loop (or while loop, if you like) to read through the lines of the file. While you are reading each line, count the number of lines that contains the word "love"
7. Does Shakespeare use a lot of love in his plays? How about other synonyms of "love"?
Line 83 ⟶ 92:
====Lists====
* Recall that lists in Python can contain <b>
* We may declare our list as such
<pre>
Line 115 ⟶ 124:
====Dictionaries====
* Dictionaries are another data structure commonly used in programming. Dictionaries
* Entries in the dictionary are stored <b>
* <b>keys</b> for each entry in the dictionary must be unique, <b> values </b> do not have to be unique.
* For our simplicity, we will use a string as keys.
Line 129 ⟶ 138:
</pre>
* The following basic operations
<pre>
>>> del myDict["Interesting"]
Line 142 ⟶ 151:
1
</pre>
For more information on the dictionary data type: https://docs.python.org/2/tutorial/datastructures.html#dictionaries
====<font color="navy">Lists and Dictionaries Exercises====
<b>List & Iteration Exercise 1:</b>
* Create a new python
* Import the list of characters from <b> A Midsummer Night's Dream </b>
<pre>
>>> summerChar.SummerChar
# prints all the characters (such as <HELENA>) that are in the play A Midsummer Night's Dream.
</pre>
<b>List & Iteration Exercise 2:</b>
*
*
<b>Demo: Which of Shakespeare's famous tragic heros talks the most?</b>
<b>List & Iteration Exercise 3*:</b>
* Now iterate through the list of character you saved from exercise 1, see how many times each of them speak.
* Print to screen each character's name and the number of times they spoke.
<b>List & Iteration Exercise 4**:</b>
Line 178 ⟶ 180:
<b>Who speaks the most in Shakespeare's A Midsummer Night's Dream? Can you guess (or if you know) the pairings of the couples in the story?</b></font>
<b>Exercise 1:</b>
* In modern English, we owe many words to William Shakespeare, who alone invented over 2000 words in the English literature.▼
* The file "popularWords.txt" lists some popular words that we still use today that were invented by Shakespeare (bet you didn't know that!). Each word originates from at least one of his plays. Can you find the
* You can save the <b>popular word</b> and <b> the play it comes from </b> as a <b>key:value</b> pair in a dictionary. (remember that value does not have to be a number, it could be arbitrary data structure, i.e. it could be a string or a list of strings)▼
* Give yourself a word that you see in a play. Try to output the entire dialogue spoken by the person that first contained the word.
* i.e. if your word is "fantastical" and you are looking at the play "Twelfth Night", you should output:
Line 203 ⟶ 210:
* Note that each person's speech is opened with their name in angular brackets and ends with </...>, as similar to HTML tags.
<b>Exercise
* Animals are often used as condescending terms to address someone in Shakespeare's plays.
* Using the list of animals given in the file "listAnimal.txt" can you find (in a play or in many plays) which play and who spoke of something as what animal?
<b>Finally...</b>
▲Exercise 3:
* Feel free to devise your own exercises that you would enjoy and find out things about Shakespeare's plays!
▲* In modern English, we owe many words to William Shakespeare, who alone invented over 2000 words in the English literature.
▲* The file "popularWords.txt" lists some popular words that we still use today that were invented by Shakespeare (bet you didn't know that!). Each word originates from at least one of his plays. Can you find the plays where each word originated?
▲* You can save the <b>popular word</b> and <b> the play it comes from </b> as a <b>key:value</b> pair in a dictionary. (remember that value does not have to be a number, it could be arbitrary data structure, i.e. it could be a string)
|