day15.Python内置函数

作用域相关

locals() -- 获取执行本方法所在命名空间的局部变量的字典

globals() --  获取全局变量的字典

print(locals())
print(globals())
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000001A78CA58748>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'E:/python学习/day15/内置函数.py', '__cached__': None}
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000001A78CA58748>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'E:/python学习/day15/内置函数.py', '__cached__': None}

迭代器相关的

range

next  迭代器.__next__  == next(迭代器)

iter   迭代对象.__next__  == iter(可迭代对象) 

其他

dir() 常用查询方法,查看变量拥有的方法

检测是否是一个函数
print(callable(print))
a = 1
print(callable(a))
True
False

查看帮助文档
help(str) 查看具体用法

导入模块:
import ()

查看内存地址:
id()

hash()将可hash的值转换成一串数字串
数字,字符串

输入输出:
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: 立即把内容输出到流文件,不作缓存
    """
例:
with open(
'file',mode='w') as f: print(1,2,3,4,5,6,end='',sep='#',file=f) # end指定换行符,sep指定多个元素的分隔符,file将输出内容写入文件

 字符串操作相关

exec , eval 都可以执行字符串类型的代码

eval() 需要在明确字符串的情况下执行,慎用

eval('print("hello")')
exec('print("world")')
hello
world

ret1 = eval('1+2+3')
ret2 = exec('1+2+3')
print(ret1,ret2)
6 None

数学运算

abs()         求绝对值

divmod()    除余,可做分页功能
divmod(7,3)  (2,1)  得2余1

round(3.1415,3)  3.131 小数取位数

pow(2,3)  8 两个数做幂运算
pow(3,2,5) 4   先做幂运算,再做取余

sum() 求和

min()计算最小值

max()计算最大值 

 序列:

reversed:
获取一个反转的生成器:

l1 = [1,2,3,4,5] l2 = reversed(l1) l2.__iter__() for i in l2: print(i)

slice:
切片
l1 = [1,2,3,4,5]
l3 = slice(1,4,2)
print(l1[l3])

和字符串相关:

bytes

编码:
name = '小明'
print(name.encode('utf-8'))
print(bytes(name,encoding='utf-8'))
b'\xe5\xb0\x8f\xe6\x98\x8e'
b'\xe5\xb0\x8f\xe6\x98\x8e'
解码:
name = '小明'
print(name.encode('utf-8').decode('utf-8'))
print(bytes(name,encoding='utf-8').decode('utf-8'))
小明
小明

ord: 字符按照unicode转数字

 chr: 数字按照unicode转字符

print(ord(''))
print(ord(''))
print(chr(21835))
print(chr(37202))
21834
26149
啋
酒

repr:让变量或字符类型原形毕露

name = '你好'
print(name)
print(repr(name))
你好
'你好'

 几个重要内置函数:

zip:拉链方法,

返回一个迭代器,将各种可迭代对象拉在一起

l1 = [1,2,3,4,5]
l2 = ['a','b','c','d','e','f']
l3 = ('','','','')
l4 = {'k1':'v1','k2':'v2','k3':'v3','k4':'v4'}

print(zip(l1,l2,l3,l4))
for i in zip(l1,l2,l3,l4):
    print(i)
<zip object at 0x0000020AE8396D08> (1, 'a', '', 'k1') (2, 'b', '', 'k2') (3, 'c', '', 'k3') (4, 'd', '', 'k4')

filter: “过滤器”

可以过滤掉不符合要求的可迭代对象,返回一个迭代器

filter() 函数 接收两个参数,第一个参数必须是函数的名字

将每一个可迭代对象放到函数中判断True or False 然后过滤

过滤偶数:
def
is_odd(x): return x % 2 == 1 ret = filter(is_odd,range(10)) print(ret) for i in ret: print(i)

  <filter object at 0x000001F1E06E2048>
  1
  3
  5
  7
  9   

filter 应用去掉空字符串或者去掉空内容:

l1 = [1,'helo'," ",None,[ ],6,7,'world']
def func(s):
    return s and str(s).strip()

ret = filter(func,l1)
for i in ret:
    print(i)
1
helo
6
7
world

过滤100以内平方根是整数的数字:

def func(num):
    res = num ** 0.5
    return res % 1 == 0
ret = filter(func,range(1,101))
for i in ret:
    print(i)
1
4
9
16
25
36
49
64
81
100

map:将可迭代对象进行处理

将可迭代对象进行函数处理

ret = map(abs,[2,-4,6,-8])
print(ret)
for i in ret:
    print(i)
<map object at 0x00000242F31FBEF0> 2 4 6 8

sorted:

先看一下sort:
l = [1,-2,3,-4,5,-6]
l.sort()
print(l)
l.sort(key = abs)
print(l)
[-6, -4, -2, 1, 3, 5]
[1, -2, 3, -4, 5, -6]

再看一下sorted:

sorted 也会排序

但是 sorted会产生新的列表,占用内存空间,慎用

l = [1,-2,3,-4,5,-6]
print(sorted(l))
print(l)
[
-6, -4, -2, 1, 3, 5] [1, -2, 3, -4, 5, -6]

将列表元素按照元素长度排序:

此处利用sort 和 sorted 的key值

lst = ['abc','ab','abcde','a']
lst_new = sorted(lst,key=len)
print(lst_new)
[
'a', 'ab', 'abc', 'abcde']

 

 

 

posted @ 2019-03-06 16:49  恶灵酒  阅读(258)  评论(0编辑  收藏  举报