From 9886944b9fc2c7045578d333770ac35a54f38f70 Mon Sep 17 00:00:00 2001 From: x Date: Mon, 24 Jun 2019 10:21:04 +0200 Subject: [PATCH 01/14] ch11 notes --- code/ch11 dictionary.ipynb | 88 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 code/ch11 dictionary.ipynb diff --git a/code/ch11 dictionary.ipynb b/code/ch11 dictionary.ipynb new file mode 100644 index 0000000..719c923 --- /dev/null +++ b/code/ch11 dictionary.ipynb @@ -0,0 +1,88 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'a': 1}\n", + "\n", + "1\n", + "\n", + "5\n", + "{'a': 1}\n" + ] + } + ], + "source": [ + "a_dic = {}\n", + "a_dic['a'] = 1\n", + "print(a_dic) \n", + "print(type(a_dic.get('a',0)))\n", + "print(a_dic.get('a',0))\n", + "print()\n", + "\n", + "print(a_dic.get('b',5))\n", + "print(a_dic)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'h': 2, 'a': 2}\n" + ] + } + ], + "source": [ + "def count_letters(string):\n", + " \"\"\"A fuction used to count letters in a string.\"\"\"\n", + " d = dict()\n", + " for char in string:\n", + " d[char] = d.get(char, 0) + 1 \n", + " return d\n", + "\n", + "# Problem with my code:\n", + "# def count_letters(string):\n", + "# \"\"\"A fuction used to count letters in a string.\"\"\"\n", + "# d = dict()\n", + "# for char in string:\n", + "# d[char] = d.get(d[char], 0) + 1 \n", + "# return d\n", + "# function get() takes a KEY and a default value, but I passed a value (d[char]) instead a key (char) to it\n", + "a = count_letters('haha')\n", + "print(a)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 9c990edb5a269c3054b5f7cafbc68cce48089ac3 Mon Sep 17 00:00:00 2001 From: x Date: Tue, 25 Jun 2019 10:40:20 +0200 Subject: [PATCH 02/14] finished chapter 11 --- code/ch11 dictionary.ipynb | 70 +++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/code/ch11 dictionary.ipynb b/code/ch11 dictionary.ipynb index 719c923..2a96392 100644 --- a/code/ch11 dictionary.ipynb +++ b/code/ch11 dictionary.ipynb @@ -32,7 +32,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -62,6 +62,74 @@ "a = count_letters('haha')\n", "print(a)" ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "defaultdict(, {'h': 2, 'a': 2})\n" + ] + } + ], + "source": [ + "from collections import defaultdict\n", + "\n", + "def count_letters_two(string):\n", + " d = defaultdict(int) # default value of int is 0\n", + " for char in string:\n", + " d[char] += 1\n", + " return d\n", + "\n", + "b = count_letters_two('haha')\n", + "print(b)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['spam', 'spam', 'spam', 'spam', 'spam', 'spam', 'eggs', 'spam']\n" + ] + } + ], + "source": [ + "food_list = 'spam spam spam spam spam spam eggs spam'.split()\n", + "print(food_list)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "defaultdict(, {'TX': ['Austin', 'Houston', 'Dallas'], 'NY': ['Albany', 'Syracuse', 'Buffalo', 'Rochester'], 'CA': ['Sacramento', 'Palo Alto'], 'GA': ['Atlanta']})\n" + ] + } + ], + "source": [ + "city_list = [('TX','Austin'), ('TX','Houston'), ('NY','Albany'), ('NY', 'Syracuse'), ('NY', 'Buffalo'), \n", + " ('NY', 'Rochester'), ('TX', 'Dallas'), ('CA','Sacramento'), ('CA', 'Palo Alto'), ('GA', 'Atlanta')]\n", + "\n", + "state_city_dict = defaultdict(list)\n", + "for state, city in city_list:\n", + " state_city_dict[state].append(city)\n", + " \n", + "print(state_city_dict)" + ] } ], "metadata": { From ccd4aa9e6c01801c43df904fb62bd09d3b7ca247 Mon Sep 17 00:00:00 2001 From: x Date: Tue, 25 Jun 2019 10:40:40 +0200 Subject: [PATCH 03/14] read through chapter 14 --- code/ch14 files.ipynb | 739 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 739 insertions(+) create mode 100644 code/ch14 files.ipynb diff --git a/code/ch14 files.ipynb b/code/ch14 files.ipynb new file mode 100644 index 0000000..dc6713b --- /dev/null +++ b/code/ch14 files.ipynb @@ -0,0 +1,739 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import os" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'C:\\\\Users\\\\zheng\\\\Documents\\\\python\\\\ThinkPython2\\\\code'" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "os.getcwd()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:1: DeprecationWarning: The Windows bytes API has been deprecated, use Unicode filenames instead\n", + " \"\"\"Entry point for launching an IPython kernel.\n" + ] + }, + { + "data": { + "text/plain": [ + "b'C:\\\\Users\\\\zheng\\\\Documents\\\\python\\\\ThinkPython2\\\\code'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "os.getcwdb()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'C:\\\\Users\\\\zheng\\\\Documents\\\\python\\\\ThinkPython2\\\\code\\\\ch11 dictionary.ipynb'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "os.path.abspath('ch11 dictionary.ipynb')" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "os.path.exists('ch11 dictionary.ipynb')" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "os.path.exists('.ipynb_checkpoints')" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "os.path.isdir('.ipynb_checkpoints')" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['.ipynb_checkpoints',\n", + " '158-0.txt',\n", + " 'ackermann.py',\n", + " 'ackermann_memo.py',\n", + " 'anagram_db.py',\n", + " 'anagram_sets.py',\n", + " 'analyze_book1.py',\n", + " 'analyze_book2.py',\n", + " 'analyze_book3.py',\n", + " 'BadKangaroo.py',\n", + " 'birthday.py',\n", + " 'c06d',\n", + " 'Card.py',\n", + " 'Card_test.py',\n", + " 'cartalk1.py',\n", + " 'cartalk2.py',\n", + " 'cartalk3.py',\n", + " 'ch11 dictionary.ipynb',\n", + " 'ch14 files.ipynb',\n", + " 'Circle.py',\n", + " 'double.py',\n", + " 'do_four.py',\n", + " 'draw.py',\n", + " 'find_duplicates.py',\n", + " 'find_duplicates_copy.py',\n", + " 'flower.py',\n", + " 'GoodKangaroo.py',\n", + " 'grid.py',\n", + " 'has_duplicates.py',\n", + " 'homophone.py',\n", + " 'inlist.py',\n", + " 'interlock.py',\n", + " 'invert_dict.py',\n", + " 'koch.py',\n", + " 'letters.py',\n", + " 'list_exercises.py',\n", + " 'Map.py',\n", + " 'markov.py',\n", + " 'markov2.py',\n", + " 'metathesis.py',\n", + " 'most_frequent.py',\n", + " 'pace_calc.py',\n", + " 'palindrome_soln.py',\n", + " 'pi.py',\n", + " 'pie.py',\n", + " 'Point1.py',\n", + " 'Point1_soln.py',\n", + " 'Point2_soln.py',\n", + " 'PokerHand.py',\n", + " 'PokerHandSoln.py',\n", + " 'polygon.py',\n", + " 'pronounce.py',\n", + " 'reducible.py',\n", + " 'reverse_pair.py',\n", + " 'rotate.py',\n", + " 'rotate_pairs.py',\n", + " 'run_all_solutions.py',\n", + " 'sed.py',\n", + " 'see NY.text',\n", + " 'spiral.py',\n", + " 'structshape.py',\n", + " 'Time1.py',\n", + " 'Time1_soln.py',\n", + " 'Time2.py',\n", + " 'Time2_soln.py',\n", + " 'typewriter.py',\n", + " 'unstable_sort.py',\n", + " 'walk.py',\n", + " 'wordlist.py',\n", + " 'words.txt',\n", + " 'zipf.py']" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "os.listdir()" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "def walk(dirname):\n", + " for name in os.listdir(dirname):\n", + " a = os.path.join(dirname,name)\n", + " # pay attention to the indentation needed!\n", + " if os.path.isdir(a):\n", + " walk(a)\n", + " else:\n", + " print(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\config\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\description\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\FETCH_HEAD\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\HEAD\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\hooks\\README.sample\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\index\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\info\\exclude\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\logs\\HEAD\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\logs\\refs\\heads\\master\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\logs\\refs\\remotes\\origin\\add-license-1\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\logs\\refs\\remotes\\origin\\master\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\objects\\09\\3610598ce6e1b537a1de8871366c7682605359\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\objects\\60\\dd5aaa4b24ab35ff9b0a29255e62d7a9b835d9\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\objects\\71\\9c9231144e0d06d16d75b9ba202687938f1e0e\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\objects\\98\\86944b9fc2c7045578d333770ac35a54f38f70\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\objects\\pack\\pack-07d17babc0231a6043a91c9070f5532032cd2d36.idx\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\objects\\pack\\pack-07d17babc0231a6043a91c9070f5532032cd2d36.pack\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\refs\\heads\\master\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\refs\\remotes\\origin\\add-license-1\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\refs\\remotes\\origin\\master\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.gitignore\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\back.png\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\book.tex\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\assign2.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\assign2.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\assign2.fig.bak\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\assign2.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\banana.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\banana.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\banana.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\card1.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\card1.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\card1.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\class1.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\class1.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\class1.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\compile.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\compile.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\compile.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\dict1.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\dict1.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\dict1.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\dict2.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\dict2.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\dict2.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\fibonacci.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\fibonacci.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\fibonacci.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\flower.test.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\flowers.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\flowers.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\interpret.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\interpret.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\interpret.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\koch.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\koch.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\list1.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\list1.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\list1.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\list2.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\list2.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\list2.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\list3.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\list3.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\list3.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\liststate.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\liststate.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\liststate.fig.bak\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\liststate.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\listsum1.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\listsum1.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\listsum2.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\listsum2.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\loop.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\loop.py~\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\pies.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\pies.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\point.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\point.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\point.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\rectangle.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\rectangle.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\rectangle.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\rectangle2.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\rectangle2.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\rectangle2.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack2.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack2.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack2.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack3.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack3.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack3.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack4.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack4.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack4.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack5.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack5.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack5.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state2.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state2.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state2.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state3.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state3.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state3.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state4.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state4.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state4.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state5.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state5.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state5.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\time.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\time.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\time.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\towers.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\towers.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\towers.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\tuple1.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\tuple1.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\tuple1.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\footer.html\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\header.html\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\hevea.sty\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\htmlonly\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\latexonly\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\localdef.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\Makefile\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\next.png\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\up.png\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\.ipynb_checkpoints\\ch11 dictionary-checkpoint.ipynb\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\.ipynb_checkpoints\\ch14 files-checkpoint.ipynb\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\158-0.txt\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\ackermann.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\ackermann_memo.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\anagram_db.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\anagram_sets.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\analyze_book1.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\analyze_book2.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\analyze_book3.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\BadKangaroo.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\birthday.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\c06d\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\Card.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\Card_test.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\cartalk1.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\cartalk2.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\cartalk3.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\ch11 dictionary.ipynb\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\ch14 files.ipynb\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\Circle.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\double.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\do_four.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\draw.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\find_duplicates.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\find_duplicates_copy.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\flower.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\GoodKangaroo.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\grid.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\has_duplicates.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\homophone.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\inlist.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\interlock.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\invert_dict.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\koch.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\letters.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\list_exercises.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\Map.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\markov.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\markov2.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\metathesis.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\most_frequent.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\pace_calc.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\palindrome_soln.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\pi.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\pie.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\Point1.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\Point1_soln.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\Point2_soln.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\PokerHand.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\PokerHandSoln.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\polygon.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\pronounce.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\reducible.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\reverse_pair.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\rotate.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\rotate_pairs.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\run_all_solutions.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\sed.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\see NY.text\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\spiral.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\structshape.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\Time1.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\Time1_soln.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\Time2.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\Time2_soln.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\typewriter.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\unstable_sort.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\walk.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\wordlist.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\words.txt\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\zipf.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\LICENSE\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\python_thinking\\Frame.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\README.md\n" + ] + } + ], + "source": [ + "b = walk('C:\\\\Users\\\\zheng\\\\Documents\\\\python\\\\ThinkPython2')" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['158-0.txt', 'ackermann.py', 'ackermann_memo.py', 'anagram_db.py', 'anagram_sets.py', 'analyze_book1.py', 'analyze_book2.py', 'analyze_book3.py', 'BadKangaroo.py', 'birthday.py', 'c06d', 'Card.py', 'Card_test.py', 'cartalk1.py', 'cartalk2.py', 'cartalk3.py', 'ch11 dictionary.ipynb', 'ch14 files.ipynb', 'Circle.py', 'double.py', 'do_four.py', 'draw.py', 'find_duplicates.py', 'find_duplicates_copy.py', 'flower.py', 'GoodKangaroo.py', 'grid.py', 'has_duplicates.py', 'homophone.py', 'inlist.py', 'interlock.py', 'invert_dict.py', 'koch.py', 'letters.py', 'list_exercises.py', 'Map.py', 'markov.py', 'markov2.py', 'metathesis.py', 'most_frequent.py', 'pace_calc.py', 'palindrome_soln.py', 'pi.py', 'pie.py', 'Point1.py', 'Point1_soln.py', 'Point2_soln.py', 'PokerHand.py', 'PokerHandSoln.py', 'polygon.py', 'pronounce.py', 'reducible.py', 'reverse_pair.py', 'rotate.py', 'rotate_pairs.py', 'run_all_solutions.py', 'sed.py', 'see NY.text', 'spiral.py', 'structshape.py', 'Time1.py', 'Time1_soln.py', 'Time2.py', 'Time2_soln.py', 'typewriter.py', 'unstable_sort.py', 'walk.py', 'wordlist.py', 'words.txt', 'zipf.py']\n", + "['ch11 dictionary-checkpoint.ipynb', 'ch14 files-checkpoint.ipynb']\n" + ] + } + ], + "source": [ + "for a,b,c in os.walk(os.getcwd(),topdown=True):\n", + "# print(a)\n", + "# print(b)\n", + " print(c)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [], + "source": [ + "import dbm" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [], + "source": [ + "db = dbm.open('captions', 'c')" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [], + "source": [ + "db['bikinipants.png'] = 'A photo of bikinipants'" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "b'A photo of bikinipants'" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "db['bikinipants.png']" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [], + "source": [ + "db['bikinipants.png'] = 'bikinipants swimming'" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "b'bikinipants swimming'" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "db['bikinipants.png']" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "b'bikinipants.png' b'bikinipants swimming'\n" + ] + } + ], + "source": [ + "for key in db:\n", + " print(key, db[key])" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [], + "source": [ + "db.close() \n", + "# three files created, .DAT .DIR & .BAK\n", + "#.bak a backup copy of a file" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [], + "source": [ + "import pickle" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "b'\\x80\\x03]q\\x00(K\\x01K\\x02K\\x03e.'\n" + ] + } + ], + "source": [ + "l1 = [1,2,3]\n", + "s1 = pickle.dumps(l1)\n", + "print(s1)" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3]\n" + ] + } + ], + "source": [ + "l2 = pickle.loads(s1)\n", + "print(l2)" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l1 == l2" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l2 is l1" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "import wc" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "156\n", + "wc\n" + ] + } + ], + "source": [ + "print(wc.line_count('C:\\\\Users\\\\zheng\\\\Documents\\\\python\\\\ThinkPython2\\\\code\\\\ch11 dictionary.ipynb'))\n", + "print(wc.__name__)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 7cb65b217c87e3e867a15d9705cdc100f288ddda Mon Sep 17 00:00:00 2001 From: x Date: Tue, 25 Jun 2019 10:40:58 +0200 Subject: [PATCH 04/14] chapter 14 exercise --- code/wc.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 code/wc.py diff --git a/code/wc.py b/code/wc.py new file mode 100644 index 0000000..e2b0d5d --- /dev/null +++ b/code/wc.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python +# coding: utf-8 + +# In[ ]: + + +def line_count(filename): + """A function counts how many lines a file has.""" + cnt = 0 + for line in open(filename): + cnt += 1 + return cnt + +if __name__ == '__main__': + print(line_count('wc.py')) + From 5cc9c8a8a933befede80c918844f49b087daf88f Mon Sep 17 00:00:00 2001 From: x Date: Fri, 28 Jun 2019 09:40:16 +0200 Subject: [PATCH 05/14] stage --- code/wc.ipynb | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 code/wc.ipynb diff --git a/code/wc.ipynb b/code/wc.ipynb new file mode 100644 index 0000000..9c89842 --- /dev/null +++ b/code/wc.ipynb @@ -0,0 +1,53 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "__main__\n", + "16\n" + ] + } + ], + "source": [ + "def line_count(filename):\n", + " \"\"\"A function counts how many lines a file has.\"\"\"\n", + " cnt = 0\n", + " for line in open(filename):\n", + " cnt += 1\n", + " return cnt\n", + "\n", + "print(__name__)\n", + "\n", + "if __name__ == '__main__':\n", + " print(line_count('wc.py'))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From a2caa2b63f41971d6d6292c4ef9e095d1b6f6f51 Mon Sep 17 00:00:00 2001 From: x Date: Fri, 28 Jun 2019 09:40:29 +0200 Subject: [PATCH 06/14] chap 17 --- code/Think Python Ch17 Class and Method.ipynb | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 code/Think Python Ch17 Class and Method.ipynb diff --git a/code/Think Python Ch17 Class and Method.ipynb b/code/Think Python Ch17 Class and Method.ipynb new file mode 100644 index 0000000..33d3cbd --- /dev/null +++ b/code/Think Python Ch17 Class and Method.ipynb @@ -0,0 +1,75 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "class Time:\n", + " \"\"\"A class represents time.\"\"\"\n", + " \n", + " def print_time(self):\n", + " print('%.2d:%.2d:%.2d' % (self.hour,self.minute,self.second))\n", + " \n", + " def time_to_int(self):\n", + " \"\"\"A method convert a time to seconds.\"\"\"\n", + " sec = self.hour * 3600 + self.minute * 60 + self.second\n", + " return sec\n", + " def increment(self, sec):\n", + " sec += self.time_to_int()\n", + " return int_to_time(sec)\n", + " \n", + "def int_to_time(sec):\n", + " time = Time()\n", + " time.minute, time.second = divmod(sec,60)\n", + " time.hour, time.minute = divmod(time.minute, 60)\n", + " return time" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "05:03:00\n" + ] + } + ], + "source": [ + "t1 = Time()\n", + "t1.hour=5\n", + "t1.minute=1\n", + "t1.second=0\n", + "t2 = Time.increment(sec=120, self=t1)\n", + "t2.print_time()\n", + "\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From cd2f124a7aa2fca7881316c29216f5cba354ee1b Mon Sep 17 00:00:00 2001 From: x Date: Fri, 28 Jun 2019 09:40:37 +0200 Subject: [PATCH 07/14] chap 16 --- ...nk Python Ch16 Classes and Functions.ipynb | 149 ++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 code/Think Python Ch16 Classes and Functions.ipynb diff --git a/code/Think Python Ch16 Classes and Functions.ipynb b/code/Think Python Ch16 Classes and Functions.ipynb new file mode 100644 index 0000000..93e918e --- /dev/null +++ b/code/Think Python Ch16 Classes and Functions.ipynb @@ -0,0 +1,149 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "write functions that take\n", + "programmer-defined objects as parameters and return them as results." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "class Time:\n", + " \"\"\" A class represents time\n", + " Atributes: hour, minute, second\n", + " \"\"\"\n", + " \n", + "def print_time(t):\n", + " print('%.2d:%.2d:%.2d' % (t.hour, t.minute, t.second))\n", + " \n", + " \n", + "def is_after(t1, t2):\n", + "# print_time(t1, t2)\n", + " return (t1.hour, t1.minute, t1.second) > (t2.hour, t2.minute, t2.second)\n", + "\n", + "def add_time(t1,t2):\n", + " \"\"\"A function to increase time t1 by duration t2.\"\"\"\n", + " if t1.second + t2.second < 60:\n", + " t1.second += t2.second\n", + " if t1.minute + t2.minute < 60:\n", + " t1.minute += t2.minute\n", + " if t1.hour + t2.hour < 24:\n", + " t1.hour += t2.hour\n", + " else:\n", + " t1.hour = t1.hour + t2.hour - 24\n", + " else:\n", + " t1.minute = t1.minute + t2.minute - 60\n", + " if t1.hour + t2.hour + 1 < 24:\n", + " t1.hour = t1.hour + t2.hour +1\n", + " else:\n", + " t1.hour = t1.hour + t2.hour - 23\n", + " else:\n", + " t1.second = t1.second + t2.second - 60\n", + " if t1.minute + t2.minute + 1 < 60:\n", + " t1.minute = t1.minute + t2.minute + 1\n", + " if t1.hour + t2.hour < 24:\n", + " t1.hour += t2.hour\n", + " else:\n", + " t1.hour = t1.hour + t2.hour - 24\n", + " else:\n", + " t1.minute = t1.minute + t2.minute -59\n", + " if t1.hour + t2.hour + 1 < 24:\n", + " t1.hour = t1.hour + t2.hour + 1\n", + " else: \n", + " t1.hour = t1.hour + t2.hour -23 \n", + " return t1\n", + " \n", + "def add_time_two(t1, t2):\n", + " sum = Time()\n", + " sum.hour = t1.hour + t2.hour\n", + " sum.minute = t1.minute + t2.minute\n", + " sum.second = t1.second + t2.second\n", + "\n", + " if sum.second >= 60:\n", + " sum.second -= 60\n", + " sum.minute += 1\n", + "\n", + " if sum.minute >= 60:\n", + " sum.minute -= 60\n", + " sum.hour += 1\n", + " \n", + " if sum.hour > 24:\n", + " sum.hour = sum.hour%24\n", + "\n", + " return sum" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "05:10:10\n", + "06:01:00\n" + ] + } + ], + "source": [ + "time1 = Time()\n", + "time1.hour = 5\n", + "time1.minute = 10\n", + "time1.second = 10\n", + "\n", + "time2 = Time()\n", + "time2.hour = 24\n", + "time2.minute = 50\n", + "time2.second = 50\n", + "\n", + "a = is_after(time1, time2)\n", + "print(a)\n", + "\n", + "# print_time(time1)\n", + "# b = add_time(time1, time2)\n", + "# print_time(b)\n", + "\n", + "print_time(time1)\n", + "c=add_time_two(time1,time2)\n", + "print_time(c)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 65f959a4265c889bed9b900234efcb9b823114a9 Mon Sep 17 00:00:00 2001 From: x Date: Fri, 28 Jun 2019 09:40:46 +0200 Subject: [PATCH 08/14] chap 15 --- ...hink Python Ch15 Classes and Objects.ipynb | 181 ++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 code/Think Python Ch15 Classes and Objects.ipynb diff --git a/code/Think Python Ch15 Classes and Objects.ipynb b/code/Think Python Ch15 Classes and Objects.ipynb new file mode 100644 index 0000000..f7417df --- /dev/null +++ b/code/Think Python Ch15 Classes and Objects.ipynb @@ -0,0 +1,181 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Exercise 15.1" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "from math import sqrt\n", + "import copy\n", + "\n", + "class Point:\n", + " \"\"\"A class represents a point in 2D space\"\"\"\n", + " \n", + "class Rectangle:\n", + " \"\"\"A class represents a rectangle\n", + " Attributes: lower left corner, width, height \n", + " \"\"\" \n", + "\n", + "class Circle:\n", + " \"\"\"A class represents a circle\n", + " Attributes: center, radius\n", + " \"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "def print_point(point):\n", + " print('(%g, %g)'%(point.x, point.y))\n", + "\n", + "def distance_between_points(point1,point2):\n", + " d = sqrt((point1.x-point2.x)**2 + (point1.y - point2.y)**2)\n", + " return d\n", + "\n", + "def find_center(rectangle):\n", + " point = Point()\n", + " point.x = rectangle.corner.x + rectangle.width/2\n", + " point.y = rectangle.corner.y + rectangle.height/2\n", + " return point\n", + "\n", + "def change_rectangle(rectangle, dwidth, dheight):\n", + "# rectangle = Rectangle()\n", + " rectangle.width += dwidth\n", + " rectangle.height += dheight\n", + " \n", + "def move_rectangle(rectangle, dx, dy):\n", + " rectangle.corner.x += dx\n", + " rectangle.corner.y += dy\n", + "\n", + "def move_copied_rectangle(rectangle, dx, dy):\n", + " copied_rec = copy.deepcopy(rectangle)\n", + " move_rectangle(copied_rec,dx,dy)\n", + " return copied_rec\n", + "\n", + "def point_in_circle(circle, point):\n", + " \"\"\"To test if a point is in a circle\n", + " True if the point lies in or on the boundary of the circle or on the circle, false otherwise\n", + " \"\"\"\n", + " d = distance_between_points(circle.center, point)\n", + " return d <= circle.radius\n", + "\n", + "def my_rect_in_circle(circle, rec):\n", + " rec.upper_right_corner = Point()\n", + " rec.upper_right_corner.x = rec.corner.x + rec.width\n", + " rec.upper_right_corner.y = rec.corner.y + rec.height\n", + "\n", + " d1 = distance_between_points(circle.center, rec.corner)\n", + " d2 = distance_between_points(circle.center, rec.upper_right_corner)\n", + " \n", + " return d1 <= circle.radius and d2 <= circle.radius\n", + "\n", + "def rect_in_circle(circle,rect):\n", + " \"\"\"Checks whether the corners of a rect fall in/on a circle.\n", + "\n", + " rect: Rectangle object\n", + " circle: Circle object\n", + " \"\"\"\n", + " p = copy.copy(rect.corner)\n", + " print_point(p)\n", + " if not point_in_circle(circle,p):\n", + " return False\n", + "\n", + " p.x += rect.width\n", + " print_point(p)\n", + " if not point_in_circle(circle,p):\n", + " return False\n", + "\n", + " p.y -= rect.height\n", + " print_point(p)\n", + " if not point_in_circle(circle,p):\n", + " return False\n", + "\n", + " p.x -= rect.width\n", + " print_point(p)\n", + " if not point_in_circle(circle,p):\n", + " return False\n", + "\n", + " return True" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "True\n", + "(-1, -1)\n", + "(1, -1)\n", + "(1, -3)\n", + "(-1, -3)\n", + "True\n" + ] + } + ], + "source": [ + "circle1 = Circle()\n", + "circle1.center = Point()\n", + "circle1.center.x = 0\n", + "circle1.center.y = 0\n", + "circle1.radius = 10\n", + "\n", + "rec1 = Rectangle()\n", + "rec1.corner = Point()\n", + "rec1.corner.x = -1\n", + "rec1.corner.y = -1\n", + "rec1.width = 2\n", + "rec1.height = 2\n", + "\n", + "point1 = Point()\n", + "point1.x = 0\n", + "point1.y = 0\n", + "\n", + "a = my_rect_in_circle(circle1, rec1)\n", + "print(a)\n", + "\n", + "b = point_in_circle(circle1, point1)\n", + "print(b)\n", + "\n", + "c= rect_in_circle(circle1,rec1)\n", + "print(c)\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From b9778184f11cdc479fee4b5b3eaeaadfe6b78299 Mon Sep 17 00:00:00 2001 From: x Date: Fri, 28 Jun 2019 09:40:57 +0200 Subject: [PATCH 09/14] checkpoint --- code/.ipynb_checkpoints/wc-checkpoint.ipynb | 53 +++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 code/.ipynb_checkpoints/wc-checkpoint.ipynb diff --git a/code/.ipynb_checkpoints/wc-checkpoint.ipynb b/code/.ipynb_checkpoints/wc-checkpoint.ipynb new file mode 100644 index 0000000..9c89842 --- /dev/null +++ b/code/.ipynb_checkpoints/wc-checkpoint.ipynb @@ -0,0 +1,53 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "__main__\n", + "16\n" + ] + } + ], + "source": [ + "def line_count(filename):\n", + " \"\"\"A function counts how many lines a file has.\"\"\"\n", + " cnt = 0\n", + " for line in open(filename):\n", + " cnt += 1\n", + " return cnt\n", + "\n", + "print(__name__)\n", + "\n", + "if __name__ == '__main__':\n", + " print(line_count('wc.py'))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From fcddd36f7501e76d440a1d652af4cb9743a540d4 Mon Sep 17 00:00:00 2001 From: x Date: Fri, 28 Jun 2019 09:41:05 +0200 Subject: [PATCH 10/14] chap 17 --- .../Think Python Ch17 Class and Method-checkpoint.ipynb | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 code/.ipynb_checkpoints/Think Python Ch17 Class and Method-checkpoint.ipynb diff --git a/code/.ipynb_checkpoints/Think Python Ch17 Class and Method-checkpoint.ipynb b/code/.ipynb_checkpoints/Think Python Ch17 Class and Method-checkpoint.ipynb new file mode 100644 index 0000000..2fd6442 --- /dev/null +++ b/code/.ipynb_checkpoints/Think Python Ch17 Class and Method-checkpoint.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 2 +} From 8a54da928913adfb675dbc211371259d000c27a7 Mon Sep 17 00:00:00 2001 From: x Date: Fri, 28 Jun 2019 09:41:20 +0200 Subject: [PATCH 11/14] chap 16 --- ...Think Python Ch16 Classes and Functions-checkpoint.ipynb | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 code/.ipynb_checkpoints/Think Python Ch16 Classes and Functions-checkpoint.ipynb diff --git a/code/.ipynb_checkpoints/Think Python Ch16 Classes and Functions-checkpoint.ipynb b/code/.ipynb_checkpoints/Think Python Ch16 Classes and Functions-checkpoint.ipynb new file mode 100644 index 0000000..2fd6442 --- /dev/null +++ b/code/.ipynb_checkpoints/Think Python Ch16 Classes and Functions-checkpoint.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 2 +} From be5d3a07b1eeaeb205d9a1d4b93c89c414aeb85b Mon Sep 17 00:00:00 2001 From: x Date: Fri, 28 Jun 2019 09:41:35 +0200 Subject: [PATCH 12/14] stage all --- ... Think Python Ch12 Tuples-checkpoint.ipynb | 477 +++++++++++ ... Ch15 Classes and Objects-checkpoint.ipynb | 181 +++++ .../ch11 dictionary-checkpoint.ipynb | 156 ++++ .../ch14 files-checkpoint.ipynb | 739 ++++++++++++++++++ 4 files changed, 1553 insertions(+) create mode 100644 code/.ipynb_checkpoints/ Think Python Ch12 Tuples-checkpoint.ipynb create mode 100644 code/.ipynb_checkpoints/Think Python Ch15 Classes and Objects-checkpoint.ipynb create mode 100644 code/.ipynb_checkpoints/ch11 dictionary-checkpoint.ipynb create mode 100644 code/.ipynb_checkpoints/ch14 files-checkpoint.ipynb diff --git a/code/.ipynb_checkpoints/ Think Python Ch12 Tuples-checkpoint.ipynb b/code/.ipynb_checkpoints/ Think Python Ch12 Tuples-checkpoint.ipynb new file mode 100644 index 0000000..9dcf042 --- /dev/null +++ b/code/.ipynb_checkpoints/ Think Python Ch12 Tuples-checkpoint.ipynb @@ -0,0 +1,477 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "t = 'a'\n", + "type(t)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "t1 = 'a', #add a COMMA\n", + "type(t1)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "t2 = ('a')\n", + "type(t2)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "t3 = ('a',) #comma!!!\n", + "type(t3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### tuple assignment" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[4, 5]\n", + "[1, 2, 3]\n", + "[7, 8] [9, 0]\n" + ] + } + ], + "source": [ + "a = [1,2,3]\n", + "b = [4,5]\n", + "\n", + "a,b = b,a \n", + "# a tuple of variables = a tuple of expressions\n", + "print(a)\n", + "print(b)\n", + "\n", + "c,d = [7,8],[9,0]\n", + "print(c,d)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(1, 34)" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def extrems(t):\n", + " return min(t), max(t)\n", + "\n", + "a = (1,2,34)\n", + "b = extrems(a)\n", + "b" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Gather and Scatter" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(0, 1)" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def sum_three_num(a,b,c):\n", + " return a+b+c\n", + "\n", + "e = sum_three_num(*a)\n", + "e\n", + "\n", + "f = divmod(*b)\n", + "f" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def sum_all(*args):\n", + "# a = list(args)\n", + " return sum(args)\n", + " \n", + "g = sum_all(1,2,3)\n", + "g\n", + "\n", + "# sum(1,2,3) TypeError: 'int' object is not iterable" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### zip\n", + "---\n", + "* not indexable" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "s = 'abc'\n", + "t = [1,2,3]\n", + "z = zip(s,t)\n", + "print(z)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('a', 1)\n", + "('b', 2)\n", + "('c', 3)\n" + ] + } + ], + "source": [ + "for i in z:\n", + " print(i)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### initialize a dictionary with a list of tuple" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'lisa': 12, 'lee': 23, 'Tom': 44}" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = ['lisa', 'lee', 'Tom']\n", + "b = [12,23,44]\n", + "dict1 = dict(zip(a,b)) # no need to convert the zip object to a list object\n", + "dict1" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'lisa': 12, 'lee': 23, 'Tom': 44, 'jane': 18}" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c=['jane']\n", + "e=[18]\n", + "dict1.update(zip(c,e))\n", + "dict1" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'lisa': 12, 'lee': 23, 'Tom': 44, 'jane': 80}" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f = ['jane']\n", + "g=[80]\n", + "dict1.update(zip(f,g))\n", + "dict1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "* dictionay keys have to be immutable, therefore they can be int, str, tuple etc., but not list\n", + "* if the tuple contains mutable objects, it cannot be used as a key" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{('lisa', 'zhang'): 123, ('lee', 'schmidt'): 234, ('Tom', 'bay'): 345}" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f_name = a\n", + "l_name = ['zhang','schmidt','bay']\n", + "phones = [123,234,345]\n", + "\n", + "names = list(zip(f_name,l_name))\n", + "# names = tuple(zip(f_name,l_name)) \n", + "# both work\n", + "\n", + "# names = zip(f_name,l_name)\n", + "# TypeError: 'zip' object is not subscriptable\n", + "# It basically means that the object implements the __getitem__() method. \n", + "# In other words, it describes objects that are \"containers\", meaning they contain other objects. \n", + "# This includes lists, tuples, and dictionaries.\n", + "\n", + "# phone_book={}\n", + "# for i in range(3):\n", + "# phone_book[(f_name,l_name)[i]]=phones[i]\n", + "# TypeError: unhashable type: 'list'\n", + "# The problem is that you can't use a list as the key in a dict\n", + " \n", + "phone_book={}\n", + "for i in range(3):\n", + " phone_book[names[i]]=phones[i]\n", + "\n", + "phone_book" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Exercises\n", + "---\n", + "### Exercise 12.1. \n", + "Write a function called most_frequent that takes a string and prints the letters\n", + "in decreasing order of frequency. Find text samples from several different languages and see\n", + "how letter frequency varies between languages." + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [], + "source": [ + "def count_letters(text):\n", + " letter_hist = {}\n", + " for letter in text:\n", + " if letter != ' ':\n", + " letter_hist[letter] = letter_hist.get(letter,0) + 1\n", + " else:\n", + " pass\n", + " \n", + " return letter_hist" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'i': 1,\n", + " 'c': 2,\n", + " 'o': 2,\n", + " 'u': 1,\n", + " 'l': 3,\n", + " 'd': 1,\n", + " 's': 1,\n", + " 'e': 3,\n", + " 'a': 1,\n", + " 'r': 1,\n", + " 'y': 1,\n", + " 'n': 1,\n", + " 'w': 1}" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = 'i could see clearly now'\n", + "b = count_letters(a)\n", + "b" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/code/.ipynb_checkpoints/Think Python Ch15 Classes and Objects-checkpoint.ipynb b/code/.ipynb_checkpoints/Think Python Ch15 Classes and Objects-checkpoint.ipynb new file mode 100644 index 0000000..f7417df --- /dev/null +++ b/code/.ipynb_checkpoints/Think Python Ch15 Classes and Objects-checkpoint.ipynb @@ -0,0 +1,181 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Exercise 15.1" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "from math import sqrt\n", + "import copy\n", + "\n", + "class Point:\n", + " \"\"\"A class represents a point in 2D space\"\"\"\n", + " \n", + "class Rectangle:\n", + " \"\"\"A class represents a rectangle\n", + " Attributes: lower left corner, width, height \n", + " \"\"\" \n", + "\n", + "class Circle:\n", + " \"\"\"A class represents a circle\n", + " Attributes: center, radius\n", + " \"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "def print_point(point):\n", + " print('(%g, %g)'%(point.x, point.y))\n", + "\n", + "def distance_between_points(point1,point2):\n", + " d = sqrt((point1.x-point2.x)**2 + (point1.y - point2.y)**2)\n", + " return d\n", + "\n", + "def find_center(rectangle):\n", + " point = Point()\n", + " point.x = rectangle.corner.x + rectangle.width/2\n", + " point.y = rectangle.corner.y + rectangle.height/2\n", + " return point\n", + "\n", + "def change_rectangle(rectangle, dwidth, dheight):\n", + "# rectangle = Rectangle()\n", + " rectangle.width += dwidth\n", + " rectangle.height += dheight\n", + " \n", + "def move_rectangle(rectangle, dx, dy):\n", + " rectangle.corner.x += dx\n", + " rectangle.corner.y += dy\n", + "\n", + "def move_copied_rectangle(rectangle, dx, dy):\n", + " copied_rec = copy.deepcopy(rectangle)\n", + " move_rectangle(copied_rec,dx,dy)\n", + " return copied_rec\n", + "\n", + "def point_in_circle(circle, point):\n", + " \"\"\"To test if a point is in a circle\n", + " True if the point lies in or on the boundary of the circle or on the circle, false otherwise\n", + " \"\"\"\n", + " d = distance_between_points(circle.center, point)\n", + " return d <= circle.radius\n", + "\n", + "def my_rect_in_circle(circle, rec):\n", + " rec.upper_right_corner = Point()\n", + " rec.upper_right_corner.x = rec.corner.x + rec.width\n", + " rec.upper_right_corner.y = rec.corner.y + rec.height\n", + "\n", + " d1 = distance_between_points(circle.center, rec.corner)\n", + " d2 = distance_between_points(circle.center, rec.upper_right_corner)\n", + " \n", + " return d1 <= circle.radius and d2 <= circle.radius\n", + "\n", + "def rect_in_circle(circle,rect):\n", + " \"\"\"Checks whether the corners of a rect fall in/on a circle.\n", + "\n", + " rect: Rectangle object\n", + " circle: Circle object\n", + " \"\"\"\n", + " p = copy.copy(rect.corner)\n", + " print_point(p)\n", + " if not point_in_circle(circle,p):\n", + " return False\n", + "\n", + " p.x += rect.width\n", + " print_point(p)\n", + " if not point_in_circle(circle,p):\n", + " return False\n", + "\n", + " p.y -= rect.height\n", + " print_point(p)\n", + " if not point_in_circle(circle,p):\n", + " return False\n", + "\n", + " p.x -= rect.width\n", + " print_point(p)\n", + " if not point_in_circle(circle,p):\n", + " return False\n", + "\n", + " return True" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "True\n", + "(-1, -1)\n", + "(1, -1)\n", + "(1, -3)\n", + "(-1, -3)\n", + "True\n" + ] + } + ], + "source": [ + "circle1 = Circle()\n", + "circle1.center = Point()\n", + "circle1.center.x = 0\n", + "circle1.center.y = 0\n", + "circle1.radius = 10\n", + "\n", + "rec1 = Rectangle()\n", + "rec1.corner = Point()\n", + "rec1.corner.x = -1\n", + "rec1.corner.y = -1\n", + "rec1.width = 2\n", + "rec1.height = 2\n", + "\n", + "point1 = Point()\n", + "point1.x = 0\n", + "point1.y = 0\n", + "\n", + "a = my_rect_in_circle(circle1, rec1)\n", + "print(a)\n", + "\n", + "b = point_in_circle(circle1, point1)\n", + "print(b)\n", + "\n", + "c= rect_in_circle(circle1,rec1)\n", + "print(c)\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/code/.ipynb_checkpoints/ch11 dictionary-checkpoint.ipynb b/code/.ipynb_checkpoints/ch11 dictionary-checkpoint.ipynb new file mode 100644 index 0000000..2a96392 --- /dev/null +++ b/code/.ipynb_checkpoints/ch11 dictionary-checkpoint.ipynb @@ -0,0 +1,156 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'a': 1}\n", + "\n", + "1\n", + "\n", + "5\n", + "{'a': 1}\n" + ] + } + ], + "source": [ + "a_dic = {}\n", + "a_dic['a'] = 1\n", + "print(a_dic) \n", + "print(type(a_dic.get('a',0)))\n", + "print(a_dic.get('a',0))\n", + "print()\n", + "\n", + "print(a_dic.get('b',5))\n", + "print(a_dic)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'h': 2, 'a': 2}\n" + ] + } + ], + "source": [ + "def count_letters(string):\n", + " \"\"\"A fuction used to count letters in a string.\"\"\"\n", + " d = dict()\n", + " for char in string:\n", + " d[char] = d.get(char, 0) + 1 \n", + " return d\n", + "\n", + "# Problem with my code:\n", + "# def count_letters(string):\n", + "# \"\"\"A fuction used to count letters in a string.\"\"\"\n", + "# d = dict()\n", + "# for char in string:\n", + "# d[char] = d.get(d[char], 0) + 1 \n", + "# return d\n", + "# function get() takes a KEY and a default value, but I passed a value (d[char]) instead a key (char) to it\n", + "a = count_letters('haha')\n", + "print(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "defaultdict(, {'h': 2, 'a': 2})\n" + ] + } + ], + "source": [ + "from collections import defaultdict\n", + "\n", + "def count_letters_two(string):\n", + " d = defaultdict(int) # default value of int is 0\n", + " for char in string:\n", + " d[char] += 1\n", + " return d\n", + "\n", + "b = count_letters_two('haha')\n", + "print(b)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['spam', 'spam', 'spam', 'spam', 'spam', 'spam', 'eggs', 'spam']\n" + ] + } + ], + "source": [ + "food_list = 'spam spam spam spam spam spam eggs spam'.split()\n", + "print(food_list)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "defaultdict(, {'TX': ['Austin', 'Houston', 'Dallas'], 'NY': ['Albany', 'Syracuse', 'Buffalo', 'Rochester'], 'CA': ['Sacramento', 'Palo Alto'], 'GA': ['Atlanta']})\n" + ] + } + ], + "source": [ + "city_list = [('TX','Austin'), ('TX','Houston'), ('NY','Albany'), ('NY', 'Syracuse'), ('NY', 'Buffalo'), \n", + " ('NY', 'Rochester'), ('TX', 'Dallas'), ('CA','Sacramento'), ('CA', 'Palo Alto'), ('GA', 'Atlanta')]\n", + "\n", + "state_city_dict = defaultdict(list)\n", + "for state, city in city_list:\n", + " state_city_dict[state].append(city)\n", + " \n", + "print(state_city_dict)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/code/.ipynb_checkpoints/ch14 files-checkpoint.ipynb b/code/.ipynb_checkpoints/ch14 files-checkpoint.ipynb new file mode 100644 index 0000000..dc6713b --- /dev/null +++ b/code/.ipynb_checkpoints/ch14 files-checkpoint.ipynb @@ -0,0 +1,739 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import os" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'C:\\\\Users\\\\zheng\\\\Documents\\\\python\\\\ThinkPython2\\\\code'" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "os.getcwd()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:1: DeprecationWarning: The Windows bytes API has been deprecated, use Unicode filenames instead\n", + " \"\"\"Entry point for launching an IPython kernel.\n" + ] + }, + { + "data": { + "text/plain": [ + "b'C:\\\\Users\\\\zheng\\\\Documents\\\\python\\\\ThinkPython2\\\\code'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "os.getcwdb()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'C:\\\\Users\\\\zheng\\\\Documents\\\\python\\\\ThinkPython2\\\\code\\\\ch11 dictionary.ipynb'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "os.path.abspath('ch11 dictionary.ipynb')" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "os.path.exists('ch11 dictionary.ipynb')" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "os.path.exists('.ipynb_checkpoints')" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "os.path.isdir('.ipynb_checkpoints')" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['.ipynb_checkpoints',\n", + " '158-0.txt',\n", + " 'ackermann.py',\n", + " 'ackermann_memo.py',\n", + " 'anagram_db.py',\n", + " 'anagram_sets.py',\n", + " 'analyze_book1.py',\n", + " 'analyze_book2.py',\n", + " 'analyze_book3.py',\n", + " 'BadKangaroo.py',\n", + " 'birthday.py',\n", + " 'c06d',\n", + " 'Card.py',\n", + " 'Card_test.py',\n", + " 'cartalk1.py',\n", + " 'cartalk2.py',\n", + " 'cartalk3.py',\n", + " 'ch11 dictionary.ipynb',\n", + " 'ch14 files.ipynb',\n", + " 'Circle.py',\n", + " 'double.py',\n", + " 'do_four.py',\n", + " 'draw.py',\n", + " 'find_duplicates.py',\n", + " 'find_duplicates_copy.py',\n", + " 'flower.py',\n", + " 'GoodKangaroo.py',\n", + " 'grid.py',\n", + " 'has_duplicates.py',\n", + " 'homophone.py',\n", + " 'inlist.py',\n", + " 'interlock.py',\n", + " 'invert_dict.py',\n", + " 'koch.py',\n", + " 'letters.py',\n", + " 'list_exercises.py',\n", + " 'Map.py',\n", + " 'markov.py',\n", + " 'markov2.py',\n", + " 'metathesis.py',\n", + " 'most_frequent.py',\n", + " 'pace_calc.py',\n", + " 'palindrome_soln.py',\n", + " 'pi.py',\n", + " 'pie.py',\n", + " 'Point1.py',\n", + " 'Point1_soln.py',\n", + " 'Point2_soln.py',\n", + " 'PokerHand.py',\n", + " 'PokerHandSoln.py',\n", + " 'polygon.py',\n", + " 'pronounce.py',\n", + " 'reducible.py',\n", + " 'reverse_pair.py',\n", + " 'rotate.py',\n", + " 'rotate_pairs.py',\n", + " 'run_all_solutions.py',\n", + " 'sed.py',\n", + " 'see NY.text',\n", + " 'spiral.py',\n", + " 'structshape.py',\n", + " 'Time1.py',\n", + " 'Time1_soln.py',\n", + " 'Time2.py',\n", + " 'Time2_soln.py',\n", + " 'typewriter.py',\n", + " 'unstable_sort.py',\n", + " 'walk.py',\n", + " 'wordlist.py',\n", + " 'words.txt',\n", + " 'zipf.py']" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "os.listdir()" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "def walk(dirname):\n", + " for name in os.listdir(dirname):\n", + " a = os.path.join(dirname,name)\n", + " # pay attention to the indentation needed!\n", + " if os.path.isdir(a):\n", + " walk(a)\n", + " else:\n", + " print(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\config\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\description\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\FETCH_HEAD\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\HEAD\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\hooks\\README.sample\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\index\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\info\\exclude\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\logs\\HEAD\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\logs\\refs\\heads\\master\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\logs\\refs\\remotes\\origin\\add-license-1\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\logs\\refs\\remotes\\origin\\master\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\objects\\09\\3610598ce6e1b537a1de8871366c7682605359\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\objects\\60\\dd5aaa4b24ab35ff9b0a29255e62d7a9b835d9\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\objects\\71\\9c9231144e0d06d16d75b9ba202687938f1e0e\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\objects\\98\\86944b9fc2c7045578d333770ac35a54f38f70\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\objects\\pack\\pack-07d17babc0231a6043a91c9070f5532032cd2d36.idx\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\objects\\pack\\pack-07d17babc0231a6043a91c9070f5532032cd2d36.pack\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\refs\\heads\\master\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\refs\\remotes\\origin\\add-license-1\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.git\\refs\\remotes\\origin\\master\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\.gitignore\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\back.png\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\book.tex\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\assign2.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\assign2.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\assign2.fig.bak\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\assign2.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\banana.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\banana.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\banana.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\card1.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\card1.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\card1.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\class1.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\class1.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\class1.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\compile.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\compile.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\compile.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\dict1.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\dict1.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\dict1.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\dict2.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\dict2.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\dict2.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\fibonacci.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\fibonacci.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\fibonacci.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\flower.test.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\flowers.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\flowers.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\interpret.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\interpret.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\interpret.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\koch.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\koch.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\list1.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\list1.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\list1.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\list2.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\list2.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\list2.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\list3.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\list3.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\list3.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\liststate.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\liststate.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\liststate.fig.bak\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\liststate.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\listsum1.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\listsum1.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\listsum2.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\listsum2.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\loop.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\loop.py~\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\pies.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\pies.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\point.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\point.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\point.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\rectangle.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\rectangle.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\rectangle.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\rectangle2.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\rectangle2.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\rectangle2.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack2.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack2.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack2.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack3.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack3.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack3.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack4.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack4.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack4.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack5.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack5.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\stack5.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state2.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state2.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state2.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state3.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state3.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state3.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state4.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state4.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state4.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state5.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state5.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\state5.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\time.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\time.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\time.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\towers.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\towers.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\towers.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\tuple1.eps\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\tuple1.fig\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\figs\\tuple1.pdf\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\footer.html\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\header.html\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\hevea.sty\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\htmlonly\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\latexonly\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\localdef.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\Makefile\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\next.png\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\book\\up.png\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\.ipynb_checkpoints\\ch11 dictionary-checkpoint.ipynb\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\.ipynb_checkpoints\\ch14 files-checkpoint.ipynb\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\158-0.txt\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\ackermann.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\ackermann_memo.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\anagram_db.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\anagram_sets.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\analyze_book1.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\analyze_book2.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\analyze_book3.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\BadKangaroo.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\birthday.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\c06d\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\Card.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\Card_test.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\cartalk1.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\cartalk2.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\cartalk3.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\ch11 dictionary.ipynb\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\ch14 files.ipynb\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\Circle.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\double.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\do_four.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\draw.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\find_duplicates.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\find_duplicates_copy.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\flower.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\GoodKangaroo.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\grid.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\has_duplicates.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\homophone.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\inlist.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\interlock.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\invert_dict.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\koch.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\letters.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\list_exercises.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\Map.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\markov.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\markov2.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\metathesis.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\most_frequent.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\pace_calc.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\palindrome_soln.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\pi.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\pie.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\Point1.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\Point1_soln.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\Point2_soln.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\PokerHand.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\PokerHandSoln.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\polygon.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\pronounce.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\reducible.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\reverse_pair.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\rotate.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\rotate_pairs.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\run_all_solutions.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\sed.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\see NY.text\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\spiral.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\structshape.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\Time1.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\Time1_soln.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\Time2.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\Time2_soln.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\typewriter.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\unstable_sort.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\walk.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\wordlist.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\words.txt\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\code\\zipf.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\LICENSE\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\python_thinking\\Frame.py\n", + "C:\\Users\\zheng\\Documents\\python\\ThinkPython2\\README.md\n" + ] + } + ], + "source": [ + "b = walk('C:\\\\Users\\\\zheng\\\\Documents\\\\python\\\\ThinkPython2')" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['158-0.txt', 'ackermann.py', 'ackermann_memo.py', 'anagram_db.py', 'anagram_sets.py', 'analyze_book1.py', 'analyze_book2.py', 'analyze_book3.py', 'BadKangaroo.py', 'birthday.py', 'c06d', 'Card.py', 'Card_test.py', 'cartalk1.py', 'cartalk2.py', 'cartalk3.py', 'ch11 dictionary.ipynb', 'ch14 files.ipynb', 'Circle.py', 'double.py', 'do_four.py', 'draw.py', 'find_duplicates.py', 'find_duplicates_copy.py', 'flower.py', 'GoodKangaroo.py', 'grid.py', 'has_duplicates.py', 'homophone.py', 'inlist.py', 'interlock.py', 'invert_dict.py', 'koch.py', 'letters.py', 'list_exercises.py', 'Map.py', 'markov.py', 'markov2.py', 'metathesis.py', 'most_frequent.py', 'pace_calc.py', 'palindrome_soln.py', 'pi.py', 'pie.py', 'Point1.py', 'Point1_soln.py', 'Point2_soln.py', 'PokerHand.py', 'PokerHandSoln.py', 'polygon.py', 'pronounce.py', 'reducible.py', 'reverse_pair.py', 'rotate.py', 'rotate_pairs.py', 'run_all_solutions.py', 'sed.py', 'see NY.text', 'spiral.py', 'structshape.py', 'Time1.py', 'Time1_soln.py', 'Time2.py', 'Time2_soln.py', 'typewriter.py', 'unstable_sort.py', 'walk.py', 'wordlist.py', 'words.txt', 'zipf.py']\n", + "['ch11 dictionary-checkpoint.ipynb', 'ch14 files-checkpoint.ipynb']\n" + ] + } + ], + "source": [ + "for a,b,c in os.walk(os.getcwd(),topdown=True):\n", + "# print(a)\n", + "# print(b)\n", + " print(c)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [], + "source": [ + "import dbm" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [], + "source": [ + "db = dbm.open('captions', 'c')" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [], + "source": [ + "db['bikinipants.png'] = 'A photo of bikinipants'" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "b'A photo of bikinipants'" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "db['bikinipants.png']" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [], + "source": [ + "db['bikinipants.png'] = 'bikinipants swimming'" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "b'bikinipants swimming'" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "db['bikinipants.png']" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "b'bikinipants.png' b'bikinipants swimming'\n" + ] + } + ], + "source": [ + "for key in db:\n", + " print(key, db[key])" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [], + "source": [ + "db.close() \n", + "# three files created, .DAT .DIR & .BAK\n", + "#.bak a backup copy of a file" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [], + "source": [ + "import pickle" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "b'\\x80\\x03]q\\x00(K\\x01K\\x02K\\x03e.'\n" + ] + } + ], + "source": [ + "l1 = [1,2,3]\n", + "s1 = pickle.dumps(l1)\n", + "print(s1)" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3]\n" + ] + } + ], + "source": [ + "l2 = pickle.loads(s1)\n", + "print(l2)" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l1 == l2" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l2 is l1" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "import wc" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "156\n", + "wc\n" + ] + } + ], + "source": [ + "print(wc.line_count('C:\\\\Users\\\\zheng\\\\Documents\\\\python\\\\ThinkPython2\\\\code\\\\ch11 dictionary.ipynb'))\n", + "print(wc.__name__)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 1a757236bd985f59f6049a880ae9193fc5b2a5e0 Mon Sep 17 00:00:00 2001 From: x Date: Fri, 28 Jun 2019 09:42:55 +0200 Subject: [PATCH 13/14] chap 12 --- code/ Think Python Ch12 Tuples.ipynb | 477 +++++++++++++++++++++++++++ 1 file changed, 477 insertions(+) create mode 100644 code/ Think Python Ch12 Tuples.ipynb diff --git a/code/ Think Python Ch12 Tuples.ipynb b/code/ Think Python Ch12 Tuples.ipynb new file mode 100644 index 0000000..9dcf042 --- /dev/null +++ b/code/ Think Python Ch12 Tuples.ipynb @@ -0,0 +1,477 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "t = 'a'\n", + "type(t)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "t1 = 'a', #add a COMMA\n", + "type(t1)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "t2 = ('a')\n", + "type(t2)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "t3 = ('a',) #comma!!!\n", + "type(t3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### tuple assignment" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[4, 5]\n", + "[1, 2, 3]\n", + "[7, 8] [9, 0]\n" + ] + } + ], + "source": [ + "a = [1,2,3]\n", + "b = [4,5]\n", + "\n", + "a,b = b,a \n", + "# a tuple of variables = a tuple of expressions\n", + "print(a)\n", + "print(b)\n", + "\n", + "c,d = [7,8],[9,0]\n", + "print(c,d)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(1, 34)" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def extrems(t):\n", + " return min(t), max(t)\n", + "\n", + "a = (1,2,34)\n", + "b = extrems(a)\n", + "b" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Gather and Scatter" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(0, 1)" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def sum_three_num(a,b,c):\n", + " return a+b+c\n", + "\n", + "e = sum_three_num(*a)\n", + "e\n", + "\n", + "f = divmod(*b)\n", + "f" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def sum_all(*args):\n", + "# a = list(args)\n", + " return sum(args)\n", + " \n", + "g = sum_all(1,2,3)\n", + "g\n", + "\n", + "# sum(1,2,3) TypeError: 'int' object is not iterable" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### zip\n", + "---\n", + "* not indexable" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "s = 'abc'\n", + "t = [1,2,3]\n", + "z = zip(s,t)\n", + "print(z)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('a', 1)\n", + "('b', 2)\n", + "('c', 3)\n" + ] + } + ], + "source": [ + "for i in z:\n", + " print(i)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### initialize a dictionary with a list of tuple" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'lisa': 12, 'lee': 23, 'Tom': 44}" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = ['lisa', 'lee', 'Tom']\n", + "b = [12,23,44]\n", + "dict1 = dict(zip(a,b)) # no need to convert the zip object to a list object\n", + "dict1" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'lisa': 12, 'lee': 23, 'Tom': 44, 'jane': 18}" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c=['jane']\n", + "e=[18]\n", + "dict1.update(zip(c,e))\n", + "dict1" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'lisa': 12, 'lee': 23, 'Tom': 44, 'jane': 80}" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f = ['jane']\n", + "g=[80]\n", + "dict1.update(zip(f,g))\n", + "dict1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "* dictionay keys have to be immutable, therefore they can be int, str, tuple etc., but not list\n", + "* if the tuple contains mutable objects, it cannot be used as a key" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{('lisa', 'zhang'): 123, ('lee', 'schmidt'): 234, ('Tom', 'bay'): 345}" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f_name = a\n", + "l_name = ['zhang','schmidt','bay']\n", + "phones = [123,234,345]\n", + "\n", + "names = list(zip(f_name,l_name))\n", + "# names = tuple(zip(f_name,l_name)) \n", + "# both work\n", + "\n", + "# names = zip(f_name,l_name)\n", + "# TypeError: 'zip' object is not subscriptable\n", + "# It basically means that the object implements the __getitem__() method. \n", + "# In other words, it describes objects that are \"containers\", meaning they contain other objects. \n", + "# This includes lists, tuples, and dictionaries.\n", + "\n", + "# phone_book={}\n", + "# for i in range(3):\n", + "# phone_book[(f_name,l_name)[i]]=phones[i]\n", + "# TypeError: unhashable type: 'list'\n", + "# The problem is that you can't use a list as the key in a dict\n", + " \n", + "phone_book={}\n", + "for i in range(3):\n", + " phone_book[names[i]]=phones[i]\n", + "\n", + "phone_book" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Exercises\n", + "---\n", + "### Exercise 12.1. \n", + "Write a function called most_frequent that takes a string and prints the letters\n", + "in decreasing order of frequency. Find text samples from several different languages and see\n", + "how letter frequency varies between languages." + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [], + "source": [ + "def count_letters(text):\n", + " letter_hist = {}\n", + " for letter in text:\n", + " if letter != ' ':\n", + " letter_hist[letter] = letter_hist.get(letter,0) + 1\n", + " else:\n", + " pass\n", + " \n", + " return letter_hist" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'i': 1,\n", + " 'c': 2,\n", + " 'o': 2,\n", + " 'u': 1,\n", + " 'l': 3,\n", + " 'd': 1,\n", + " 's': 1,\n", + " 'e': 3,\n", + " 'a': 1,\n", + " 'r': 1,\n", + " 'y': 1,\n", + " 'n': 1,\n", + " 'w': 1}" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = 'i could see clearly now'\n", + "b = count_letters(a)\n", + "b" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 71d5fcdcd2919675a90470564c75e2482bcad0b6 Mon Sep 17 00:00:00 2001 From: x Date: Fri, 28 Jun 2019 09:48:31 +0200 Subject: [PATCH 14/14] rename --- ...kpoint.ipynb => Think Python Ch11 dictionary-checkpoint.ipynb} | 0 .../Think Python Ch12 Tuples-checkpoint.ipynb} | 0 ...-checkpoint.ipynb => Think Python Ch14 files-checkpoint.ipynb} | 0 .../{ch11 dictionary.ipynb => Think Python Ch11 dictionary.ipynb} | 0 ...h12 Tuples-checkpoint.ipynb => Think Python Ch12 Tuples.ipynb} | 0 code/{ch14 files.ipynb => Think Python Ch14 files.ipynb} | 0 6 files changed, 0 insertions(+), 0 deletions(-) rename code/.ipynb_checkpoints/{ch11 dictionary-checkpoint.ipynb => Think Python Ch11 dictionary-checkpoint.ipynb} (100%) rename code/{ Think Python Ch12 Tuples.ipynb => .ipynb_checkpoints/Think Python Ch12 Tuples-checkpoint.ipynb} (100%) rename code/.ipynb_checkpoints/{ch14 files-checkpoint.ipynb => Think Python Ch14 files-checkpoint.ipynb} (100%) rename code/{ch11 dictionary.ipynb => Think Python Ch11 dictionary.ipynb} (100%) rename code/{.ipynb_checkpoints/ Think Python Ch12 Tuples-checkpoint.ipynb => Think Python Ch12 Tuples.ipynb} (100%) rename code/{ch14 files.ipynb => Think Python Ch14 files.ipynb} (100%) diff --git a/code/.ipynb_checkpoints/ch11 dictionary-checkpoint.ipynb b/code/.ipynb_checkpoints/Think Python Ch11 dictionary-checkpoint.ipynb similarity index 100% rename from code/.ipynb_checkpoints/ch11 dictionary-checkpoint.ipynb rename to code/.ipynb_checkpoints/Think Python Ch11 dictionary-checkpoint.ipynb diff --git a/code/ Think Python Ch12 Tuples.ipynb b/code/.ipynb_checkpoints/Think Python Ch12 Tuples-checkpoint.ipynb similarity index 100% rename from code/ Think Python Ch12 Tuples.ipynb rename to code/.ipynb_checkpoints/Think Python Ch12 Tuples-checkpoint.ipynb diff --git a/code/.ipynb_checkpoints/ch14 files-checkpoint.ipynb b/code/.ipynb_checkpoints/Think Python Ch14 files-checkpoint.ipynb similarity index 100% rename from code/.ipynb_checkpoints/ch14 files-checkpoint.ipynb rename to code/.ipynb_checkpoints/Think Python Ch14 files-checkpoint.ipynb diff --git a/code/ch11 dictionary.ipynb b/code/Think Python Ch11 dictionary.ipynb similarity index 100% rename from code/ch11 dictionary.ipynb rename to code/Think Python Ch11 dictionary.ipynb diff --git a/code/.ipynb_checkpoints/ Think Python Ch12 Tuples-checkpoint.ipynb b/code/Think Python Ch12 Tuples.ipynb similarity index 100% rename from code/.ipynb_checkpoints/ Think Python Ch12 Tuples-checkpoint.ipynb rename to code/Think Python Ch12 Tuples.ipynb diff --git a/code/ch14 files.ipynb b/code/Think Python Ch14 files.ipynb similarity index 100% rename from code/ch14 files.ipynb rename to code/Think Python Ch14 files.ipynb