{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 1: Computing with Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Robert Johansson\n", "\n", "Source code listings for [Numerical Python - Scientific Computing and Data Science Applications with Numpy, SciPy and Matplotlib](https://link.springer.com/book/10.1007/979-8-8688-0413-7) (ISBN 979-8-8688-0412-0)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Interpreter" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting hello.py\n" ] } ], "source": [ "%%writefile hello.py\n", "print(\"Hello from Python!\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello from Python!\n" ] } ], "source": [ "!python hello.py" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python 3.10.12\n" ] } ], "source": [ "!python --version" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Input and output caching\n", "\n", "(restart the kernel for the same output and cell numbers)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 * 3" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'3 * 3'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "In[1]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Out[1]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "['', '3 * 3', 'In[1]', 'Out[1]', 'In']" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "In" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "{1: 9, 2: '3 * 3', 3: 9, 4: ['', '3 * 3', 'In[1]', 'Out[1]', 'In', 'Out']}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Out" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1+2" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "tags": [] }, "outputs": [], "source": [ "1+2;" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "tags": [] }, "outputs": [], "source": [ "x = 1" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 2; x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Documentation" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "tags": [] }, "outputs": [], "source": [ "import os" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "tags": [] }, "outputs": [], "source": [ "# try os.w" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "tags": [] }, "outputs": [], "source": [ "import math" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "\u001b[0;31mSignature:\u001b[0m \u001b[0mmath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcos\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m/\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mDocstring:\u001b[0m Return the cosine of x (measured in radians).\n", "\u001b[0;31mType:\u001b[0m builtin_function_or_method" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "math.cos?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Interaction with System Shell" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "tags": [] }, "outputs": [], "source": [ "!touch file1.py file2.py file3.py" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "file1.py file2.py file3.py\n" ] } ], "source": [ "!ls file*" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "tags": [] }, "outputs": [], "source": [ "files = !ls file*" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(files)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "['file1.py', 'file2.py', 'file3.py']" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "files" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "tags": [] }, "outputs": [], "source": [ "file = \"file1.py\"" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-rw-------@ 1 rob staff 0 Nov 2 17:58 file1.py\n" ] } ], "source": [ "!ls -l $file" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Running scripts from the IPython console" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting fib.py\n" ] } ], "source": [ "%%writefile fib.py\n", "\n", "def fib(N): \n", " \"\"\" \n", " Return a list of the first N Fibonacci numbers.\n", " \"\"\" \n", " f0, f1 = 0, 1\n", " f = [1] * N\n", " for n in range(1, N):\n", " f[n] = f0 + f1\n", " f0, f1 = f1, f[n]\n", "\n", " return f\n", "\n", "print(fib(10))" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]\n" ] } ], "source": [ "!python fib.py" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]\n" ] } ], "source": [ "%run fib.py" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[1, 1, 2, 3, 5, 8]" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fib(6)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Debugger" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "tags": [] }, "outputs": [ { "ename": "TypeError", "evalue": "can't multiply sequence by non-int of type 'float'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[25], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mfib\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1.0\u001b[39;49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/OneDrive/Desktop/npbook-ed3/code/numerical-python-code-ed3-v2-github/fib.py:7\u001b[0m, in \u001b[0;36mfib\u001b[0;34m(N)\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\" \u001b[39;00m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;124;03mReturn a list of the first N Fibonacci numbers.\u001b[39;00m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m \n\u001b[1;32m 6\u001b[0m f0, f1 \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m1\u001b[39m\n\u001b[0;32m----> 7\u001b[0m f \u001b[38;5;241m=\u001b[39m \u001b[43m[\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mN\u001b[49m\n\u001b[1;32m 8\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m n \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;241m1\u001b[39m, N):\n\u001b[1;32m 9\u001b[0m f[n] \u001b[38;5;241m=\u001b[39m f0 \u001b[38;5;241m+\u001b[39m f1\n", "\u001b[0;31mTypeError\u001b[0m: can't multiply sequence by non-int of type 'float'" ] } ], "source": [ "fib(1.0)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "> \u001b[0;32m/Users/rob/OneDrive/Desktop/npbook-ed3/code/numerical-python-code-ed3-v2-github/fib.py\u001b[0m(7)\u001b[0;36mfib\u001b[0;34m()\u001b[0m\n", "\u001b[0;32m 5 \u001b[0;31m \"\"\" \n", "\u001b[0m\u001b[0;32m 6 \u001b[0;31m \u001b[0mf0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mf1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0m\u001b[0;32m----> 7 \u001b[0;31m \u001b[0mf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mN\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0m\u001b[0;32m 8 \u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mn\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mN\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0m\u001b[0;32m 9 \u001b[0;31m \u001b[0mf\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mf0\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mf1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0m\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ "ipdb> N\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "1.0\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ "ipdb> q\n" ] } ], "source": [ "%debug" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Timing and profiling code" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "11.6 µs ± 532 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)\n" ] } ], "source": [ "%timeit fib(100)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 21 µs, sys: 1 µs, total: 22 µs\n", "Wall time: 24.8 µs\n" ] } ], "source": [ "result = %time fib(100)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(result)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "tags": [] }, "outputs": [], "source": [ "import numpy as np\n", "\n", "def random_walker_max_distance(M, N):\n", " \"\"\"\n", " Simulate N random walkers taking M steps, and return the largest distance\n", " from the starting point achieved by any of the random walkers.\n", " \"\"\"\n", " trajectories = [np.random.randn(M).cumsum() for _ in range(N)]\n", " return np.max(np.abs(trajectories))" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " " ] }, { "data": { "text/plain": [ " 20013 function calls in 0.335 seconds\n", "\n", " Ordered by: internal time\n", "\n", " ncalls tottime percall cumtime percall filename:lineno(function)\n", " 10000 0.184 0.000 0.184 0.000 {method 'randn' of 'numpy.random.mtrand.RandomState' objects}\n", " 10000 0.071 0.000 0.071 0.000 {method 'cumsum' of 'numpy.ndarray' objects}\n", " 1 0.051 0.051 0.328 0.328 2615584822.py:3(random_walker_max_distance)\n", " 1 0.020 0.020 0.276 0.276 2615584822.py:8()\n", " 1 0.007 0.007 0.335 0.335 :1()\n", " 1 0.002 0.002 0.002 0.002 {method 'reduce' of 'numpy.ufunc' objects}\n", " 1 0.000 0.000 0.335 0.335 {built-in method builtins.exec}\n", " 1 0.000 0.000 0.002 0.002 fromnumeric.py:69(_wrapreduction)\n", " 1 0.000 0.000 0.002 0.002 <__array_function__ internals>:177(amax)\n", " 1 0.000 0.000 0.002 0.002 fromnumeric.py:2675(amax)\n", " 1 0.000 0.000 0.002 0.002 {built-in method numpy.core._multiarray_umath.implement_array_function}\n", " 1 0.000 0.000 0.000 0.000 fromnumeric.py:70()\n", " 1 0.000 0.000 0.000 0.000 fromnumeric.py:2670(_amax_dispatcher)\n", " 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}\n", " 1 0.000 0.000 0.000 0.000 {method 'items' of 'dict' objects}" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%prun random_walker_max_distance(400, 10000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Jupyter notebook" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "tags": [] }, "outputs": [], "source": [ "from IPython.display import display, Image, HTML, Math" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Image(url='http://python.org/images/python-logo.gif')" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "tags": [] }, "outputs": [], "source": [ "import scipy, numpy, matplotlib\n", "modules = [numpy, matplotlib, scipy]\n", "row = \" %s %s \"\n", "rows = \"\\n\".join([row % (module.__name__, module.__version__) for module in modules])\n", "s = \" %s
LibraryVersion
\" % rows" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'\\n\\n
LibraryVersion
numpy 1.22.3
matplotlib 3.7.1
scipy 1.7.3
'" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
LibraryVersion
numpy 1.22.3
matplotlib 3.7.1
scipy 1.7.3
" ], "text/plain": [ "" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "HTML(s)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "tags": [] }, "outputs": [], "source": [ "class HTMLDisplayer(object):\n", " def __init__(self, code):\n", " self.code = code\n", " \n", " def _repr_html_(self):\n", " return self.code" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
LibraryVersion
numpy 1.22.3
matplotlib 3.7.1
scipy 1.7.3
" ], "text/plain": [ "<__main__.HTMLDisplayer at 0x7fd091cbe680>" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "HTMLDisplayer(s)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\hat{H} = -\\frac{1}{2}\\epsilon \\hat{\\sigma}_z-\\frac{1}{2}\\delta \\hat{\\sigma}_x$" ], "text/plain": [ "" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Math(r'\\hat{H} = -\\frac{1}{2}\\epsilon \\hat{\\sigma}_z-\\frac{1}{2}\\delta \\hat{\\sigma}_x')" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "tags": [] }, "outputs": [], "source": [ "class QubitHamiltonian(object):\n", " def __init__(self, epsilon, delta):\n", " self.epsilon = epsilon\n", " self.delta = delta\n", "\n", " def _repr_latex_(self):\n", " return \"$\\hat{H} = -%.2f\\hat{\\sigma}_z-%.2f\\hat{\\sigma}_x$\" % \\\n", " (self.epsilon/2, self.delta/2)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/latex": [ "$\\hat{H} = -0.25\\hat{\\sigma}_z-0.12\\hat{\\sigma}_x$" ], "text/plain": [ "<__main__.QubitHamiltonian at 0x7fd093a851e0>" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "QubitHamiltonian(0.5, 0.25)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "tags": [] }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from scipy import stats\n", "\n", "def f(mu):\n", " X = stats.norm(loc=mu, scale=np.sqrt(mu))\n", " N = stats.poisson(mu)\n", " x = np.linspace(0, X.ppf(0.999))\n", " n = np.arange(0, x[-1])\n", "\n", " fig, ax = plt.subplots()\n", " ax.plot(x, X.pdf(x), color='black', lw=2, label=\"Normal($\\mu=%d, \\sigma^2=%d$)\" % (mu, mu))\n", " ax.bar(n, N.pmf(n), align='edge', label=r\"Poisson($\\lambda=%d$)\" % mu)\n", " ax.set_ylim(0, X.pdf(x).max() * 1.25)\n", " ax.legend(loc=2, ncol=2)\n", " plt.close(fig)\n", " return fig" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "tags": [] }, "outputs": [], "source": [ "from ipywidgets import interact\n", "import ipywidgets as widgets" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "32de52fb68014fa792832416359e353e", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(FloatSlider(value=1.0, description='mu', max=20.0, min=1.0, step=1.0), Output()), _dom_c…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "interact(f, mu=widgets.FloatSlider(min=1.0, max=20.0, step=1.0));" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Jupyter nbconvert" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[NbConvertApp] Converting notebook ch01-code-listing.ipynb to html\n", "[NbConvertApp] Writing 649717 bytes to ch01-code-listing.html\n" ] } ], "source": [ "!jupyter nbconvert --to html ch01-code-listing.ipynb" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[NbConvertApp] Converting notebook ch01-code-listing.ipynb to pdf\n", "[NbConvertApp] Writing 57494 bytes to notebook.tex\n", "[NbConvertApp] Building PDF\n", "[NbConvertApp] Running xelatex 3 times: ['xelatex', 'notebook.tex', '-quiet']\n", "[NbConvertApp] Running bibtex 1 time: ['bibtex', 'notebook']\n", "[NbConvertApp] WARNING | bibtex had problems, most likely because there were no citations\n", "[NbConvertApp] PDF successfully created\n", "[NbConvertApp] Writing 66341 bytes to ch01-code-listing.pdf\n" ] } ], "source": [ "!jupyter nbconvert --to pdf ch01-code-listing.ipynb" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting custom_template.tplx\n" ] } ], "source": [ "%%writefile custom_template.tplx\n", "((*- extends 'article.tplx' -*))\n", "\n", "((* block title *)) \\title{Document title} ((* endblock title *))\n", "((* block author *)) \\author{Author's Name} ((* endblock author *))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!jupyter nbconvert ch01-code-listing.ipynb --to pdf --template custom_template.tplx" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[NbConvertApp] Converting notebook ch01-code-listing.ipynb to python\n", "[NbConvertApp] Writing 5152 bytes to ch01-code-listing.py\n" ] } ], "source": [ "!jupyter nbconvert ch01-code-listing.ipynb --to python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Versions" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "tags": [] }, "outputs": [ { "data": { "application/json": { "Software versions": [ { "module": "Python", "version": "3.10.12 64bit [Clang 14.0.6 ]" }, { "module": "IPython", "version": "8.12.0" }, { "module": "OS", "version": "macOS 10.15.7 x86_64 i386 64bit" }, { "module": "numpy", "version": "1.22.3" } ] }, "text/html": [ "
SoftwareVersion
Python3.10.12 64bit [Clang 14.0.6 ]
IPython8.12.0
OSmacOS 10.15.7 x86\\_64 i386 64bit
numpy1.22.3
Sat Nov 02 17:59:59 2024 JST
" ], "text/latex": [ "\\begin{tabular}{|l|l|}\\hline\n", "{\\bf Software} & {\\bf Version} \\\\ \\hline\\hline\n", "Python & 3.10.12 64bit [Clang 14.0.6 ] \\\\ \\hline\n", "IPython & 8.12.0 \\\\ \\hline\n", "OS & macOS 10.15.7 x86\\_64 i386 64bit \\\\ \\hline\n", "numpy & 1.22.3 \\\\ \\hline\n", "\\hline \\multicolumn{2}{|l|}{Sat Nov 02 17:59:59 2024 JST} \\\\ \\hline\n", "\\end{tabular}\n" ], "text/plain": [ "Software versions\n", "Python 3.10.12 64bit [Clang 14.0.6 ]\n", "IPython 8.12.0\n", "OS macOS 10.15.7 x86_64 i386 64bit\n", "numpy 1.22.3\n", "Sat Nov 02 17:59:59 2024 JST" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%reload_ext version_information\n", "%version_information numpy" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.12" } }, "nbformat": 4, "nbformat_minor": 4 }