{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "你好 Python\n" ] } ], "source": [ "print('你好 Python')" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "test\n" ] } ], "source": [ "print('test')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "for i in range(10):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "x = 3.14" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 一级标题\n", "## 二级标题\n", "### 三级标题" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 魔法命令" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## %run" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "大家好,很高兴认识你\n" ] } ], "source": [ "%run ./My_Test.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## %load" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# %load ./My_Test.py\n", "print('大家好,很高兴认识你')\n", "\n", "def my_print(x):\n", "\tprint(x*2)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python好,大家好\n" ] } ], "source": [ "my_print('Python好,大家好')" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "大家好,很高兴认识你\n", "xixi\n" ] } ], "source": [ "from My_Test import my_print\n", "my_print('xixi')" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "大家好,很高兴认识你\n" ] } ], "source": [ "# notebook对同一个文件只会导入一次\n", "%run ./My_Test.py" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python好,大家好Python好,大家好\n" ] } ], "source": [ "my_print('Python好,大家好')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# %timeit" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "415 µs ± 16.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" ] } ], "source": [ "%timeit li = [i**2 for i in range(1000)]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "469 ms ± 21.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" ] } ], "source": [ "%timeit li = [i**2 for i in range(1000000)]" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5.44 µs ± 421 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n" ] } ], "source": [ "%timeit li = [i**2 for i in range(10)]" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "# %timeit 后面只跟一句代码\n", "# 测试代码块 用%%timit" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "742 µs ± 71.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" ] } ], "source": [ "%%timeit\n", "li = []\n", "for i in range(1000):\n", " li.append(i**2)\n", "# 在python中使用列表生成式比for高效 " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## %time" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "# %time 只会测量一次代码的执行时间" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 662 µs, sys: 2 µs, total: 664 µs\n", "Wall time: 678 µs\n" ] } ], "source": [ "%time li = [i**2 for i in range(1000)]" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 953 µs, sys: 60 µs, total: 1.01 ms\n", "Wall time: 1.23 ms\n" ] } ], "source": [ "%%time\n", "li = []\n", "for i in range(1000):\n", " li.append(i**2)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.18 ms ± 403 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" ] } ], "source": [ "import random\n", "li = [random.random() for i in range(100000)]\n", "%timeit li.sort()" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 27.1 ms, sys: 2.68 ms, total: 29.8 ms\n", "Wall time: 31 ms\n" ] } ], "source": [ "li = [random.random() for i in range(100000)]\n", "%time li.sort()" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 3.61 ms, sys: 133 µs, total: 3.74 ms\n", "Wall time: 4.09 ms\n" ] } ], "source": [ "%time li.sort()" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 3.3 ms, sys: 14 µs, total: 3.31 ms\n", "Wall time: 3.4 ms\n" ] } ], "source": [ "%time li.sort()" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 4.06 ms, sys: 137 µs, total: 4.19 ms\n", "Wall time: 4.28 ms\n" ] } ], "source": [ "%time li.sort()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## %%html" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/html": [ "