{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "skip" }, "toc": "true" }, "source": [ "# Table of Contents\n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Python 内置函数\n", "\n", "python内置了很多简单实用的内置函数,日常工作中会很常用到这些函数,通过这些内置函数能够提高代码得简洁性,节省工作量。" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "![inside-function](inside-function.png)\n", "\n", "查看更多详细:[官方](https://docs.python.org/3/library/functions.html?highlight=format#float)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## abs\n", "\n", "取绝对值" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(-10)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## bool\n", "求布尔值" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "False" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "False" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "True" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "False" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "False" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "False" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "False" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "False" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(True)\n", "bool(False)\n", "bool(0)\n", "bool(1)\n", "bool(None)\n", "\n", "s = \"\"\n", "bool(s)\n", "\n", "t = ()\n", "bool(t)\n", "\n", "l = []\n", "bool(l)\n", "\n", "d = {}\n", "bool(d)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "False None 0 \"\" () [] {}\n", "\n", "标准值 **False** 和 **None**,所有类型的数字 **0**(包括浮点型、长整型和其他类型)、 **空序列** (比如空字符串、元组合列表)以及空的字典都为 **假(False)**,其他得一切都为 **真(True)** 和特殊值 **True**。" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## all\n", "所有为真,结果为真" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "all([1, 2, 3, None])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## any\n", "只要有真,结果就为真" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "any([1, 2, 3, None])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 进制转换" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'0b1010'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "'0o12'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "'0xa'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# bin() 转换2进制 0b\n", "bin(10)\n", "\n", "# oct() 转换8进制 0o\n", "oct(10)\n", "\n", "# hex() 转换16进制 0x\n", "hex(10)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 编码" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b'\\xe6\\xa2\\x81\\xe5\\x85\\x88\\xe6\\xa3\\xae'\n" ] } ], "source": [ "# bytes() 字符串转换成字节\n", "name = '梁先森'\n", "b = bytes(name, encoding=\"utf-8\")\n", "print(b)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "梁先森\n" ] } ], "source": [ "# str() 字节转换字符串\n", "s1 = str(b, encoding=\"utf-8\")\n", "print(s1)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "scrolled": true, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "65\n", "A\n" ] } ], "source": [ "# ASCII 转换\n", "bm = ord('A') # 输入字符返回ascii中对应位置\n", "print(bm)\n", "\n", "ch = chr(bm) # 通过ascii中对应位置转换对应的的字符\n", "print(ch)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## pow\n", "幂运算" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "1024" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "1024" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**10\n", "pow(2, 10)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## divmod\n", "求商和余数" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "(9, 7)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "divmod(97, 10)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## round\n", "四舍五入" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "92" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(91.79)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## max,min,sum\n", "求最大,最小,求和." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "33" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "11" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "66" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max([11,22,33])\n", "min([11,22,33])\n", "sum([11,22,33])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## len\n", "获取一个序列长度,有多少元素" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "3" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "4" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "3" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len('liang')\n", "len((1, 2, 3,))\n", "len([1, 'o', 3, 'p'])\n", "len({\"name\": \"Kevin\", \"age\": 18, \"job\": \"programmer\"}) # 字典的长度即多少个key。" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## zip\n", "拉链,交叉序列,不相同长度序列多出的会丢弃。" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ " at 0x1042e5540, file \"this is string\", line 1>\n",
"123\n"
]
}
],
"source": [
"r = compile(\"print(123)\", \"this is string\", \"exec\") # 参数: python字符串代码, 字符串说明,mode制定编译种类single,eval,exec\n",
"print(r)\n",
"\n",
"#执行代码,接收代码和字符串都能执行,没有返回值\n",
"exec(r) # exec() 执行编译后python的代码"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## filter\n",
"对一个可迭代对象进行筛选"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"text/plain": [
""
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"[6, 7, 8, 9, 10]"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 参数1, 函数\n",
"# 参数2, 一个可迭代对象(序列)\n",
"\"\"\"\n",
"filter() 内部实现感觉应该是这样:\n",
"\n",
"result = []\n",
"for item in 参数2:\n",
" r = 参数1(item) # 执行结果 \n",
" if r: # 如果合法\n",
" result.append(item)\n",
" \n",
"return result # 全部执行完了,吐出过了条件的序列\n",
"\"\"\"\n",
"\n",
"# filter ,循环第二个参数,让每个循环元素执行函数,如果函数返回True,表示元素合法\n",
"\n",
"def f1(num):\n",
" if num > 5:\n",
" return num\n",
" \n",
"li = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
"\n",
"result = filter(f1, li)\n",
"result # 可迭代对象\n",
"list(result) # 变成list"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## map\n",
"迭代操作,遍历序列,对序列每个元素进行操作"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[101, 102, 103, 104, 105, 106, 107, 108, 109, 110]"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def f2(num):\n",
" return num + 100\n",
" \n",
"list(map(f2, li)) # 对list中的每个元素 + 100"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"这里你可能会感觉,**map()** 和 **filter()** 感觉功能一样啊"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[None, None, None, None, None, 6, 7, 8, 9, 10]"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"\"\"\n",
"map() 内部实现感觉是这样:\n",
"\n",
"for item in 参数2:\n",
" return 参数1(item) # 执行结果 \n",
"\"\"\"\n",
"\n",
"# 一不一样试一下不就知道了\n",
"list(map(f1, li))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"**接下来看图理解:**"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"![filter-function](filter-function.png)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"![map-function](map-function.png)"
]
}
],
"metadata": {
"celltoolbar": "Slideshow",
"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.4"
},
"toc": {
"colors": {
"hover_highlight": "#DAA520",
"navigate_num": "#000000",
"navigate_text": "#333333",
"running_highlight": "#FF0000",
"selected_highlight": "#FFD700",
"sidebar_border": "#EEEEEE",
"wrapper_background": "#FFFFFF"
},
"moveMenuLeft": true,
"nav_menu": {
"height": "30px",
"width": "252px"
},
"navigate_menu": true,
"number_sections": false,
"sideBar": true,
"threshold": 4,
"toc_cell": true,
"toc_section_display": "block",
"toc_window_display": false,
"widenNotebook": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}