码迷,mamicode.com
首页 > 编程语言 > 详细

python内置方法181030

时间:2018-10-31 23:22:49      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:最小值   slice   [1]   http   变量   try   https   exe   ash   

Python内置方法

参考官方文档:https://docs.python.org/3/library/functions.html
abs() #取绝对值
all() #如果可迭代的对象所有的元素都为真则返回True,则不能存在0元素
any() #如果可迭代的对象所有的元素有一个为真则返回True
ascii() #将内存的对象变成一个可打印的字符串
bin() #十进制转二进制
bool() #判断真假
bytes() #将字符串转换为二进制
bytearray() #将二进制转换为列表
callable() #判断对象是否可调用
chr() #返回ASCII码的对应表
ord() #返回字符的ASCII码
compile() #将字符串编译成可执行代码
dir() #获取对象的可用方法
divmod() #相除返回余数
enumerate() #为列表加个index
eval() #将字符串变为字典
exec()
filter() #按照函数的规定过滤指定数据
map() #将数据按照给定的方法处理后返回
reduce()
frozenset() #将集合冻结
globals() #返回当前程序的全局变量
locals() #返回当前程序的本地变量
hash() #取hash值
hex() #将一个数字转换为16进制
max() #返回最大值
min() #返回最小值
repr() #用字符串表示对象
reversed() #翻转
round() #保留精度
slice() #切片
sorted() #将字典排序变成列表
sum() #将列表求和
type() #查看数据类型
vars() #返回一个对象的所有的属性名
zip() #拉链,将俩个对象一一对应
__import__() #导入字符串格式的模块名

代码示例

# Author:Li Dongfei
print(all([0,-5,3]))
print(any([0,-5,3]))
print(ascii(["我"]))
print(bin(255))
print(bool([]))
a = bytes("abcdef",encoding="utf-8")
print(a.capitalize(),a)
b = bytearray("abcdef",encoding="utf-8")
b[1] = 100
print(b)
print(callable([]))
print(chr(98))
print(ord('b'))
code = '''
def fib(max):
    n, a, b = 0, 0, 1
    while n < max:
        yield b
        a, b = b, a + b
        n = n + 1
    return "done"
f = fib(10)
while True:
    try:
        x = next(f)
        print('f:', x)
    except StopIteration as e:
        print('Generator return value:', e.value)
        break
'''
py_obj = compile(code,"err.log","exec")
exec(py_obj)
print(divmod(5,3))
res = filter(lambda n:n>5,range(10))
for i in res:
    print(i)
res2 = map(lambda n:n*n,range(10))
for i in res2:
    print(i)
import functools
res3 = functools.reduce( lambda x,y:x*y,range(1,10))  #阶乘
print(res3)
a = frozenset([1,2,3,2,3,1])
print(globals())
print(hash('dongfei'))
hex(254)
print(repr("c"))
print(round(3.1415926,4))
d = range(20)
print(d[slice(2,5)])
c = {6:2,8:0,1:4,-5:6}
print(sorted(c.items()))  #按key排序
print(sorted(c.items(),key=lambda x:x[1]))  #按val排序
d = [1,2,3,4]
e = ['a','b','c','d']
for i in zip(d,e):
    print(i)
__import__('mode_name')

python内置方法181030

标签:最小值   slice   [1]   http   变量   try   https   exe   ash   

原文地址:https://www.cnblogs.com/L-dongf/p/9886318.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!