1,内置函数python
截止到python版本3.6.2,如今python一共为咱们提供了68个内置函数。它们就是python提供给你直接能够拿来使用的全部函数shell
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() |
2,做用域相关缓存
2.1,globals()——获取全局变量的字典安全
2.2,locals()——获取所在域的变量的字典函数
3,帮助ui
3.1,dir()——查看其对象的全部方法编码
3.2,callable()——看这个变量是否是可调用spa
callable(参数),看这个变量是否是可调用。若是参数是一个函数名,就会返回Truecode
print(callable(print))
3.3,help()——查看帮助信息 orm
在控制台执行help()进入帮助模式。能够随意输入变量或者变量的类型。输入q退出,或者直接执行help(参数),查看和参数有关的操做
help(str)
4,模块相关
4.1,__import__导入一个模块
import time #导入模块
os = __import__('os')
print(os.path.abspath('.'))
5,文件操做相关
5.1,open()
open() 打开一个文件,返回一个文件操做符(文件句柄)
操做文件的模式有r,w,a,r+,w+,a+ 共6种,每一种方式均可以用二进制的形式操做(rb,wb,ab,rb+,wb+,ab+)
能够用encoding指定编码.
5.2,判断文件是否可读,可写
f = open('file')
print(f.writable())
print(f.readable())
6,内存相关
6.1,id(参数)—— 返回一个变量的内存地址
6.2,hash(参数) ——返回一个可hash变量的哈希值,不可hash的变量被hash以后会报错。
print(hash(123))
print(hash('fskjs'))
print(hash('fskjs'))
print(hash([])) #报错
字典寻址方式(字典寻址快的缘由):
对键进行hash将获得的值做为地址,将键值存入该内存地址
7,输入输出
7.1,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: 当即把内容输出到流文件,不做缓存
"""
f = open('file','w')
print('aaa',file=f)
f.close
#打印进度条
import time
for i in range(0,101,2):
time.sleep(0.1)
char_num = i//2 #打印多少个'*' ,单'/'结果是float型,双'//'是整除,结果是int型
per_str = '\r%s%% : %s\n' % (i, '*' * char_num) if i == 100 else '\r%s%% : %s'%(i,'*'*char_num)
print(per_str,end='', flush=True) #'%%' 是打印'%'
# \r 能够把光标移动到行首但不换行
7.2,input()
s = input("请输入内容 : ") #输入的内容赋值给s变量
print(s) #输入什么打印什么。数据类型是str
8,字符串类型代码的执行
8.1,exec()和eval()均可以执行 字符串类型的代码;compile 将字符串类型的代码编译
exec('print(123)')
eval('print(123)')
print(eval('1+2+3'))
print(exec('1+2+3'))
code = '''for i in range(3): print(i*'*') ''' exec(code)
8.2,compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1)
将字符串类型的代码编译。代码对象可以经过exec语句来执行或者eval()进行求值。一次编译,能够一直使用
参数说明:
1. 参数source:字符串或者AST(Abstract Syntax Trees)对象。即须要动态执行的代码段。
2. 参数 filename:代码文件名称,若是不是从文件读取代码则传递一些可辨认的值。当传入了source参数时,filename参数传入空字符便可。
3. 参数model:指定编译代码的种类,能够指定为 ‘exec’,’eval’,’single’。当source中包含流程语句时,model应指定为‘exec’;当source中只包含一个简单的求值表达式,model应指定为‘eval’;当source中包含了交互式命令语句,model应指定为'single'。
#流程语句使用exec
code1 = 'for i in range(0,10): print (i)'
compile1 = compile(code1,'','exec')
exec (compile1)
#简单求值表达式用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变量有值
9,complex 复数
数学中复数的虚部用 i 表示,在python中是用 j 表示
浮点数:有限循环小数,无限循环小数
小数:有限循环小数,无限循环小数,无限不循环小数
10,进制转换
print(bin(10)) # 0b1010 二进制
print(oct(10)) # 0o12 八进制
print(hex(10)) # 0xa 十六进制
11,数学运算
11.1,
print(abs(-5))#求绝对值
print(divmod(7,2)) #div除法 mod取余 #求商和余数
print(round(3.14159,2))#精确值
print(pow(2,3))#求幂运算
print(pow(2,3,3))#幂运算以后再取余
11.2,sum(iterable,start)
#sum(iterable,start)
#必须是可迭代的才能求和
#start#从几开始加
ret = sum([1,2,3,4,5],10)
print(ret)
11.3,min(iterable,key,defult)
min(*args,key,defult)
print(min(1,2,3,4))
print(min([1,2,3,4]))
print(min(1,2,3,-4,key = abs))#都换成绝对值后再求最小值