python3第六天

一、内置函数
1.abs():绝对值
2.all(Iterable): 可迭代对象里面每个元素都为真,函数为真
3.any(Iterable):可迭代对象里面只要有一个元素为真,函数为真
4.ascii(object):把一个对象转换成可打印的字符串的形式
5.bin(x): 十进制转二进制    oct(x)   hex(x)
6.bool():
7.bytearray():
8.bytes():unicode —— encode——utf-8
9.callable():检测一个对象能否被调用
10.chr(): asscii对应的数字转化成字符 (chr(65))           (ord(‘#’))字符转化成数字
11.classmethod():
   staticmethod
   property
 
12.complie():
s='for i in range(10):print(i,x)'
code=compile(s,'','exec')
print(code)
exec(code,{},{'x':1111})
 
13.complex():复数   float int str list tuple  dict set  frozenset   数据类型
14.delattr():反射
hasattr
setattr
 
15.dict()
16.dir():  print(dir(sys)):查看一个名字后面的属性
17.divmod():  商跟余数  print(divmod(10,3))   结果:(3,1)
18.enumerate():
  I=[‘a’,’b',’c’]
  res=enumerate(I)
for i in res:
(0,’a')
(1,’b')
(2,’c')
19.eval():指定作用域 
20.exec():
21.filter():    
filter(lambda name:name.endswith(’SB’),names)
 
22.float()
23.format()
24.frozenset(): 集合用法同set ,不同点:不可变集合
25.getattr()
26.globals():查看全局作用域    locals():查看局部作用域
27.hasattr():
28.hash():检验一个字符串   hash(‘bcklasv')
29.help():查看帮助信息
30.hex():
31.id(x):反应出来x在内存里面的位置 只是python解释器实现的功能  不是内存地址
32.input():
33.int()
34.isinstance(x,int):x是不是int的一个实例
35.issubclass()
36.iter():迭代器
37.len():长度
38.list():列表
39.locals()
40.map():映射
name=['da','dc','bs']
print(list(map(lambda x:x+'hs',name )))
 
 
41.max():最大值   元组可以一一对应比大小
salaries={
    'egon':3000,
    'alex':100000000,
    'wupeiqi':10000,
    'yuanhao':2000
}
print(list(zip(salaries.values(),salaries.keys())))
[(3000, 'egon'), (100000000, 'alex'), (10000, 'wupeiqi'), (2000, 'yuanhao’)]
print(max(zip(salaries.values(),salaries.keys())))
print(max(salaries,key=lambda name:salaries[name]))
(100000000, 'alex')
alex
key是函数的内存地址
 
42.memoryview():
43.min():最小值
44.next():
45.object():
46.oct()
47.open():
48.ord()
49.pow():    print(pow(x,y,z)) x的y次方再取模
50.print()
51.property():
52.range():
 
53.repr():  把对象转化成字符串形式   与str相似
54.reversed(): 反转 一下产生一个新的列表
55.round():   round(3.478,2)
 
56.set():
57.serattr():
58.slice():切片  
slice(0,4,2) 【 起始 结束  步长   】相当于 I[0:4:2]
59.sorted():排序  
sorted(I,reverse=True 从小到大)
60.staticmethod():
61.str()
62.sum():
63.super():
64.tuple():
65.type():
66.vars():  
print(vars(m1)==m1.__dict__)   True
67.zip():拉链函数
s=“hello”
I=[1,2,3,4,5]
res=zip(s,l)
print(list(zip(s,l)))
[(‘h’,1),(‘e’,2),(‘l’,3),(‘l’,4),(‘o’,5)]
 
68__import__():  
 __import__(’sys’)==sys
 
二、正则表达式
w:字母数字下划线
W:非字母数字下划线
import re
print(re.findall('w','hello egon 123')) 
['h', 'e', 'l', 'l', 'o', 'e', 'g', 'o', 'n', '1', '2', '3']
print(re.findall('W','hello egon 123')) 
[' ', ' '] 
s:匹配任意空白字符     :制表符 :换行 :回到行首 f:
S:匹配任意非空白字符
print(re.findall('s','hello  egon  123')) 
[' ', ' ', ' ', ' ']
print(re.findall('S','hello  egon  123')) 
['h', 'e', 'l', 'l', 'o', 'e', 'g', 'o', 'n', '1', '2', '3']
d:匹配任意数字
D:匹配任意非数字
print(re.findall('d','hello egon 123')) 
['1', '2', '3']
print(re.findall('D','hello egon 123')) 
['h', 'e', 'l', 'l', 'o', ' ', 'e', 'g', 'o', 'n', ' ']
A:从头开始匹配相当于^
print(re.findall('Ahe','hello egon 123')) 
print(re.findall(‘^he','hello egon 123')) 
['he’] 
:从尾部开始
相当于$
print(re.findall(‘123','hello egon 123')) 
print(re.findall(‘123$','hello egon 123')) 
[‘123']
 
. [ ] [^]
. 代表一个字符(换行符排除在外)
print(re.findall('a.c’,’a a1c a*c a2c abc a c aaaaaac')) 
[‘a1c’ ‘a*c’ ‘a2c’ ‘abc’ ‘a c’ ‘aac’]
print(re.findall('a.c’,’a a1c a*c a2c abc a c,re.DOTALL’))
print(re.findall('a.c’,’a a1c a*c a2c abc a c,re.S’))
[‘a1c’ ‘a*c’ ‘a2c’ ‘abc’ ‘a c’]
[ ]内部可以有多个字符,但是本身只配多个字符中的一个
print(re.findall(‘a[1 23]c’,’a a1c a*c a2c a c a c,re.S’))
[‘a1c’  ‘a2c’ ‘a c’]
[^]代表不含中括号里面的字符
print(re.findall(‘a[a-zA-Z]c’,’aac abc aAc a12c a1c a*c a2c a c a c’))
print(re.findall(‘a[^a-zA-Z]c’,’aac abc aAc a12c a1c a*c a2c a c a c’))
 [‘aac’,’abc’,’aAc’]
[‘a1c’,’a*c’,’a2c’,’a c’,’a c’]
:转义符号
print(re.findall(‘a\c’,’ac abc')) 错误
print(re.findall(‘a\\c’,’ac abc’))
print(re.findall(r‘a\c’,’ac abc'))
[‘a\c’]
 
? * + {}
?左边那一个字符有0个或者1个
print(re.findall('ab?','a')) 
['a']
print(re.findall('ab?','abbb')) 
['ab']
.*默认为贪婪匹配
print(re.findall('a.*b','a1b22222222b')) #['a1b22222222b']
.*?为非贪婪匹配:推荐使用
print(re.findall('a.*?b','a1b22222222b')) #['a1b']
():分组
print(re.findall('ab+','ababab123')) #['ab', 'ab', 'ab']
print(re.findall('(ab)+123','ababab123')) #['ab'],匹配到末尾的ab123中的ab
print(re.findall('(?:ab)+123','ababab123')) #findall的结果不是匹配的全部内容,而是组内的内容,?:可以让结果为匹配的全部内容
|
print(re.findall('compan(?:y|ies)','Too many companies have gone bankrupt, and the next one is my company'))
 
三、time模块
时间戳:

import time
print(time.time()) 
print(time.strftime("%Y-%m-%d %X"))

print(time.localtime()) 

print(time.gmtime())

 
四、random模块
import random

print(random.random())

#(0,1)----float 大于0且小于1之间的小数

print(random.randint(1,3))

#[1,3] 大于等于1且小于等于3之间的整数


print(random.randrange(1,3))

#[1,3) 大于等于1且小于3之间的整数

print(random.choice([1,'23',[4,5]]))

#1或者23或者[4,5]

print(random.sample([1,'23',[4,5]],2))

#列表元素任意2个组合

print(random.uniform(1,3))

#大于1小于3的小数,如1.927109612082716 

五、os

os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径
os.chdir("dirname") 改变当前脚本工作目录;相当于shell下cd
os.curdir 返回当前目录: ('.')
os.pardir 获取当前目录的父目录字符串名:('..')
os.makedirs('dirname1/dirname2') 可生成多层递归目录
os.removedirs('dirname1') 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
os.mkdir('dirname') 生成单级目录;相当于shell中mkdir dirname
os.rmdir('dirname') 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
os.listdir('dirname') 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
os.remove() 删除一个文件
os.rename("oldname","newname") 重命名文件/目录
os.stat('path/filename') 获取文件/目录信息
os.sep 输出操作系统特定的路径分隔符,win下为"\",Linux下为"/"
os.linesep 输出当前平台使用的行终止符,win下为" ",Linux下为" "
os.pathsep 输出用于分割文件路径的字符串 win下为;,Linux下为:
os.name 输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
os.system("bash command") 运行shell命令,直接显示
os.environ 获取系统环境变量
os.path.abspath(path) 返回path规范化的绝对路径
os.path.split(path) 将path分割成目录和文件名二元组返回
os.path.dirname(path) 返回path的目录。其实就是os.path.split(path)的第一个元素
os.path.basename(path) 返回path最后的文件名。如何path以/或结尾,那么就会返回空值。即os.path.split(path)的第二个元素
os.path.exists(path) 如果path存在,返回True;如果path不存在,返回False
os.path.isabs(path) 如果path是绝对路径,返回True
os.path.isfile(path) 如果path是一个存在的文件,返回True。否则返回False
os.path.isdir(path) 如果path是一个存在的目录,则返回True。否则返回False
os.path.join(path1[, path2[, ...]]) 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
os.path.getatime(path) 返回path所指向的文件或者目录的最后存取时间
os.path.getmtime(path) 返回path所指向的文件或者目录的最后修改时间
os.path.getsize(path) 返回path的大小

 
六、sys
sys.argv 命令行参数List,第一个元素是程序本身路径
import sys
print (sys.argv[0])
print (sys.argv[1])
python3 sys.py glj

sys.py

 glj
sys.exit(n) 退出程序,正常退出时exit(0)
sys.version 获取Python解释程序的版本信息
sys.maxint 最大的Int值
 
sys.path :
获取指定模块搜索路径的字符串集合,可以将写好的模块放在得到的某个路径下,就可以在程序中import时正确找到。
返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
import sys
print(sys.path)

['/Users/gelujing/Desktop/project/Day6', '/Users/gelujing/Desktop/project', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages']
 
sys.platform 返回操作系统平台名称
 
原文地址:https://www.cnblogs.com/jnbb/p/7344935.html