what's the 内置函数?

  内置函数,内置函数就是python本身定义好的,我们直接拿来就可以用的函数。(python中一共有68中内置函数。)

    Built-in Functions    
abs() dict() help() min() setattr()
all() dir() hex() next() slice()
any() divmod() id() object() sorted()
ascii() enumerate() input() oct() staticmethod()
bin() eval() int() open() str()
bool() exec() isinstance() ord() sum()
bytearray() filter() issubclass() pow() super()
bytes() float() iter() print() tuple()
callable() format() len() property() type()
chr() frozenset() list() range() vars()
classmethod() getattr() locals() repr() zip()
compile() globals() map() reversed() __import__()
complex() hasattr() max() round()  
delattr() hash() memoryview() set()  

下面我们由作用不同分别进行详述:(字体加粗的为重点要掌握的)

与作用域相关:global和local

  • global——获取全局变量的字典
  • local——获取执行本方法所在命名空间内的局部变量的字典

str类型代码的执行:eval、exec、compile

  • eval()——将字符串类型的代码执行并返回结果
  • exec()——将字符串类型的代码执行但不返回结果
  • compile ——将字符串类型的代码编译。代码对象能够通过exec语句来执行或者eval()进行求值
>>> #流程语句使用exec
>>> code1 = 'for i in range(0,10): print (i)'
>>> compile1 = compile(code1,'','exec')
>>> exec (compile1)
1
3
5
7
9 >>> #简单求值表达式用eval
>>> code2 = '1 + 2 + 3 + 4'
>>> compile2 = compile(code2,'','eval')
>>> eval(compile2) >>> #交互语句用single
>>> code3 = 'name = input("please input your name:")'
>>> compile3 = compile(code3,'','single')
>>> name #执行前name变量不存在
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
name
NameError: name 'name' is not defined
>>> exec(compile3) #执行时显示交互命令,提示输入
please input your name:'pythoner'
>>> name #执行后name变量有值
"'pythoner'"

与数字相关的:

  • 数字——数据类型相关:bool,int,float,complex
  • 数字——进制转换相关:bin,oct,hex
  • 数字——数学运算:abs(输出为数字的绝对值),divmod(使用方法即divmod(数字1,数字2),输出为(数字1整除数字2后得到的数,余数)),min,max,sum,round(精确的功能),pow(pow的使用方法即pow(数字1,数字2),数字1**数字2,即次方的形式)
print(divmod(7,3))#(2,1)

print(round(3.14159,2))#3.14
f = 4.197937590783291932703479 #-->二进制转换的问题
print(f)#4.197937590783292

