Beruflich Dokumente
Kultur Dokumente
Victor Miclovich 24th May 2009
1.0 Introduction
I hope you have read chapter four by now. In this lecture, I'll look at the applications of
dictionaries but first I take a look at what dictionaries are.
There are times when in real life you want to lookup a definition of a word. Of course, you
might have the word in your mind (but no meaning!), then this word is your key to finding it
in the dictionary.
Let's find out what the “psychosis” means. You take out your dictionary, and search for the page
that's got 'p' words, you move to from 'pa' to 'pb', 'pc', ..., until 'ps' words, you do the same until
you reach the 'psy' words... This is will be the way you search for the key within the dictionary;
but this key/word has got a meaning – value that you badly need.
You might eventually see something like:
psy∙cho∙sis
Pronunciation: \sīˈkōsəs\
Function: noun
Inflected Form(s): plural psy∙cho∙ses \ˌsēz\
Etymology: New Latin
Date: 1847
: fundamental derangement of the mind (as in schizophrenia) characterized by
defective or lost contact with reality especially as evidenced by delusions,
hallucinations, and disorganized speech and behavior
Well, I've had enough talk about use of dictionaries to search for words... let's have a recap of
what dictionaries are in the computing world of Python.
1.0.1 Dictionaries in Python
The format of a dictionary is:
dictionaryName = { key : valueOfKey }
Putting some flesh to the above definition with some real code:
studentScores = { 'chris' : 'A' , 'victor' : 'B', 'ken' : 'A+'}
We can extend the dictionary using ',' – commas, as shown above! The key is the name
string and the value is a score string (using alphabetic representation).
1.0.2 Dictionaries and their methods
Remember, dictionaries are sequences. And the majority of sequences have some
common operations associated with them such as counting the elements/items in them,
inserting new elements and their deletion (nontuple sequences), assignment operations: you
can reassign lists... this means give lists new variable names/references.
1.0.2.1 Looking at the keys of the a dictionary
To check out the keys within a dictionary: check out this annotated code snippet;
>>> d = { 'chris' : 'A', 'vi' : 'B' }
>>> d.keys()
>>> ['chris', 'vi']
1.0.2.2 Getting a value using a key
If d is a dictionary, then:
>>> d[key]
>>> [value sequence]
d = {'psychosis' : “fundamental derangement of the mind (as in schizophrenia)
characterized by defective or lost contact with reality especially as evidenced by delusions,
hallucinations, and disorganized speech and behavior”, 'Food': “something that is eaten :)
” }
Assume that we extend d to contain a link to a database of words and definitions or just
having the definitions inbuilt!
So if some is lost... and doesn't know the meaning of a certain word, all he got to do is type
>>> d['Psychosis']
So let me just implement this in a runnable code format!
d # has already been created/defined
keyword = raw_input(“What definition are you searching for: ”)
# this code will work for words 'psychosis' and 'food'
if keyword.lower() in d:
print “This will work for only psychosis and food!”
print d.upper() + “\n” + d[keyword.lower()]
print “\nThank you for asking”
Note:
I have attached in the email a copy of the actual source code. If you just past the above code
from the pdf, it might fail to run.... So just play with the source code that is put in the
download site: http://code.google.com/p/lpython
Design issues:
Dictionary keys have to always be unique/different/disimilar.
Dictionary values can be anything... the same, unique, whatever you feel like doing with
them.
Health Note
You might be frustrated by the way python handles numbers.... We have several base
systems that are usually used: binary (base 2), octal (base 8), decimal, hexadecimal (base
16). Octal numbers start with a 0, base16 starts with 0x, the others any can do; base 2 is
a bit different; you might hardly use it... but for an assignment you will design an
algorithm for finding the binary value of a number (decimal/hexadecimal/other base)
What do you know about base 2. Well, the only numbers in binary are 0 and 1. So, 310 is
112. That's the mathematical notation for bases and indices.
So...
The deal behind 112 ???
112 = (1 x 21) + (1 x 20)
1112 = (1 x 22) + (1 x 21) + (1 x 20)
1012 = (1 x 22) + (0 x 11) + (1 x 20)
... you can work out other examples...
The formula for binary sequences
Object 1
Tip
A word on running your code: If you are using the IDLE in Windows, you might have to
click file, open, and browse your computer for the downloaded source code.
Also, it might be safe to write your code in script mode... where there is no '>>>' prompt. It
should give you practice at being able to debug (search for error) your software well and
without pain; this is done by [ Ctrl + N ] or click file, then click New, you might be
prompted to save... so don't forget to save your source code with a .py extension, as in
instead of just typing a file name e.g. codeLover, you should type something like
codeLover.py and then click saver.
To run your program you can use the F5 functional key on the standard keyboard (well, if
you program python using an iPod, you might have to direct yourself through its simple
GUI like features!). Another way of running (seeing output) your source is to click the run
option on the menu bar of IDLE; you can search for it there coz it's somewhere there!
NOTE
Read the book well... These
lecture notes are not meant to be
a replacement of the book! The
book and the Python language
reference already have enough
information on the constructs of
Python!