|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466 |
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# 介绍IPython和Jupyter笔记本"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {
- "podoc": {
- "output_text": "Screenshot of a Jupyter notebook"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Hello world!\n"
- ]
- }
- ],
- "source": [
- "print(\"Hello world!\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "4"
- ]
- },
- "execution_count": 4,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "2 + 2"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "12"
- ]
- },
- "execution_count": 5,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "_ * 3"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1-numpy_tutorial_EN.ipynb\texample.png\r\n",
- "1-numpy_tutorial.ipynb\t\tmatplotlib_ani.ipynb\r\n",
- "2-matplotlib_tutorial_EN.ipynb\tmatplotlib_full.ipynb\r\n",
- "2-matplotlib_tutorial.ipynb\trandom-matrix.csv\r\n",
- "3-ipython_notebook_EN.ipynb\trandom-matrix.npy\r\n",
- "3-ipython_notebook.ipynb\tREADME.md\r\n",
- "4-scipy_tutorial_EN.ipynb\tstockholm_td_adj.dat\r\n",
- "4-scipy_tutorial.ipynb\t\tutils_git_advanced.ipynb\r\n",
- "5-sympy_tutorial_EN.ipynb\tutils_git.ipynb\r\n",
- "5-sympy_tutorial.ipynb\t\tutils_shell.ipynb\r\n",
- "bokeh_tutorial.ipynb\r\n"
- ]
- }
- ],
- "source": [
- "!ls"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "application/json": {
- "cell": {
- "!": "OSMagics",
- "HTML": "Other",
- "SVG": "Other",
- "bash": "Other",
- "capture": "ExecutionMagics",
- "debug": "ExecutionMagics",
- "file": "Other",
- "html": "DisplayMagics",
- "javascript": "DisplayMagics",
- "js": "DisplayMagics",
- "latex": "DisplayMagics",
- "markdown": "DisplayMagics",
- "perl": "Other",
- "prun": "ExecutionMagics",
- "pypy": "Other",
- "python": "Other",
- "python2": "Other",
- "python3": "Other",
- "ruby": "Other",
- "script": "ScriptMagics",
- "sh": "Other",
- "svg": "DisplayMagics",
- "sx": "OSMagics",
- "system": "OSMagics",
- "time": "ExecutionMagics",
- "timeit": "ExecutionMagics",
- "writefile": "OSMagics"
- },
- "line": {
- "alias": "OSMagics",
- "alias_magic": "BasicMagics",
- "autoawait": "AsyncMagics",
- "autocall": "AutoMagics",
- "automagic": "AutoMagics",
- "autosave": "KernelMagics",
- "bookmark": "OSMagics",
- "cat": "Other",
- "cd": "OSMagics",
- "clear": "KernelMagics",
- "colors": "BasicMagics",
- "conda": "PackagingMagics",
- "config": "ConfigMagics",
- "connect_info": "KernelMagics",
- "cp": "Other",
- "debug": "ExecutionMagics",
- "dhist": "OSMagics",
- "dirs": "OSMagics",
- "doctest_mode": "BasicMagics",
- "ed": "Other",
- "edit": "KernelMagics",
- "env": "OSMagics",
- "gui": "BasicMagics",
- "hist": "Other",
- "history": "HistoryMagics",
- "killbgscripts": "ScriptMagics",
- "ldir": "Other",
- "less": "KernelMagics",
- "lf": "Other",
- "lk": "Other",
- "ll": "Other",
- "load": "CodeMagics",
- "load_ext": "ExtensionMagics",
- "loadpy": "CodeMagics",
- "logoff": "LoggingMagics",
- "logon": "LoggingMagics",
- "logstart": "LoggingMagics",
- "logstate": "LoggingMagics",
- "logstop": "LoggingMagics",
- "ls": "Other",
- "lsmagic": "BasicMagics",
- "lx": "Other",
- "macro": "ExecutionMagics",
- "magic": "BasicMagics",
- "man": "KernelMagics",
- "matplotlib": "PylabMagics",
- "mkdir": "Other",
- "more": "KernelMagics",
- "mv": "Other",
- "notebook": "BasicMagics",
- "page": "BasicMagics",
- "pastebin": "CodeMagics",
- "pdb": "ExecutionMagics",
- "pdef": "NamespaceMagics",
- "pdoc": "NamespaceMagics",
- "pfile": "NamespaceMagics",
- "pinfo": "NamespaceMagics",
- "pinfo2": "NamespaceMagics",
- "pip": "PackagingMagics",
- "popd": "OSMagics",
- "pprint": "BasicMagics",
- "precision": "BasicMagics",
- "prun": "ExecutionMagics",
- "psearch": "NamespaceMagics",
- "psource": "NamespaceMagics",
- "pushd": "OSMagics",
- "pwd": "OSMagics",
- "pycat": "OSMagics",
- "pylab": "PylabMagics",
- "qtconsole": "KernelMagics",
- "quickref": "BasicMagics",
- "recall": "HistoryMagics",
- "rehashx": "OSMagics",
- "reload_ext": "ExtensionMagics",
- "rep": "Other",
- "rerun": "HistoryMagics",
- "reset": "NamespaceMagics",
- "reset_selective": "NamespaceMagics",
- "rm": "Other",
- "rmdir": "Other",
- "run": "ExecutionMagics",
- "save": "CodeMagics",
- "sc": "OSMagics",
- "set_env": "OSMagics",
- "store": "StoreMagics",
- "sx": "OSMagics",
- "system": "OSMagics",
- "tb": "ExecutionMagics",
- "time": "ExecutionMagics",
- "timeit": "ExecutionMagics",
- "unalias": "OSMagics",
- "unload_ext": "ExtensionMagics",
- "who": "NamespaceMagics",
- "who_ls": "NamespaceMagics",
- "whos": "NamespaceMagics",
- "xdel": "NamespaceMagics",
- "xmode": "BasicMagics"
- }
- },
- "text/plain": [
- "Available line magics:\n",
- "%alias %alias_magic %autoawait %autocall %automagic %autosave %bookmark %cat %cd %clear %colors %conda %config %connect_info %cp %debug %dhist %dirs %doctest_mode %ed %edit %env %gui %hist %history %killbgscripts %ldir %less %lf %lk %ll %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %lx %macro %magic %man %matplotlib %mkdir %more %mv %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %pip %popd %pprint %precision %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %rep %rerun %reset %reset_selective %rm %rmdir %run %save %sc %set_env %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode\n",
- "\n",
- "Available cell magics:\n",
- "%%! %%HTML %%SVG %%bash %%capture %%debug %%file %%html %%javascript %%js %%latex %%markdown %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile\n",
- "\n",
- "Automagic is ON, % prefix IS NOT needed for line magics."
- ]
- },
- "execution_count": 7,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "%lsmagic"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Writing test.txt\n"
- ]
- }
- ],
- "source": [
- "%%writefile test.txt\n",
- "Hello world!"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Hello world!\n",
- "\n"
- ]
- }
- ],
- "source": [
- "# Let's check what this file contains.\n",
- "with open('test.txt', 'r') as f:\n",
- " print(f.read())"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {
- "podoc": {
- "output_text": "Screenshot of the pager"
- }
- },
- "outputs": [],
- "source": [
- "%run?"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "outputs": [],
- "source": [
- "from IPython.display import HTML, SVG, YouTubeVideo"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {
- "podoc": {
- "output_text": "<IPython.core.display.HTML object>"
- }
- },
- "outputs": [
- {
- "data": {
- "text/html": [
- "\n",
- "<table style=\"border: 2px solid black;\">\n",
- "<tr><td>0,0</td><td>0,1</td><td>0,2</td><td>0,3</td><td>0,4</td></tr><tr><td>1,0</td><td>1,1</td><td>1,2</td><td>1,3</td><td>1,4</td></tr><tr><td>2,0</td><td>2,1</td><td>2,2</td><td>2,3</td><td>2,4</td></tr><tr><td>3,0</td><td>3,1</td><td>3,2</td><td>3,3</td><td>3,4</td></tr><tr><td>4,0</td><td>4,1</td><td>4,2</td><td>4,3</td><td>4,4</td></tr>\n",
- "</table>\n"
- ],
- "text/plain": [
- "<IPython.core.display.HTML object>"
- ]
- },
- "execution_count": 12,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "HTML('''\n",
- "<table style=\"border: 2px solid black;\">\n",
- "''' +\n",
- " ''.join(['<tr>' +\n",
- " ''.join([f'<td>{row},{col}</td>'\n",
- " for col in range(5)]) +\n",
- " '</tr>' for row in range(5)]) +\n",
- " '''\n",
- "</table>\n",
- "''')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 13,
- "metadata": {
- "podoc": {
- "output_text": "<IPython.core.display.SVG object>"
- }
- },
- "outputs": [
- {
- "data": {
- "image/svg+xml": [
- "<svg height=\"80\" width=\"600\"><circle cx=\"300\" cy=\"30\" fill=\"red\" r=\"0.0\" stroke=\"black\" stroke-width=\"2\">\n",
- " </circle><circle cx=\"297\" cy=\"30\" fill=\"red\" r=\"3.0\" stroke=\"black\" stroke-width=\"2\">\n",
- " </circle><circle cx=\"288\" cy=\"30\" fill=\"red\" r=\"6.0\" stroke=\"black\" stroke-width=\"2\">\n",
- " </circle><circle cx=\"273\" cy=\"30\" fill=\"red\" r=\"9.0\" stroke=\"black\" stroke-width=\"2\">\n",
- " </circle><circle cx=\"252\" cy=\"30\" fill=\"red\" r=\"12.0\" stroke=\"black\" stroke-width=\"2\">\n",
- " </circle><circle cx=\"225\" cy=\"30\" fill=\"red\" r=\"15.0\" stroke=\"black\" stroke-width=\"2\">\n",
- " </circle><circle cx=\"192\" cy=\"30\" fill=\"red\" r=\"18.0\" stroke=\"black\" stroke-width=\"2\">\n",
- " </circle><circle cx=\"153\" cy=\"30\" fill=\"red\" r=\"21.0\" stroke=\"black\" stroke-width=\"2\">\n",
- " </circle><circle cx=\"108\" cy=\"30\" fill=\"red\" r=\"24.0\" stroke=\"black\" stroke-width=\"2\">\n",
- " </circle><circle cx=\"57\" cy=\"30\" fill=\"red\" r=\"27.0\" stroke=\"black\" stroke-width=\"2\">\n",
- " </circle></svg>"
- ],
- "text/plain": [
- "<IPython.core.display.SVG object>"
- ]
- },
- "execution_count": 13,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "SVG('''<svg width=\"600\" height=\"80\">''' +\n",
- " ''.join([f'''<circle\n",
- " cx=\"{(30 + 3*i) * (10 - i)}\"\n",
- " cy=\"30\"\n",
- " r=\"{3. * float(i)}\"\n",
- " fill=\"red\"\n",
- " stroke-width=\"2\"\n",
- " stroke=\"black\">\n",
- " </circle>''' for i in range(10)]) +\n",
- " '''</svg>''')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "podoc": {
- "output_text": "<IPython.lib.display.YouTubeVideo at 0x7fc0000b64a8>"
- }
- },
- "outputs": [],
- "source": [
- "YouTubeVideo('VQBZ2MqWBZI')"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "```json\n",
- "{\n",
- " \"cells\": [\n",
- " {\n",
- " \"cell_type\": \"code\",\n",
- " \"execution_count\": 1,\n",
- " \"metadata\": {},\n",
- " \"outputs\": [\n",
- " {\n",
- " \"name\": \"stdout\",\n",
- " \"output_type\": \"stream\",\n",
- " \"text\": [\n",
- " \"Hello world!\\n\"\n",
- " ]\n",
- " }\n",
- " ],\n",
- " \"source\": [\n",
- " \"print(\\\"Hello world!\\\")\"\n",
- " ]\n",
- " }\n",
- " ],\n",
- " \"metadata\": {},\n",
- " \"nbformat\": 4,\n",
- " \"nbformat_minor\": 2\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.6.9"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
|