与数据结构有关:

  • 序列——列表和元组相关的:list和tuple
  • 序列——字符串相关的:str,format,bytes,bytesarry,memoryview,ord(ord与chr互为倒数,不过这不需要掌握),chr(返回表示Unicode代码点为整数i的字符的字符串。例如,chr(97)返回字符串'a',同时 chr(8364)返回字符串'€'),ascii,repr
  • 序列:reversed(用l.reverse,将原列表翻转并赋值,用list(reversed(l)只是将原列表翻转看看,不改变原列表的值也就是说不覆盖),slice(切片的功能)
  • 数据集合——字典和集合:dict,set,frozenset
  • 数据集合:len,sorted(排序功能),enumerate(将一个列表的元素由“索引 值”的形式一一解包出来),all,any,zip,filter(一种过滤的功能),map(一种迭代的功能)
l2 = [1,3,5,-2,-4,-6]
print(sorted(l2,key=abs,reverse=True))#[-6, 5, -4, 3, -2, 1]
print(sorted(l2))#[-6, -4, -2, 1, 3, 5]
print(l2)#[1, 3, 5, -2, -4, -6] l = ['a','b']
for i,j in enumerate(l,1):
print(i,j)
#1 a
2 b L = [1,2,3,4]
def pow2(x):
return x*x l=map(pow2,L)
print(list(l))
# 结果:
[1, 4, 9, 16] def is_odd(x):
return x % 2 == 1 l=filter(is_odd, [1, 4, 6, 7, 9, 12, 17])
print(list(l))
# 结果:
[1, 7, 9, 17]

其他:

输入输出:input(),print()

  • input——与用户交互用的
  • print——打印
#print的源码分析:
def print(self, *args, sep=' ', end='\n', file=None): # known special case of print
"""
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
file: 默认是输出到屏幕,如果设置为文件句柄,输出到文件
sep: 打印多个值之间的分隔符,默认为空格
end: 每一次打印的结尾,默认为换行符
flush: 立即把内容输出到流文件,不作缓存
""" #有关进度条打印的小知识
import time
import sys
for i in range(0,101,2):
time.sleep(0.1)
char_num = i//2 #打印多少个#
per_str = '%s%% : %s\n' % (i, '*' * char_num) if i == 100 else '\r%s%% : %s'%(i,'*'*char_num)
print(per_str,end='', file=sys.stdout, flush=True)
复制代码
  • callable——查看参数是否能被调用
def func():pass
print(callable(func)) #参数是函数名,可调用,返回True
print(callable(123)) #参数是数字,不可调用,返回False
  • dir——可用于查看一个数据类型的内置方法,类似于help,是一种帮助

附:可供参考的有关所有内置函数的文档https://docs.python.org/3/library/functions.html#object

what's the python之内置函数的更多相关文章

  1. Python之内置函数

    内置函数 python里的内置函数.截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是python提供给你直接可以拿来使用的所有函数. 分类学习内置函数: 总共 ...

  2. python之内置函数(一)

    一.内置函数一1.内置函数总览 abs() dict() help() min() setattr()all() dir() hex() next() slice() any() divmod() i ...

  3. python之内置函数(二)与匿名函数、递归函数初识

    一.内置函数(二)1.和数据结构相关(24)列表和元祖(2)list:将一个可迭代对象转化成列表(如果是字典,默认将key作为列表的元素).tuple:将一个可迭代对象转化成元组(如果是字典,默认将k ...

  4. python之内置函数与匿名函数

    一内置函数 # print(abs(-1)) # print(all([1,2,'a',None])) # print(all([])) #bool值为假的情况:None,空,0,False # # ...

  5. python之内置函数,匿名函数

    什么是内置函数? 就是Python给你提供的,拿来直接用的函数,比如print,input等等.其实就是我们在创建.py的时候python解释器所自动生成的内置的函数,就好比我们之前所学的作用空间 内 ...

  6. python之内置函数,匿名函数,递归函数

    一. 内置函函数 什么是内置函数?就是Python给你提供的,拿来直接用的函数,比如print,input等等.截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就 ...

  7. python之内置函数(lambda,sorted,filter,map),递归,二分法

    一.lambda匿名函数 为了解决一些简单需求而设计的一句话函数,lambda表示的是匿名函数,不需要用def来声明,一句话就可以声明出一个函数. 语法: 函数名 = lambda 参数 : 返回值 ...

  8. Python之内置函数一

    一:绝对值,abs i = abs(-123) print(i) # 打印结果 123 二:判断真假,all,与any 对于all # 每个元素都为真,才是True # 假,0,None," ...

  9. python之内置函数:map ,filter ,reduce总结

    map函数: #处理序列中的每个元素,得到的结果是一个'列表',该列表元素个数及位置与原来一样 filter函数: #遍历序列中的每个元素,判断每个元素得到一个布尔值,如果是true,则留下来 peo ...

随机推荐

  1. SSM框架整合搭建教程

    自己配置了一个SSM框架,打算做个小网站,这里把SSM的配置流程详细的写了出来,方便很少接触这个框架的朋友使用,文中各个资源均免费提供! 一. 创建web项目(eclipse) File-->n ...

  2. [JS] ECMAScript 6 - String, Number, Function : compare with c#

    字符串的扩展 正则的扩展 数值的扩展 函数的扩展 字符串的扩展 js 字符的 Unicode 表示法 codePointAt() String.fromCodePoint() 字符串的遍历器接口 at ...

  3. The last packet successfully received from the server was 20,519 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago.

    本地升级了下MySQL的版本,从5.6升为5.7,数据文件直接拷贝的,项目查询数据库报错: Could not retrieve transation read-only status server ...

  4. LeetCode - 386. Lexicographical Numbers

    Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...

  5. C# 读写Excel的一些方法,Aspose.Cells.dll

    需求:现有2个Excel,一个7000,一个20W,7000在20W是完全存在的.现要分离20W的,拆分成19W3和7000. 条件:两个Excel都有“登录名”,然后用“登录名”去关联2个Excel ...

  6. cmus 命令行播放器使用

    安装 sudo eopkg it cmus 启动 cmus 设置输出 :set output_plugin=pulse 导入本地音乐 :add /home/your_username/Music 查看 ...

  7. 利用profiler工具提高NC-Verilog仿真效率

    大家进行芯片验证时,一般都会遇到仿真速度很慢.效率不高的问题.目前发现了一个方法可以debug上述问题.即,利用NC的profiler工具. 关于profiler工具,我把文档<Cadence® ...

  8. python的代码缩进和冒号

    一般语言一样采用{}或者begin...end分隔代码块,而是python中,采用代码缩进和冒号来区分代码之间的层次. 缩进的空白数量是可变的,但是所有代码块语句必须包含相同的缩进空白数量,这个必须严 ...

  9. 20165311学习基础和C语言基础调查

    一.技能学习经验 有什么技能比90%的人更好? 这个问题问的就很emmmm..我觉得自己的推理和逻辑思维能力比较出众,面对新事物的自学速度比较快. 针对技能谈一下成功的经验. 每一项出众的技能都是与平 ...

  10. [No000013F]WPF学习之X名称空间详解

    X名称空间里面的成员(如X:Name,X:Class)都是写给XAML编译器看的.用来引导XAML代码将XAML代码编译为CLR代码. 4.1X名称空间里面到底都有些什么? x名称空间映射的是:htt ...