Shakespeare: Difference between revisions

imported>Ynasser
imported>Ynasser
 
(14 intermediate revisions by the same user not shown)
Line 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 92:
 
====Lists====
* Recall that lists in Python can contain <b>arbitrarymultiple objectskinds of items (numbers, strings, etc.)</b> and <b>dynamically expand</b> as new things "arrive"are added.
* We may declare our list as such
<pre>
Line 124:
 
====Dictionaries====
* Dictionaries are another data structure commonly used in programming. Dictionaries (think of dictionary in real life) storesstore what we call <b> key-value pairs</b> (kind of like dictionaries in real life). One common thing to do with dictionary is determine an entry's value given its key.
* Entries in the dictionary are stored <b> orderlesswithout order </b> (i.e. no way to arrange storage position according to value)
* <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 138:
</pre>
 
* The following basic operations workswork with entries in a dictionary (remove an entry, add an entry, check if a key is in the dictionary, print the value corresponding to a given key):
<pre>
>>> del myDict["Interesting"]
Line 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 programfile.
* Import the list of characters from <b> A Midsummer Night's Dream </b> by importing <code>summerChar</code> and import the play <code>summerNight</code>
 
<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>
* In the same programfile that you created fromin exercisethe 1previous (or start a new file, but don't forget to importexercise:
* Count how many times has <b> <OBERON> </b>has spokespoken.
* Print to screen the name <b><OBERON></b> and the number of times he spoke.
 
<b>Demo: Which of Shakespeare's famous tragic heros talks the most?</b>
Anonymous user