3.5. Python types assignment¶
[Note: this section is part of the online notes but it is really
an document file creatred automatically from an ipython notebook
session, placed here since you think of it as part of your reading.
To do the assignment, you really want to download the ipynb file
.]
This is your first Python assignment. We will accomplish several things
with this assignment. First we will make sure your Python is installed
correctly and that some of the basic components we want it to have are
working correctly, in particular, IPython notebook
. Next we will try
to get you comfortable working with an IPython notebook. You need to be
able to execute the code in it, observe the results, and write down some
of what you see. Equally important, you need to be able to create a new
notebook file that has your work in it, SAVE your work and turn it in.
You will turn your work in by emailing a copy of the .ipynb file to me.
As a last resort, you can save it as an html file, bring that html file
up in your browser, and print the html file to give me a hard copy. In
any case, I recommend saving a hard copy in case anything goes wrong
with the email version of your work that you send me. We go through each
of these steps in turn.
Create your own notebook file. After you have read these directions, , you should pull down the
File
menu that appears just below theIP[y]: Notebook
header and chooseMake a Copy
. This will create a new Tab in your browser containing all the material in this notebook with a new name including the wordCopy
. Then you should pull down theFile
menu that appears just below the IP[y]: Notebook header and chooseRename
. Rename the notebook to be<yourname>_ipython_types
. For example I would rename my assignment to bemark_gawron_python_types
. That will create a new file namedmark_gawron_python_types.ipynb
in the same directory as I started ipython notebook in. This is the file you will work on and eventually attach to an email to me to turn in your homework. You should copy and rename as soon as you can in your notebook session. Following these steps will not change the original notebook file you downloaded to do this assignment. So if anything goes wrong you can go back to that to start over. If you do theRename
step without the copy step you will end up overwriting the original copy you downloaded.Saving. After you have renamed the file, you can save your further work just by pulling down the same file menu and choosing
Save and Checkpoint
. You will notice the menu includes another selectionRevert to Checkpoint
. Once you have established a checkpoint this allows you to revert to that saved state. This is very useful if things go wrong and you get into a state where Python does not seem to respond. More on that below. But just remember this: Save your work. Save it often.Terminating Python and restarting. If Python does not seem to be responding to you when you execute commands that should have a response (like asking for the value of a variable by just typing the variable name), you can restart Python by pulling down the
Kernel
menu and choosingInterrupt
. Or at any time, whether it’s after anInterrupt
or not, you can chooseKernel>Restart
. Caution: This will have the effect of wiping Python’s memory clean. So none of the definitions you have already executed will be in effect. That means if you want to work with an expression that depends on something previously defined, you will have to re-execute all the definitions it depends on. See the section on interacting with IPython notebook.Ending your IPython notebook session. Select
Close and halt
from theFile
menu. This kills the Python process running behind your notebook. But it does not kill the program that is interacting with you through your web browser, called theNotebook Server
. This is still running back in the window you originally started the notebook from. To kill this program, go back that terminal window and typeCtrl-C
. Or just ignore it. It’s not hurting anyone.Creating an html version of your notebook session. Only when you want to create a hard copy of your ipython sesssion. Exit and terminate your notebook session. Then, in the same terminal window you started your ipython notebook session from, do:
ipython nbconvert --to html python_types.ipynb
This will create a new file called
python_types.html
in the same directory. You can open this file up with your browser and print it.Emailing your assignment to me. Email only
.ipynb
files or.py
files. No.html
files. The contents of an.html
can only be turned in as a hard copy. My email address isgawron@mail.sdsu.edu
.
3.5.1. Interacting with IPython Notebook¶
Basic concept. An IPython notebook is a kind of spreadsheet consisting of a sequence of cells of two types, called Code cells and Markdown cells. These two types of cells let you do two different things, enter text and interact with Python. The code cells all begin with
In [<integer>]:
. Any time you wish, you can double click on a code cell and edit it, adding or deleting material. When you’re done editing, position your cursor in the code cell and hit the[Enter]
key while holding down the shift key, which we will henceforth write as[Shift]
-[Enter]
. This will execute the code in that cell and give you Python’s response (if there is one). Keep in mind that Python is responding to the last line typed in the cell and some Python statements don’t all call for a response. For example, if you type2+3
in a code cell and hit[Shift]
-[Enter]
, Python will respond5
, But if you typeX=2+3
, Python says nothing. It merely changes its state so that the variableX
is now defined and now has the value5
.Code cell dependencies. Is everything defined? When you load up a Python notebook, Python doesn’t automatically execute all the code in code cells. It waits for you to tell it to. Frequently a code cell will have expressions that will depend on other expressions being defined or on some Python module being loaded, and those things will have happened in cells above the one you’re editing. In that case, executing the cell you’re editing before executing the cells above will cause a Python error, often a
NameError
. There are two ways to fix this. One is to execute the cell with the needed definition and then re-execute the cell you were editing. This is the scalpel solution. The hammer solution is to pull down theCell
menu and selectRun all above
. As time goes on and our notebooks become more and more complicated, the hammer solution will work less and less often, or you will become impatient with waiting for unnecessary pieces of code to finish executing. At that point it will be a good idea to understand the dependencies among code cells and just execute the cells you need for your current editing purposes. Often this will be as simple as just executing the cells in the section of the notebook you’re working on.Help. Of course the way to get help is to pull down the
Help
menu, but it is worth pointing out that most of the items there are not for beginners. The two most important items areNotebook Help. This takes you to some web pages that are basically a Tutorial on IPython notebook. Highly recommended.
Keyboard shortcuts. This includes all the ways to get things done with keyboard commands, which are often more efficient than menu shuffling. Most of the commands begin with
Ctrl-m
. As with[Shift]
-[Enter]
, this means holding down the first key while pressing the second. SoCtrl-m
means hold down the ``Control`` key while pressing the ``m`` key.
Creating new cells. Eventually you will be doing assignments in which you create new code cells and new markdown cells. For now cells of both types have been provided below for you to put your answers in. When you want to start creating cells, you can use keyboard commands shown in the Keyboard shortcut help that will put new cells above and below the cell your cursor is in. By default a new cell is a code cell. There is also a keyboard shortcut for turning a code cell into a markdown cell.
3.5.2. Tuples¶
To start the assignment, recall the directions you were given in class
about how to use IPython notebook. Position your cursor in the next cell
(numbered 2) and hit the [Enter]
key while holding down the shift
key, which we will henceforth write as [Shift]
-[Enter]
. Nothing
will happen. But you will have defined the variable T to be a tuple with
three members.
T = ('a','b','c')
The cell below contains an error. To see what the error is, position
your cursor in the cell and type [Shift]
-[Enter]
. Explain what
the error is in the cell 2 cells down (labeled
Enter your answer here
). (It is not a NameError
; if you get a
NameError
, that’s because you haven’t executed the cell that defines
the variable T
first.) Explain how you could define T
differently so that the expression in the cell below would not be an
error.
T[1] = '1'
[Enter your answer here by double-clicking on this text. This will open
open this cell up for editing. When you are done typing your answer,
press [Shift]
-[Enter]
to convert your answer into nicely
formatted text. If you change your mind and want to revise an answer,
just double-click on it again.]
In the cell below, write an expression that computes the length of
T
. Execute the expression by typing [Shift]
-[Enter]
and
check that you get the right result. If you don’t remember how to
compute the length of a tuple, you can check this introductory
book-draft material on Python
tuples
OR you can do a Google search on “Python tuple length”.
Higgledypiggledy = [1, ['a','b','c'], (1.4, 'Sam')]
In the next cell, write an expression that retrieves the value
['a','b','c']
from Higgledypiggledy
. For example, the expression
Higgledypiggledy[0]
retrieves the value 1
. Test your answer by
typing [Shift]
-[Enter]
to see what you get. If you get
NameError
, it’s because you didn’t first execute the expression in
the cell above to define the variable Higgledypiggledy
. To do that
position your cursor in the cell above and type [Shift]
-[Enter]
.
In the next cell, type a single expression that retrieves the value
(1.4, 'Sam')
from Higgledypiggledy
and sets avariable to that
value. This answer will be of the form Something = Something
. The
first something will be a variable name. You can choose any name you
want as a variable name, as long as it is all letters (avoid numbers and
special characters for now) and isn’t already reserved as the name of a
builtin Python operator or function. Python will raise a SyntaxError
if you use a reserved name.
[answer here]
In the next cell, type a single expression that retrieves the value
"Sam"
from Higgledypiggledy
. If you can’t think of one
expression that does this, you can use the variable you defined in cell
[5] and retrieve "Sam"
from that. But that won’t be as good because
it takes two steps to retrieve value that could be retrieved in one
step.
3.5.3. Strings¶
The cell below defines a string. Execute that definition. (This is
another way of saying, “Place your cursor in the cell and type
[Shift]
-[Enter]
)”. Notice that when you evaluate this cell,
Python doesn’t seem to do anything. Nevertheless it has changed its
state to reflect the fact that the variable Example_str
has been
defined to denote a particular string.
Example_str = "rather"
In the next cell, write and execute an expression that retrieves the
value "r"
from Example_str
. There are two r’s in the string,
so there are two possible answers:
[answer here]
The string rather
has the nice property that some of its substrings
are English words. In the cell below write a single Python expression
that retrieves the word rat from Example_str
. You might need to
review the lecture material on string
slices.
Evaluate it with [Shift]
-[Enter]
to check your answer.
[answer here]
In the same cell (the cell above), on new lines, write two more
expressions that retrieve two other English words. Each time you add a
line to the cell, type [Shift]
-[Enter]
. Notice that when there’s
more than one line in the cell, Python only seems to be responding to
the last line. This is how the Python Interpreter (the program you are
interacting with when you type commands to Python) works. Despite the
fact that Python only responds to the last line, all the lines are
being executed.
3.5.4. Dictionaries¶
There are two ways to define dictionaries which are both worth knowing. We start with the easiest to type.
dd = dict(name = 'Mark Gawron',
language = 'Python',
favorite_tv_show = 'Monty Python',
favorite_desert = 'Apple pie')
In the next cell down, write a single expression to retrieve the value
"Python"
from dd
.
[answer here]
In the next cell down write an expression that adds information to
dd
. Specifically add the key favorite_baseball_team
and set
its value to be "Chicago Cubs"
. Note that strings can have spaces in
them and still be valid strings.
[answer here]
In the next cell we use a different syntax for defining a dictionary.
letter_counts = {' ': 9, 'e': 5, 'o': 5, 'l': 3, 'd': 2,
'h': 2, 'r': 2, 'u': 2, 'w': 2, 'y': 2, '.': 1,
'a': 1, 'c': 1, 'b': 1, 'g': 1, 'f': 1,
'i': 1, 'k': 1, 'j': 1, 'm': 1, 'n': 1, 'q': 1,
'p': 1, 't': 2, 'v': 1, 'x': 1, 'z': 1}
Notice some of the features of this dictionary. They keys are all single
characters and the values are all integers. Moreover, there is one key
for each letter of the alphabet. Plus there are keys for space and a
punctuation mark, period (‘.’). There are 28 keys in all. In fact this
dictionary stores the number of times each of these 28 characters occurs
in a particular sentence of English. (You are welcome to guess which
one, obviously a sentence that includes all 26 letters of the alphabet).
In the cell below, write an expression that retrieves the value 5
from letter_counts
. (There are two possible answers).
[answer here]
In the cell below write an expression that retrieves the integer
associated with the key q
from letter_counts
.
[answer here]
3.5.5. Filling a Dictionary with information¶
Now let’s look at a more practical example of defining a dictionary.
from collections import Counter
Sentence = 'the quick brown fox jumped over the lazy yellow dog.'
letter_freqs = Counter()
for letter in Sentence:
letter_freqs[letter] += 1
letter_freqs
Counter({' ': 9, 'e': 5, 'o': 5, 'l': 3, 'd': 2, 'h': 2, 'r': 2,
'u': 2, 't': 2, 'w': 2, 'y': 2, '.': 1, 'a': 1, 'c': 1,
'b': 1, 'g': 1, 'f': 1, 'i': 1, 'k': 1, 'j': 1, 'm': 1,
'n': 1, 'q': 1, 'p': 1, 'v': 1, 'x': 1, 'z': 1})
This Counter named letter_freqs
is defined in line 3 and filled with
information in a for
loop that begin on line 4 and ends on line 5.
The variable Sentence
is defined to be a string (line 2) and the
for
loop steps through each character of that string and adds 1 to
its count in the dictionary (line 5). You may have noticed that the keys
and counts in the Counter letter_freqs
were the same as those in the
dictionary letter_counts
, defined in cell [5]. In fact a Counter is
just a dictionary with some special features that make it easier to keep
counts of things. We will be using Counters a lot to store simple
counts. You should convince yourself the counts in letter_freqs
are
right (for example, there really is only one t in the sentence the
quick brown fox jumoed over the lazy yellow dog.)
from collections import Counter
Sentence2 = 'The quick brown fox jumped over the lazy yellow dog.'
letter_freqs2 = Counter()
for letter in Sentence:
letter_freqs2[letter] += 1
letter_freqs2
There is an important difference between the string Sentence
and the
string Sentence2
. What is it? How is the difference reflected in
letter_freqs2
.
[Double click on this text and write your answer here.]