首页 > 编程语言 > 详细

python3 内置函数

时间:2019-04-28 11:23:24      阅读:110      评论:0      收藏:0      [点我收藏+]
‘‘‘iter()和next()‘‘‘
# lst = [1, 2, 3]
# it = iter(lst)
# print(it.__next__())
# print(it.__next__())
# print(next(it))

‘‘‘eval()‘‘‘
# print(eval("1+1+9+18")) # eval 执行代码. 有返回值
# print(eval("[1,2,2,3,4,5]"))

‘‘‘exec()‘‘‘
# exec(input(‘请输入一段你想执行的代码:‘)) #a = 30
# print(a)

‘‘‘compile()‘‘‘
# exec(compile("for i in range(10):print(i)", "", "exec"))

‘‘‘hash()‘‘‘
# print(hash([1, 2, 3])) # 列表不可哈希
# print(hash(123))
# print(hash("中国")) #1854419051

‘‘‘__import__‘‘‘
# s = "os"
# __import__(s) #相当于import os

‘‘‘help()‘‘‘
# print(help(str))

‘‘‘callable()‘‘‘
# def func(a):
# if callable(a):
# a()
# else:
# print(a)
#
# def ha():
# print("我是哈哈哈")
#
# func(ha)
# func("hello")

‘‘‘bin() oct() hex()‘‘‘
# print(bin(20)) #0b10100
# print(oct(20)) #0o24
# print(hex(20)) #0x14

‘‘‘divmod()‘‘‘
# print(divmod(10, 3)) #// % 商和余数

‘‘‘pow()‘‘‘
# print(pow(2, 10)) #次幂

‘‘‘sum()‘‘‘
# print(sum([12, 2, 1, 2]))

‘‘‘reversed()‘‘‘
# lst = [2, 3, 4, 5, 6]
# r = reversed(lst) #<list_reverseiterator object at 0x00C072F0>
# for i in r:
# print(i)

‘‘‘slice()‘‘‘
# s = slice(1, 5, 2)
# ss = "hello,I‘m good"
# print(ss[1:5:2])

‘‘‘center()‘‘‘
# s = "周杰伦"
# print(s.center(20, "*"))

‘‘‘format()‘‘‘
# print(format("周杰伦", "*>20"))
# print(format("周杰伦", "*<20"))
# print(format("周杰伦", "*^20"))
# print(format(18, "010b"))
# print(bin(18))
#
# s = "192.168.1.1"
# lst = s.split(".")
# for item in lst:
# print(format(int(item), "08b"))
#
# print(format(1.23956789, ".2f"))

‘‘‘ord() chr()‘‘‘
# print(ord(‘中‘)) # unicode
# print(chr(20013))
# for i in range(65536):
# print(chr(i), end=str(i)+" ")

‘‘‘repr() \\转义字符‘‘‘
# print("你好\啊") # python中的字符串 str()
# print(repr("你好\啊")) # 官方提供的字符串表示形式
# print(r"我的天那\ 怎么不好使了") # 字符串原样输出

‘‘‘frozenset()‘‘‘
# s = {1, 2, 3}
# print(hash(s)) #TypeError: unhashable type: ‘set‘
# fs = frozenset(s)
# print(hash(fs))

‘‘‘enumerate()‘‘‘
# lst = [11, 22, 33, 44]
# for i, item in enumerate(lst):
# print(i, ":", item)

‘‘‘all() 和 any()‘‘‘
# print(all([18, False, 12])) # and => bool
# print(any([19, 0, 22])) # or => bool

‘‘‘zip() 水桶效应‘‘‘
# lst1 = ["赵本山", "范伟", "小沈阳"]
# lst2 = ("乡村爱情", "卖乖", "不差钱")
# lst3 = ["白云", "黑土"]
#
# z = zip(lst1, lst2, lst3) # 水桶效应
# for item in z:
# print(item)

‘‘‘sorted()‘‘‘
# lst = [22, 1, 3, 5, 6, 7]
# print(sorted(lst))

‘‘‘
sort(iterable, key)
首先,打开这个可迭代对象. 然后获取到每一项数据. 把每一项数据传递给函数.
根据函数返回的数据进行排序
‘‘‘
# lst = ["高进", "波多野结衣", "苍老师", "仓木麻衣", "红狗"]
# print(sorted(lst, key=lambda s: len(s)))

‘‘‘sorted(iterable, key, reverse)‘‘‘
# lst = [
# {"id": 1, "name": ‘alex‘, "age": 18},
# {"id": 2, "name": ‘wusir‘, "age": 16},
# {"id": 3, "name": ‘taibai‘, "age": 17}
# ]
# print(sorted(lst, key=lambda d: d[‘age‘], reverse=True))

‘‘‘
filter(function, iterable) 筛选出来. 大于20的数据
返回迭代器, 把可迭代对象中的每一项数据交给前面的函数. 由函数决定该数据是否保留
‘‘‘
# lst = [18, 22, 66, 35, 1, 48]
# f = filter(lambda n: n > 20, lst)
# for item in f:
# print(item)

‘‘‘filter(function, iterable) 保留成年人 age >= 18‘‘‘
# lst = [
# {"id": 1, "name": ‘alex‘, "age": 18},
# {"id": 2, "name": ‘wusir‘, "age": 16},
# {"id": 3, "name": ‘taibai‘, "age": 17}
# ]
# f = filter(lambda x: x[‘age‘] >= 18, lst)
# print(list(f))

‘‘‘map(func, iter1)‘‘‘
lst = [2, 5, 3, 2, 4]
m = map(lambda x: x*x, lst)
for item in m:
print(item)




 

python3 内置函数

原文:https://www.cnblogs.com/lilyxiaoyy/p/10782563.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!