python笔记
PEP8编码规范
元组的方法
Python3 中常用字符串方法(method)
Python3 集合的方法
Python3中常用的列表方法(method)
字典的方法
bytearray 的方法:
Python全部的错误类型
Python运算符优先级
数值对象的构造(创建)函数
时间模块 time
数学模块 math
随机模块 random
系统模块 sys
文件
本文档使用 MrDoc 发布
-
+
首页
数值对象的构造(创建)函数
<!DOCTYPE html> <!-- saved from url=(0092)http://172.88.5.36/python_base_docs_html/%E5%86%85%E5%BB%BA%E5%87%BD%E6%95%B0(builtins).html --> <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>内建函数(builtins).md—/Volumes/DATA/File/Work/tedu/weimz/python/教学笔记/html/python_base_docs</title> <style type="text/css"> html{font-family: sans-serif;-ms-text-size-adjust: 100%;-webkit-text-size-adjust: 100%}body{margin: 0}article,aside,details,figcaption,figure,footer,header,hgroup,main,nav,section,summary{display: block}audio,canvas,progress,video{display: inline-block;vertical-align: baseline}audio:not([controls]){display: none;height: 0}[hidden],template{display: none}a{background: transparent}a:active,a:hover{outline: 0}abbr[title]{border-bottom: 1px dotted}b,strong{font-weight: bold}dfn{font-style: italic}h1{font-size: 2em;margin: 0.67em 0}mark{background: #ff0;color: #000}small{font-size: 80%}sub,sup{font-size: 75%;line-height: 0;position: relative;vertical-align: baseline}sup{top: -0.5em}sub{bottom: -0.25em}img{border: 0}svg:not(:root){overflow: hidden}figure{margin: 1em 40px}hr{box-sizing: content-box;height: 0}pre{overflow: auto}code,kbd,pre,samp{font-family: monospace, monospace;font-size: 1em}button,input,optgroup,select,textarea{color: inherit;font: inherit;margin: 0}button{overflow: visible}button,select{text-transform: none}button,html input[type="button"],input[type="reset"],input[type="submit"]{-webkit-appearance: button;cursor: pointer}button[disabled],html input[disabled]{cursor: default}button::-moz-focus-inner,input::-moz-focus-inner{border: 0;padding: 0}input{line-height: normal}input[type="checkbox"],input[type="radio"]{box-sizing: border-box;padding: 0}input[type="number"]::-webkit-inner-spin-button,input[type="number"]::-webkit-outer-spin-button{height: auto}input[type="search"]{-webkit-appearance: textfield;box-sizing: content-box}input[type="search"]::-webkit-search-cancel-button,input[type="search"]::-webkit-search-decoration{-webkit-appearance: none}fieldset{border: 1px solid #c0c0c0;margin: 0 2px;padding: 0.35em 0.625em 0.75em}legend{border: 0;padding: 0}textarea{overflow: auto}optgroup{font-weight: bold}table{border-collapse: collapse;border-spacing: 0}td,th{padding: 0}*{box-sizing: border-box}input,select,textarea,button{font: 13px/1.4 Helvetica, arial, freesans, clean, sans-serif, "Segoe UI Emoji", "Segoe UI Symbol"}body{min-width: 1020px;font: 13px/1.4 Helvetica, arial, freesans, clean, sans-serif, "Segoe UI Emoji", "Segoe UI Symbol";color: #333;background-color: #fff}a{color: #4183c4;text-decoration: none}a:hover,a:active{text-decoration: underline}hr,.rule{height: 0;margin: 15px 0;overflow: hidden;background: transparent;border: 0;border-bottom: 1px solid #ddd}hr:before,.rule:before{display: table;content: ""}hr:after,.rule:after{display: table;clear: both;content: ""}h1,h2,h3,h4,h5,h6{margin-top: 15px;margin-bottom: 15px;line-height: 1.1}h1{font-size: 30px}h2{font-size: 21px}h3{font-size: 16px}h4{font-size: 14px}h5{font-size: 12px}h6{font-size: 11px}small{font-size: 90%}blockquote{margin: 0}.lead{margin-bottom: 30px;font-size: 20px;font-weight: 300;color: #555}.text-muted{color: #999}.text-danger{color: #bd2c00}.text-emphasized{font-weight: bold;color: #333}ul,ol{padding: 0;margin-top: 0;margin-bottom: 0}ol ol,ul ol{list-style-type: lower-roman}ul ul ol,ul ol ol,ol ul ol,ol ol ol{list-style-type: lower-alpha}dd{margin-left: 0}tt,code{font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;font-size: 12px}pre{margin-top: 0;margin-bottom: 0;font: 12px Consolas, "Liberation Mono", Menlo, Courier, monospace}#realtime .status{overflow: visible;position: absolute;top: -5px;left: 0;background: url("/public/images/github-status.png");width: 26px;height: 26px;display: block;margin: 0 5px 0 0}#realtime .up{background-position: 0 0}#realtime .problem{background-position: 0 -53px}#realtime .down{background-position: 0 -26px}.container{max-width: 920px;margin: 0 auto 20px auto}#header{background: #FAFAFA;background: -moz-linear-gradient(#FAFAFA, #EAEAEA);background: -webkit-linear-gradient(#FAFAFA, #EAEAEA);-ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr='#fafafa', endColorstr='#eaeaea')";border-bottom: 1px solid #CACACA;box-shadow: 0 1px 0 rgba(255, 255, 255, 0.4),0 0 10px rgba(0, 0, 0, 0.1)}#markup{padding: 3px}#markup article{padding-top: 30px}.markdown-body{overflow: hidden;font-family: "Helvetica Neue", Helvetica, "Segoe UI", Arial, freesans, sans-serif;font-size: 16px;line-height: 1.6;word-wrap: break-word}.markdown-body>*:first-child{margin-top: 0 !important}.markdown-body>*:last-child{margin-bottom: 0 !important}.markdown-body .absent{color: #c00}.markdown-body .anchor{position: absolute;top: 0;left: 0;display: block;padding-right: 6px;padding-left: 30px;margin-left: -30px}.markdown-body .anchor:focus{outline: none}.markdown-body h1,.markdown-body h2,.markdown-body h3,.markdown-body h4,.markdown-body h5,.markdown-body h6{position: relative;margin-top: 1em;margin-bottom: 16px;font-weight: bold;line-height: 1.4}.markdown-body h1 .octicon-link,.markdown-body h2 .octicon-link,.markdown-body h3 .octicon-link,.markdown-body h4 .octicon-link,.markdown-body h5 .octicon-link,.markdown-body h6 .octicon-link{display: none;color: #000;vertical-align: middle}.markdown-body h1:hover .anchor,.markdown-body h2:hover .anchor,.markdown-body h3:hover .anchor,.markdown-body h4:hover .anchor,.markdown-body h5:hover .anchor,.markdown-body h6:hover .anchor{padding-left: 8px;margin-left: -30px;text-decoration: none}.markdown-body h1:hover .anchor .octicon-link,.markdown-body h2:hover .anchor .octicon-link,.markdown-body h3:hover .anchor .octicon-link,.markdown-body h4:hover .anchor .octicon-link,.markdown-body h5:hover .anchor .octicon-link,.markdown-body h6:hover .anchor .octicon-link{display: inline-block}.markdown-body h1 tt,.markdown-body h1 code,.markdown-body h2 tt,.markdown-body h2 code,.markdown-body h3 tt,.markdown-body h3 code,.markdown-body h4 tt,.markdown-body h4 code,.markdown-body h5 tt,.markdown-body h5 code,.markdown-body h6 tt,.markdown-body h6 code{font-size: inherit}.markdown-body h1{padding-bottom: 0.3em;font-size: 2.25em;line-height: 1.2;border-bottom: 1px solid #eee}.markdown-body h1 .anchor{line-height: 1}.markdown-body h2{padding-bottom: 0.3em;font-size: 1.75em;line-height: 1.225;border-bottom: 1px solid #eee}.markdown-body h2 .anchor{line-height: 1}.markdown-body h3{font-size: 1.5em;line-height: 1.43}.markdown-body h3 .anchor{line-height: 1.2}.markdown-body h4{font-size: 1.25em}.markdown-body h4 .anchor{line-height: 1.2}.markdown-body h5{font-size: 1em}.markdown-body h5 .anchor{line-height: 1.1}.markdown-body h6{font-size: 1em;color: #777}.markdown-body h6 .anchor{line-height: 1.1}.markdown-body p,.markdown-body blockquote,.markdown-body ul,.markdown-body ol,.markdown-body dl,.markdown-body table,.markdown-body pre{margin-top: 0;margin-bottom: 16px}.markdown-body hr{height: 4px;padding: 0;margin: 16px 0;background-color: #e7e7e7;border: 0 none}.markdown-body ul,.markdown-body ol{padding-left: 2em}.markdown-body ul.no-list,.markdown-body ol.no-list{padding: 0;list-style-type: none}.markdown-body ul ul,.markdown-body ul ol,.markdown-body ol ol,.markdown-body ol ul{margin-top: 0;margin-bottom: 0}.markdown-body li>p{margin-top: 16px}.markdown-body dl{padding: 0}.markdown-body dl dt{padding: 0;margin-top: 16px;font-size: 1em;font-style: italic;font-weight: bold}.markdown-body dl dd{padding: 0 16px;margin-bottom: 16px}.markdown-body blockquote{padding: 0 15px;color: #777;border-left: 4px solid #ddd}.markdown-body blockquote>:first-child{margin-top: 0}.markdown-body blockquote>:last-child{margin-bottom: 0}.markdown-body table{display: block;width: 100%;overflow: auto;word-break: normal;word-break: keep-all}.markdown-body table th{font-weight: bold}.markdown-body table th,.markdown-body table td{padding: 6px 13px;border: 1px solid #ddd}.markdown-body table tr{background-color: #fff;border-top: 1px solid #ccc}.markdown-body table tr:nth-child(2n){background-color: #f8f8f8}.markdown-body img{max-width: 100%;box-sizing: border-box}.markdown-body span.frame{display: block;overflow: hidden}.markdown-body span.frame>span{display: block;float: left;width: auto;padding: 7px;margin: 13px 0 0;overflow: hidden;border: 1px solid #ddd}.markdown-body span.frame span img{display: block;float: left}.markdown-body span.frame span span{display: block;padding: 5px 0 0;clear: both;color: #333}.markdown-body span.align-center{display: block;overflow: hidden;clear: both}.markdown-body span.align-center>span{display: block;margin: 13px auto 0;overflow: hidden;text-align: center}.markdown-body span.align-center span img{margin: 0 auto;text-align: center}.markdown-body span.align-right{display: block;overflow: hidden;clear: both}.markdown-body span.align-right>span{display: block;margin: 13px 0 0;overflow: hidden;text-align: right}.markdown-body span.align-right span img{margin: 0;text-align: right}.markdown-body span.float-left{display: block;float: left;margin-right: 13px;overflow: hidden}.markdown-body span.float-left span{margin: 13px 0 0}.markdown-body span.float-right{display: block;float: right;margin-left: 13px;overflow: hidden}.markdown-body span.float-right>span{display: block;margin: 13px auto 0;overflow: hidden;text-align: right}.markdown-body code,.markdown-body tt{padding: 0;padding-top: 0.2em;padding-bottom: 0.2em;margin: 0;font-size: 85%;background-color: rgba(0,0,0,0.04);border-radius: 3px}.markdown-body code:before,.markdown-body code:after,.markdown-body tt:before,.markdown-body tt:after{letter-spacing: -0.2em;content: "\00a0"}.markdown-body code br,.markdown-body tt br{display: none}.markdown-body del code{text-decoration: inherit}.markdown-body pre>code{padding: 0;margin: 0;font-size: 100%;word-break: normal;white-space: pre;background: transparent;border: 0}.markdown-body .highlight{margin-bottom: 16px}.markdown-body .highlight pre,.markdown-body pre{padding: 16px;overflow: auto;font-size: 85%;line-height: 1.45;background-color: #f7f7f7;border-radius: 3px}.markdown-body .highlight pre{margin-bottom: 0;word-break: normal}.markdown-body pre{word-wrap: normal}.markdown-body pre code,.markdown-body pre tt{display: inline;max-width: initial;padding: 0;margin: 0;overflow: initial;line-height: inherit;word-wrap: normal;background-color: transparent;border: 0}.markdown-body pre code:before,.markdown-body pre code:after,.markdown-body pre tt:before,.markdown-body pre tt:after{content: normal}.markdown-body kbd{display: inline-block;padding: 3px 5px;font-size: 11px;line-height: 10px;color: #555;vertical-align: middle;background-color: #fcfcfc;border: solid 1px #ccc;border-bottom-color: #bbb;border-radius: 3px;box-shadow: inset 0 -1px 0 #bbb}.codehilite{background: #ffffff}.codehilite .c{color: #999988;font-style: italic}.codehilite .err{color: #a61717;background-color: #e3d2d2}.codehilite .k{color: #000000;font-weight: bold}.codehilite .o{color: #000000;font-weight: bold}.codehilite .cm{color: #999988;font-style: italic}.codehilite .cp{color: #999999;font-weight: bold}.codehilite .c1{color: #999988;font-style: italic}.codehilite .cs{color: #999999;font-weight: bold;font-style: italic}.codehilite .gd{color: #000000;background-color: #ffdddd}.codehilite .gd .x{color: #000000;background-color: #ffaaaa}.codehilite .ge{color: #000000;font-style: italic}.codehilite .gr{color: #aa0000}.codehilite .gh{color: #999999}.codehilite .gi{color: #000000;background-color: #ddffdd}.codehilite .gi .x{color: #000000;background-color: #aaffaa}.codehilite .go{color: #888888}.codehilite .gp{color: #555555}.codehilite .gs{font-weight: bold}.codehilite .gu{color: #aaaaaa}.codehilite .gt{color: #aa0000}.codehilite .kc{color: #000000;font-weight: bold}.codehilite .kd{color: #000000;font-weight: bold}.codehilite .kp{color: #000000;font-weight: bold}.codehilite .kr{color: #000000;font-weight: bold}.codehilite .kt{color: #445588;font-weight: bold}.codehilite .m{color: #009999}.codehilite .s{color: #d14}.codehilite .na{color: #008080}.codehilite .nb{color: #0086B3}.codehilite .nc{color: #445588;font-weight: bold}.codehilite .no{color: #008080}.codehilite .ni{color: #800080}.codehilite .ne{color: #990000;font-weight: bold}.codehilite .nf{color: #990000;font-weight: bold}.codehilite .nn{color: #555555}.codehilite .nt{color: #000080}.codehilite .nv{color: #008080}.codehilite .ow{color: #000000;font-weight: bold}.codehilite .w{color: #bbbbbb}.codehilite .mf{color: #009999}.codehilite .mh{color: #009999}.codehilite .mi{color: #009999}.codehilite .mo{color: #009999}.codehilite .sb{color: #d14}.codehilite .sc{color: #d14}.codehilite .sd{color: #d14}.codehilite .s2{color: #d14}.codehilite .se{color: #d14}.codehilite .sh{color: #d14}.codehilite .si{color: #d14}.codehilite .sx{color: #d14}.codehilite .sr{color: #009926}.codehilite .s1{color: #d14}.codehilite .ss{color: #990073}.codehilite .bp{color: #999999}.codehilite .vc{color: #008080}.codehilite .vg{color: #008080}.codehilite .vi{color: #008080}.codehilite .il{color: #009999} </style> <style type="text/css"> .markdown-body hr{background:url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAYAAAAECAYAAACtBE5DAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYwIDYxLjEzNDc3NywgMjAxMC8wMi8xMi0xNzozMjowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNSBNYWNpbnRvc2giIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6OENDRjNBN0E2NTZBMTFFMEI3QjRBODM4NzJDMjlGNDgiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6OENDRjNBN0I2NTZBMTFFMEI3QjRBODM4NzJDMjlGNDgiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDo4Q0NGM0E3ODY1NkExMUUwQjdCNEE4Mzg3MkMyOUY0OCIgc3RSZWY6ZG9jdW1lbnRJRD0ieG1wLmRpZDo4Q0NGM0E3OTY1NkExMUUwQjdCNEE4Mzg3MkMyOUY0OCIvPiA8L3JkZjpEZXNjcmlwdGlvbj4gPC9yZGY6UkRGPiA8L3g6eG1wbWV0YT4gPD94cGFja2V0IGVuZD0iciI/PqqezsUAAAAfSURBVHjaYmRABcYwBiM2QSA4y4hNEKYDQxAEAAIMAHNGAzhkPOlYAAAAAElFTkSuQmCC")} </style> </head> <body> <div class="container"> <div id="markup"> <article id="content" class="markdown-body"> <h3 id="python">《Python 教程》授课笔记附件</h3> <ul> <li>讲师: 魏明择</li> <li>时间: 2019</li> </ul> <h3 id="_1">数值对象的构造(创建)函数</h3> <table> <thead> <tr> <th>函数</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>float(obj)</td> <td>用字符串或数字转换为浮点数, 如果不给出参数,则返回0.0</td> </tr> <tr> <td>int(x, base=10)<br>int(x=0)</td> <td>用数字或字符串转换为整数,如果不给出参数,则返回0</td> </tr> <tr> <td>complex(r=0.0, i=0.0)</td> <td>用数字创建一个复数(实部为r,虚部为i)</td> </tr> <tr> <td>bool(x)</td> <td>用x获取一个布尔值(True/False)</td> </tr> </tbody> </table> <h3 id="_2">预置(内建)的数值型函数</h3> <table> <thead> <tr> <th>函数</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>abs(x)</td> <td>取x的绝对值</td> </tr> <tr> <td>round(number[, ndigits])</td> <td>对数值进行"四舍五入", ndigits是小数向右<br>取整的位数, 负数表示向左取整</td> </tr> <tr> <td>pow(x, y, z=None)</td> <td>相当于x ** y 或 x**y % z</td> </tr> </tbody> </table> <h3 id="help">help() 查看函数帮助:</h3> <div class="codehilite"><pre>help函数 >>> help(abs) # 查看abs函数的帮助 </pre></div> <h2 id="_3">基本输入输出</h2> <h3 id="input">基本输入 input</h3> <table> <thead> <tr> <th>函数</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>input('提示字符串')</td> <td>返回输入的字符串(仅Python3,'提示字符串' 可以是空)</td> </tr> </tbody> </table> <h3 id="print">基本输出函数 print</h3> <table> <thead> <tr> <th>函数</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)</td> <td>将一系列的值以字符串形式输出到<br>标准输出设备上,默认为终端.</td> </tr> </tbody> </table> <h4 id="_4">参数选项</h4> <div class="codehilite"><pre>选项的关键字参数为: sep: 两个值之间的分隔符,默认为一个空格' ' end: 输出完毕后在流末尾自动追加一个字符串,默认为换行符'\n' file: 流对象,默认为sys.stdout. flush: 是否立即将流进行输出 </pre></div> <h3 id="_5">字符编码转换函数</h3> <table> <thead> <tr> <th>函数</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>ord(c)</td> <td>返回一个字符c的Unicode编码值</td> </tr> <tr> <td>chr(i)</td> <td>返回i这个值所对应的 字符</td> </tr> </tbody> </table> <h3 id="_6">整数转换为字符串函数</h3> <table> <thead> <tr> <th>函数</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>bin(i)</td> <td>将整数转换为二进制字符串</td> </tr> <tr> <td>oct(i)</td> <td>将整数转换为八进制字符串</td> </tr> <tr> <td>hex(i)</td> <td>将整数转换为十六进制字符串</td> </tr> </tbody> </table> <h3 id="_7">字符串构造函数</h3> <table> <thead> <tr> <th>函数</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>str(obj='')</td> <td>将对象转换为字符串</td> </tr> </tbody> </table> <h4 id="range">range()函数</h4> <table> <thead> <tr> <th>函数</th> <th>意义</th> </tr> </thead> <tbody> <tr> <td>range(stop)</td> <td>用来生成 0 ~ stop 区间内的整数,直到stop为止(不包含stop)</td> </tr> <tr> <td>range(start,stop[,step])</td> <td>用来生成start~stop区间内的整数,直到stop为止,每个整数间隔step,直到stop为止(不包含stop,且step可以是负整数)</td> </tr> </tbody> </table> <h5 id="_8">格式:</h5> <p>(见:help(range))</p> <h3 id="list">列表的构造(创建)函数list</h3> <table> <thead> <tr> <th>函数</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>list()</td> <td># 生成一个空的列表 等同于 []</td> </tr> <tr> <td>list(iterable)</td> <td># 用可迭代对象创建一个列表</td> </tr> </tbody> </table> <h3 id="python3">Python3中常用的序列函数</h3> <table> <thead> <tr> <th>函数</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>len(seq)</td> <td>返回序列的长度</td> </tr> <tr> <td>max(x)</td> <td>返回序列的最大值的元素</td> </tr> <tr> <td>min(x)</td> <td>返回序列的最小值的元素</td> </tr> <tr> <td>------以上是以前学的---------</td> <td></td> </tr> <tr> <td>sum(x)</td> <td>返回序列中所有元素的和(元素必须是数值类型)</td> </tr> <tr> <td>any(x)</td> <td>真值测试,如果列表中其中一个值为真值则返回True</td> </tr> <tr> <td>all(x)</td> <td>真值测试,如果列表中所有值为真值则返回True</td> </tr> <tr> <td></td> <td></td> </tr> <tr> <td><!-- 以下函数后面再讲 --></td> <td></td> </tr> <tr> <td>reversed(seq)</td> <td>返回原序列反向顺序的可迭代对象</td> </tr> <tr> <td>sorted(iterable, reverse=False)</td> <td>返回已排序对象的</td> </tr> <tr> <td>str(obj)</td> <td>将对象obj序列化为一个字符串</td> </tr> <tr> <td>list(iterable)</td> <td>用可迭代对象生成一个列表</td> </tr> <tr> <td>tuple(iterable)</td> <td>用可迭代对象生成一个元组</td> </tr> </tbody> </table> <h3 id="tuple">元组的构造(创建)函数tuple</h3> <table> <thead> <tr> <th>函数</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>tuple()</td> <td>生成一个空的元组,等同于 ()</td> </tr> <tr> <td>tuple(iterable)</td> <td>用可迭代对象生成一个元组</td> </tr> </tbody> </table> <h4 id="dictdict">dict的构造(创建)函数dict</h4> <table> <thead> <tr> <th>函数</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>dict()</td> <td># 生成一个空的字典 等同于 {}</td> </tr> <tr> <td>dict(iterable)</td> <td>用可迭代对象初始化一个字典</td> </tr> <tr> <td>dict(**kwargs)</td> <td>关键字传参形式生成一个字典</td> </tr> </tbody> </table> <!-- dict(mapping) | (key, value) 对形式的初始化 --> <h3 id="set">集合构造(创建)函数 set</h3> <table> <thead> <tr> <th>函数</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>set()</td> <td>创建一个空的集合对象(不能用{}来创建空集合)</td> </tr> <tr> <td>set(iterable)</td> <td>用可迭代对象创建一个新的集合对象</td> </tr> </tbody> </table> <h3 id="frozenset">固定集合构造(创建)函数 frozenset</h3> <table> <thead> <tr> <th>函数</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>frozenset()</td> <td>创建一个空的固定集合对象</td> </tr> <tr> <td>frozenset(iterable)</td> <td>用可迭代对象创建一个新的固定集合对象</td> </tr> </tbody> </table> <h2 id="globals-locals">globals() / locals() 函数</h2> <table> <thead> <tr> <th>函数</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>globals()</td> <td>返回当前全局作用域内变量的字典</td> </tr> <tr> <td>locals()</td> <td>返回当前局部作用域内变量的字典</td> </tr> </tbody> </table> <h2 id="eval-exec">eval(), exec() <!-- 和compile() --> 函数</h2> <table> <thead> <tr> <th>函数</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>eval(source, globals=None, locals=None)</td> <td>把一个字符串source当成一个表达式来执行,返回表达式执行后的结果</td> </tr> <tr> <td>exec(source, globals=None, locals=None)</td> <td>把一个字符串source当成程序来执行.</td> </tr> </tbody> </table> <!-- 此处需要重新整理 --> <h2 id="high-order-function">高阶函数 High Order Function</h2> <table> <thead> <tr> <th align="center">函数</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td align="center">map(func, iterable, ...)</td> <td>返回一个可迭代对象,此可迭代对象用<br>函数func对可迭代对象iterables中的每一个元素作为参数计算后得到结果;<br>当最短的一个可迭代对象不再提供数据时此可迭代对象生成结束</td> </tr> <tr> <td align="center">filter(function, iterable)</td> <td>返回一个可迭代器对象,此可迭代对象将对iterabler获取到的数据<br>用函数function 进行筛选.function将对iterable中的每个元素进行求值,<br>返回False则将此数据丢弃,返回True,则保留此数据</td> </tr> <tr> <td align="center">sorted(iterable,<br>key=None,<br>reverse=False)</td> <td>返回一个新的包含有所有可迭代对象中数据升序排序的列表<br>iterable 可迭代对象<br>key 函数是用来提供一个值,这个值将作为排序的依据<br>reverse 标志用来设置是否降序排序</td> </tr> </tbody> </table> <h3 id="dir">dir函数</h3> <table> <thead> <tr> <th>函数</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>dir([对象])</td> <td>返回一个字符串列表</td> </tr> </tbody> </table> <h4 id="dir_1">dir函数作用</h4> <ul> <li>如果没有参数调用,则返回当前作用域内的所有变量的列表</li> <li>如果给定一个对象作为参数,则返回这个对象的所有变量的列表<ul> <li>对于一个模块,返回这个模块的全部变量</li> <li>对于一个类对象,返回类对象的所有变量,并递归的基类对象的所有变量</li> <li>对于其它对象返回所有变量、类变量和基类变量</li> </ul> </li> </ul> <h3 id="iternext">迭代器函数iter和next</h3> <table> <thead> <tr> <th>函数</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>iter(iterable)</td> <td>从可迭代对象中返回一个迭代器,iterable必须是能提供一个迭代器的对象</td> </tr> <tr> <td>next(iterator)</td> <td>从迭代器iterator中获取下一个记录,如果无法获取一下条记录,则触发 StopIteration 异常</td> </tr> </tbody> </table> <h3 id="_9">迭代工具函数</h3> <ul> <li>迭代工具函数的作用是生成一个个性化可迭代对象</li> </ul> <table> <thead> <tr> <th>函数</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>zip(iter1 [,iter2 [...]])</td> <td>返回一个zip生成器对象, 此对象用于生成一个元组,此元组的数据分别来自于参数中每个可迭代对象,生成元组的个数由最小的可迭代对象大小决定</td> </tr> <tr> <td>enumerate(iterable, start=0)</td> <td>返回一个enumerate生成器对象,此对象生成类型为(索引,值对)的元组,<br>默认索引从零开始,也可以用start指定</td> </tr> </tbody> </table> <h3 id="bytes">字节串的构造函数bytes</h3> <table> <thead> <tr> <th>函数</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>bytes()</td> <td># 生成一个空的字节串 等同于 b''</td> </tr> <tr> <td>bytes(整型可迭代对象)</td> <td># 用可迭代对象初始化一个字节串</td> </tr> <tr> <td>bytes(整数n)</td> <td>生成n个值为0的字节串</td> </tr> <tr> <td>bytes(字符串, encoding='utf-8')</td> <td>用字符串的转换编码生成一个字节串</td> </tr> </tbody> </table> <h3 id="bytearray">字节数组的构造函数 bytearray</h3> <table> <thead> <tr> <th>函数</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>bytearray()</td> <td>创建空的字节数组</td> </tr> <tr> <td>bytearray(整数)</td> <td>用可迭代对象初始化一个字节数组</td> </tr> <tr> <td>bytearray(整型可迭代对象)</td> <td>生成n个值为0的字节数组</td> </tr> <tr> <td>bytearray(字符串, encoding='utf-8')</td> <td>用字符串的转换编码生成一个字节数组</td> </tr> </tbody> </table> <h3 id="_10">文件的打开函数</h3> <table> <thead> <tr> <th>字符</th> <th>含义</th> </tr> </thead> <tbody> <tr> <td>open(file, mode='rt')</td> <td>用于打开一个文件,返回此文件流对象. <br>如果打开文件失败,则会触发OSError 错误! <br>如果要打开的文件不存在,则会触发FileNotFoundError 错误!</td> </tr> </tbody> </table> <h3 id="_11">用于类的函数</h3> <table> <thead> <tr> <th>函数</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>isinstance(obj, class_or_tuple)</td> <td>返回这个对象obj 是否是 某个类的对象,或者某些类<br>中的一个类的对象,如果是返回True,否则返回False</td> </tr> <tr> <td>type(obj)</td> <td>返回对象的类型</td> </tr> <tr> <td></td> <td></td> </tr> </tbody> </table> <h3 id="super">super函数</h3> <table> <thead> <tr> <th>函数</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>super(cls, obj)</td> <td>返回绑定超类的实例(要求obj必须为cls类型的实例)</td> </tr> <tr> <td>super()</td> <td>返回绑定超类的实例,等同于:super(<strong>class</strong>, 实例方法的第一个参数),必须用在方法内调用</td> </tr> </tbody> </table> <h3 id="_12">用于类的函数</h3> <table> <thead> <tr> <th>函数</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>issubclass(cls, class_or_tuple)</td> <td>判断一个类是否继承自其它的类,如果此类cls是class 或 tuple中的一个派生子类则返回True,否则返回False</td> </tr> <tr> <td></td> <td></td> </tr> </tbody> </table> <h4 id="_13">对象的属性管理函数</h4> <table> <thead> <tr> <th>函数</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>getattr(obj, name[, default])</td> <td>从一个对象得到对象的属性;getattr(x, 'y') 等同于x.y; 当属性不存在时,如果给<br>出default参数,则返回default,如果没有给出default 则产生一个AttributeError错误</td> </tr> <tr> <td>hasattr(obj, name)</td> <td>用给定的name返回对象obj是否有此属性,此种做法可以避免在getattr(obj, name)时引发错误</td> </tr> <tr> <td>setattr(obj, name, value)</td> <td>给对象obj的名为name的属性设置相应的值value, setattr(x, 'y', v) 等同于 x.y = v</td> </tr> <tr> <td>delattr(obj, name)</td> <td>删除对象obj中的name属性, delattr(x, 'y') 等同于 del x.y</td> </tr> </tbody> </table> <h4 id="_14">对象的属性管理函数示例:</h4> <div class="codehilite"><pre>class Dog: pass d = Dog() d.color = '白色' v = getattr(d, 'color') # 等同于 v = d.color v = getattr(d, 'kinds') # 出错,没有d.kinds属性 v = getattr(d, 'kinds', '没有这个属性') # v= '没有这个属性' hasattr(d, 'color') # True hasattr(d, 'kinds') # False setattr(d, 'kinds', '京巴') # 等同于d.kinds ='京巴' hasattr(d, 'kinds') # True delattr(d, 'kinds') # 等同于 del d.kinds hasattr(d, 'kinds') # False </pre></div> </article> </div> </div> </body></html>
有云转晴
2022年3月1日 21:08
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
分享
链接
类型
密码
更新密码