CS193Q - Day 2

> Nick's Python Guide - maybe open this in a new tab so you can get to its chapters as we go

Today: Install PythonCharm

Install PyCharm "community edition" (free) on your computer. https://www.jetbrains.com/pycharm/download/

PyCharm is an IDE. It's not required for Python, but it's handy.

cs193q-2.zip today's .zip of code

PyCharm: open the Folder containing the code - e.g. cs193q-2. Do not double click a .py file to open, does the wrong thing.

File Reading

Nick demos with poem.txt

print()

cat.py Program - Exercise 1

Python Doctests


Dict Type

>>> d = {}
>>> d['a'] = 'apple'
>>> d['g'] = 'grape'
>>> d['d'] = 'donut'
>>> 
>>> d['a']
'apple'
>>> d.keys()
dict_keys(['a', 'g', 'd'])
>>> 
>>> for key in d.keys():
...   print(key, d[key])
... 
a apple
g grape
d donut
>>> 
>>> # better: go through keys in sorted order
>>> for key in sorted(d.keys()):
...   print(key, d[key])
... 
a apple
d donut
g grape
>>> 
>>> 'a' in d
True
>>> '' in d
False

Dict Count Algorithm - ip-count.py

Tuple Type

Dict .items()

Python No Copies - Shallow

Python does not by default ever make a copy. It's always pointers!

Make a list. Put it in a dict with =. There is just one list! Modify it inside the dict, you are modifying the one original list. This is the "no copies" strategy that runs through Python. It's fine! Your code can just work this way. Python: there is just the one list, and using =, we just send references to that one around.

>>> lst = ['aaa', 'bbb']
>>> d = {}
>>> d[1] = lst
>>> 
>>> lst
['aaa', 'bbb']
>>> d
{1: ['aaa', 'bbb']}
>>> d[2] = []
>>> 
>>> d
{1: ['aaa', 'bbb'], 2: []}
>>> 
>>> 
>>> d[1].append('ccc')
>>> 
>>> d
{1: ['aaa', 'bbb', 'ccc'], 2: []}
>>> 
>>> 
>>> lst
['aaa', 'bbb', 'ccc']. I

Note: both list and dict have a .copy() method if you need it, but generally you don't need this. I've written tons of production python code, and I never needed to use .copy().


Comprehensions

Super handy way to compute a new list from a list. Here are the steps

1. Write outer [ ]

2. Write "for elem in lst" inside

3. Write expr on the left that you want to compute each elem in the new list

4. Write "if xxx" at the right side, to trim results if wanted

>>> lst = [1, 2, 3, 4]
>>> 
>>> [n * n  for n in lst ]
[1, 4, 9, 16]
>>> 
>>> [str(n) + '!'  for n in lst ]
['1!', '2!', '3!', '4!']
>>> 
>>> 
>>> [str(n) + '!'  for n in lst if n >= 2]
['2!', '3!', '4!']

Map Lambda (optional)

>>> lst = [2, 1, 3, 6]
>>> 
>>> 
>>> def double(n):
...   return n * 2
... 
>>> list(map(double, lst))   # map the def def
[4, 2, 6, 12]
>>> 
>>> list(map(lambda n: 2 * n, lst))  # use lambda!
[4, 2, 6, 12]
>>>
>>> list(map(lambda n: n + 1, lst))
[3, 2, 4, 7]
>>> 
>>> list(map(lambda n: n * n, lst))
[4, 1, 9, 36]
>>>
>>> list(map(lambda n: str(n) + '!!', lst))
['2!!', '1!!', '3!!', '6!!']

Custom Sort - Food Example

Want to sort by tasty value alt: circle tastiness for sorting

Use lambda to project out that value alt: project out tasty

Food Sort Examples

Custom Sort upper/lower

Custom Sort String upper/lower

Python Custom Sort Example


Sorted vs. Dict Count Items

Conclusions