From 66d7e45d8124a59aa47e199431e2c598a7e60450 Mon Sep 17 00:00:00 2001 From: victorgau Date: Sat, 11 Mar 2017 01:33:19 +0800 Subject: [PATCH] update .ipynb --- ...350\250\210\346\225\231\345\255\270.ipynb" | 389 +++++++++++++++++- 1 file changed, 379 insertions(+), 10 deletions(-) diff --git "a/Python \347\250\213\345\274\217\350\250\255\350\250\210\346\225\231\345\255\270.ipynb" "b/Python \347\250\213\345\274\217\350\250\255\350\250\210\346\225\231\345\255\270.ipynb" index bb3d795..0a2d11f 100644 --- "a/Python \347\250\213\345\274\217\350\250\255\350\250\210\346\225\231\345\255\270.ipynb" +++ "b/Python \347\250\213\345\274\217\350\250\255\350\250\210\346\225\231\345\255\270.ipynb" @@ -821,6 +821,98 @@ "* from... import..." ] }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false, + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['',\n", + " 'C:\\\\Program Files\\\\Anaconda3\\\\python35.zip',\n", + " 'C:\\\\Program Files\\\\Anaconda3\\\\DLLs',\n", + " 'C:\\\\Program Files\\\\Anaconda3\\\\lib',\n", + " 'C:\\\\Program Files\\\\Anaconda3',\n", + " 'C:\\\\Program Files\\\\Anaconda3\\\\lib\\\\site-packages',\n", + " 'C:\\\\Program Files\\\\Anaconda3\\\\lib\\\\site-packages\\\\Sphinx-1.4.6-py3.5.egg',\n", + " 'C:\\\\Program Files\\\\Anaconda3\\\\lib\\\\site-packages\\\\win32',\n", + " 'C:\\\\Program Files\\\\Anaconda3\\\\lib\\\\site-packages\\\\win32\\\\lib',\n", + " 'C:\\\\Program Files\\\\Anaconda3\\\\lib\\\\site-packages\\\\Pythonwin',\n", + " 'C:\\\\Program Files\\\\Anaconda3\\\\lib\\\\site-packages\\\\setuptools-27.2.0-py3.5.egg',\n", + " 'C:\\\\Program Files\\\\Anaconda3\\\\lib\\\\site-packages\\\\IPython\\\\extensions',\n", + " 'C:\\\\Users\\\\victor\\\\.ipython']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Python 的模組搜尋路徑\n", + "\n", + "import sys\n", + "\n", + "sys.path" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [], + "source": [ + "# 如果要使用的模組所在的目錄,不在搜尋的路徑上,可以使用下面方式加入\n", + "\n", + "# 1.\n", + "# sys.path.append(r\"c:\\path\\to\\the\\module\")\n", + "\n", + "# 2.\n", + "# 也可以設定 PYTHONPATH 參數,讓 Python 去搜尋特定路徑\n", + "\n", + "# 3.\n", + "# 設定一個 .pth 檔,將 .pth 檔放在 package 的存放路徑上,.pth 檔裡面填入模組的路徑" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['C:\\\\Program Files\\\\Anaconda3',\n", + " 'C:\\\\Program Files\\\\Anaconda3\\\\lib\\\\site-packages']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import site\n", + "\n", + "site.getsitepackages()" + ] + }, { "cell_type": "markdown", "metadata": { @@ -982,6 +1074,171 @@ "type(b)" ] }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false, + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['In',\n", + " 'Out',\n", + " '_',\n", + " '_1',\n", + " '_2',\n", + " '__',\n", + " '___',\n", + " '__builtin__',\n", + " '__builtins__',\n", + " '__doc__',\n", + " '__loader__',\n", + " '__name__',\n", + " '__package__',\n", + " '__spec__',\n", + " '_dh',\n", + " '_i',\n", + " '_i1',\n", + " '_i2',\n", + " '_i3',\n", + " '_ih',\n", + " '_ii',\n", + " '_iii',\n", + " '_oh',\n", + " '_sh',\n", + " 'exit',\n", + " 'get_ipython',\n", + " 'quit',\n", + " 'site',\n", + " 'sys']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 使用 dir() 列出 local scope 裡面有那些變數或模組等\n", + "dir()\n", + "\n", + "# 關於 magic methods\n", + "# http://minhhh.github.io/posts/a-guide-to-pythons-magic-methods" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false, + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['__displayhook__',\n", + " '__doc__',\n", + " '__excepthook__',\n", + " '__interactivehook__',\n", + " '__loader__',\n", + " '__name__',\n", + " '__package__',\n", + " '__spec__',\n", + " '__stderr__',\n", + " '__stdin__',\n", + " '__stdout__',\n", + " '_clear_type_cache',\n", + " '_current_frames',\n", + " '_debugmallocstats',\n", + " '_getframe',\n", + " '_home',\n", + " '_mercurial',\n", + " '_xoptions',\n", + " 'api_version',\n", + " 'argv',\n", + " 'base_exec_prefix',\n", + " 'base_prefix',\n", + " 'builtin_module_names',\n", + " 'byteorder',\n", + " 'call_tracing',\n", + " 'callstats',\n", + " 'copyright',\n", + " 'displayhook',\n", + " 'dllhandle',\n", + " 'dont_write_bytecode',\n", + " 'exc_info',\n", + " 'excepthook',\n", + " 'exec_prefix',\n", + " 'executable',\n", + " 'exit',\n", + " 'flags',\n", + " 'float_info',\n", + " 'float_repr_style',\n", + " 'get_coroutine_wrapper',\n", + " 'getallocatedblocks',\n", + " 'getcheckinterval',\n", + " 'getdefaultencoding',\n", + " 'getfilesystemencoding',\n", + " 'getprofile',\n", + " 'getrecursionlimit',\n", + " 'getrefcount',\n", + " 'getsizeof',\n", + " 'getswitchinterval',\n", + " 'gettrace',\n", + " 'getwindowsversion',\n", + " 'hash_info',\n", + " 'hexversion',\n", + " 'implementation',\n", + " 'int_info',\n", + " 'intern',\n", + " 'is_finalizing',\n", + " 'maxsize',\n", + " 'maxunicode',\n", + " 'meta_path',\n", + " 'modules',\n", + " 'path',\n", + " 'path_hooks',\n", + " 'path_importer_cache',\n", + " 'platform',\n", + " 'prefix',\n", + " 'ps1',\n", + " 'ps2',\n", + " 'ps3',\n", + " 'set_coroutine_wrapper',\n", + " 'setcheckinterval',\n", + " 'setprofile',\n", + " 'setrecursionlimit',\n", + " 'setswitchinterval',\n", + " 'settrace',\n", + " 'stderr',\n", + " 'stdin',\n", + " 'stdout',\n", + " 'thread_info',\n", + " 'version',\n", + " 'version_info',\n", + " 'warnoptions',\n", + " 'winver']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 使用 dir(module) 列出模組可以使用的屬性及方法等\n", + "\n", + "dir(sys)" + ] + }, { "cell_type": "code", "execution_count": null, @@ -1319,14 +1576,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# 字串 str\n", "\n", @@ -1337,14 +1605,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'Victor001'" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "ID = '001'\n", "\n", @@ -1355,14 +1634,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['It', 'is', 'raining', 'cats', 'and', 'dogs']\n" + ] + } + ], "source": [ "## 字串分割\n", "sentence = 'It is raining cats and dogs'\n", @@ -1372,14 +1659,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, - "outputs": [], + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'A' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[1;31m## 傳統作法 I\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Name='\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mName\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;34m': A='\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mA\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;34m'; B='\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mB\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[1;31m## 傳統作法 II\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'A' is not defined" + ] + } + ], "source": [ "# 格式化輸出\n", "\n", @@ -1874,13 +2173,83 @@ "\n", "## 宣告 dict\n", "dict1 = {}\n", - "type(dict1)\n", - "\n", + "type(dict1)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__delitem__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__setattr__',\n", + " '__setitem__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'clear',\n", + " 'copy',\n", + " 'fromkeys',\n", + " 'get',\n", + " 'items',\n", + " 'keys',\n", + " 'pop',\n", + " 'popitem',\n", + " 'setdefault',\n", + " 'update',\n", + " 'values']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir(dict)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ "dict1 = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}\n", "dict1\n", "\n", "dict1.keys()\n", "dict1.values()\n", + "dict1.items()\n", "\n", "dict1['Alice']" ]