You are on page 1of 4

Learning Journal 8

In this journal I summarized important aspects from the reading, gave some examples and
also solved some exercises from the sections I was having a hard time understanding
(dictionaries). Solving some exercises helped me a bit but there was still an exercise I was not
able to solve.

11 Dictionaries
A dictionary is like a list but in dictionaries there are keys (which are a set of indices) and a set of
values. This is called a key-value pair. Function: dict
Elements of dictionary are never indexed with integer indices. So the order of items is
unpredictable.
In operator shows you the key in the dictionary
ord built in function converts characters to numbers
implementation is a way of performing a computation
histogram is a statistical term for a set of counters (or frequencies)
get takes a key and a default value. If the key appears in the dictionary, get returns the
corresponding value, otherwise it returns the default value.
For statements traverses the keys of the dictionary
Given a dictionary d and a key k, the corresponding value can be found with v=d[k]. This
operation is called lookup. There is no simple syntax for a reverse lookup though. The following
code can be used for this:
def reverse_lookup(d, v):
for k in d:
if d[k] == v:
return k
raise ValueError
raise statement causes an exception, the Value Error, which generally indicates that there is
something wrong with the value of a parameter. In this case it would mean v does not appear in
the dictionary.
Dictionaries and lists:
Singleton is a list that contains a single element
Dictionary implementation uses a hashtable and that means keys have to be hashable.

A hash is a function that takes a value (of any kind) and returns an integer. Dictionaries use these
integers, called hash values, to store and look up key value pairs. This is why dictionary keys
must be immutable. This is also why mutable types like lists are not accepted as keys. Instead
tuples can be used.
Memo is a previously computed value that is stored for later use.
Variables in _main_ are sometimes called global because they can be accessed from any
function. Unlike local variables, which disappear when their function ends, global variables
persist from one function call to the next.
Flags are Boolean variables that indicate whether a condition is true.
To reassign a global variable inside a function you have to declare the global variable before you
used it.

For example
count=0
def example():
global count
count +=1

Exercise 11.1
infile = open(r"C:\Users\Alice\Desktop\words.txt", "r")
dic={}
for word in infile:
dic[word]= ' '
infile.close()
print (dic)

Exercise 11.2
I could not solve this one no matter what I tried. How can I use the command get() to write a
histogram?

Exercise 11.3
def alphabet(word):
dic={}
for letter in word:
if letter not in dic:
dic[letter]= 1
else:
dic[letter] += 1
for key in sorted (dic):
print (key, dic[key])

alphabet('hello')

Chapter 14 Files
Persistant: they run for a long time (or all the time). If they shut down and restart, they pick up
from where they left off.
To write a file, open it with mode w as a second parameter. So example = open(name.txt, w)
When you are done writing you have to close the file. So example.close()
The argument of write has to be a string so if we want to write a number, we have to convert it to
a string. Such as: f.write(str(x))
An alternative is to use the format operator %. For example
Os.getcwd() returs the name of the current directory
num=3
I have %d last names. %num
%d is for an integer
% is for floating point
% is for string
To deal with exceptions in files (ex protected files) you can use the try command.
Os.path.exists checks whether a file or directory exists

Os.path.isdir checks whether its a directory


Os.path.isfile checks whether it is a file.
Os.listdir returns a list of the files in the given directory
Database is a file that is organized for storing data. The module dbm provides an interface for
creating and updating databaces. Ex.
import dbm
db = dbm.open('captions.db', 'c')

You might also like