2017/9/11——何某某更博,花时间整理了所有的Python内置方法的用法,便于日后复习

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2017/9/11——何某某更博,花时间整理了所有的Python内置方法的用法,便于日后复习相关的知识,希望对你有一定的参考价值。

1、这里是所有的内置方法的使用方法

# -*- coding:utf-8 -*-
# Author : 何子辰

# 所有的内置方法总结

print(1.abs.center(50,*))
# abs 绝对值
a = abs(-5)
print(a)

print(2.all.center(50,*))
# all
# Return True if all elements of the
# iterable are true(or if the iterable
# is empty).
# 非0即真
print(all([0,-5,3]))

print(3.any.center(50,*))
# any
# Return True if any element of the iterable
# is true. if the iterable is empty.Return
# False
print(any([1,-2,9]))
print(any([]))

print(4.ascii.center(50,*))
# ascii
# return a string containing a printable of
# an object
print(ascii([1,2,"你好"]))
print(type(ascii([1,2,"你好"])))
# <class ‘str‘>
a = ascii([1,2])
print([a])
# [‘[1, 2]‘]

print(5.bin.center(50,*))
# bin
# 十进制转换二进制
print(bin(255))

print(5.bool.center(50,*))
# bool
# Ture or false
print(bool(0))
print(bool(1))
print(bool([]))

print(6.bytearray.center(50,*))
# bytearray
# Return a byte array
# bytearray 可修改二进制字节格式
a = bytes("abcde",encoding="utf-8")
print(a)
# b‘abcde‘
print(a.capitalize(),a)
# b‘Abcde‘ b‘abcde‘ 字符串不能修改,二进制字节更不能修改
b = bytearray("abcde",encoding="utf-8")
print(b[0])  # 97 a的ascii码
print(b[1])
b[1] = 100
print(b) # bytearray(b‘adcde‘) b变成了d

print(7.callable.center(50,*))
#  Judge an object if it can be callable
print(callable([])) # 列表不能调用 *列表不能加括号
def sayhi():
    pass
print(callable(sayhi)) # 函数可以调用

print(8.chr & ord.center(50,*))
# 返回ascii码的对应表
print(chr(89))  #Y
print(chr(99))  #c
# 返回字符的ascii码
print(ord(b)) #98

print(9.classmethod.center(50,*))
# 类方法

print(10.compile.center(50,*))
# 底层代码编译过程
# 将字符串编译成可执行代码
# code = "for i in range(10): print(i)"
# compile(code,‘‘,‘exec‘)
# >>> code = "for i in range(10):print(i)"
# >>> code
# ‘for i in range(10):print(i)‘
# >>>
# >>> compile(code,‘‘,‘exec‘)
# <code object <module> at 0x00000000032E9F60, file "", line 1>
# >>> c = compile(code,‘‘,‘exec‘) # 执行器 exec
# >>> exec(c)
# 0
# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9
# >>>

# >>> code = "1+3/6"
# >>> c = compile(code,‘‘,‘eval‘)  # 执行器eval
# >>> eval(c)
# 1.5

# 等价于import一个模块 实现了动态导入
code = ‘‘‘
def Fibonaci(max):
    n,a,b = 0,0,1
    while (n < max):
        print(b)
        a,b = b,a + b
        # 不相当于 a = b; b = a+b
        # 相当于 t = (b,a+b)
              # a = t[0]
              # b = t[1]
        n = n+1
    return ‘done‘

Fibonaci(20)
‘‘‘
py_obj = compile(code,"err.log","exec") # 编译
exec(py_obj)

# exec(code)  # 妈蛋的直接用exec就可以执行....

print(11.dir.center(50,*))
# dir
# 查看一个object有哪些方法
b = {}
print(dir(b))

print(12.divmod.center(50,*))
# 相除并返回余数
# >>> divmod(5,2)
# (2, 1)
# >>> divmod(123,3)
# (41, 0)
# >>>

print(13.eval.center(50,*))
# 简单数据运算
x = 1
print(eval(x+1))

print(14.exec.center(50,*))
code = ‘‘‘
def Fibonaci(max):
    n,a,b = 0,0,1
    while (n < max):
        print(b)
        a,b = b,a + b
        # 不相当于 a = b; b = a+b
        # 相当于 t = (b,a+b)
              # a = t[0]
              # b = t[1]
        n = n+1
    return ‘done‘
‘‘‘
exec(code)



# 首先关于匿名函数
def sayhi(n):
    print(n)
sayhi(3)
# 转成匿名函数
# 匿名函数 处理简单语句
# 传参数
(lambda n:print(n))(5)
calc = lambda n:print(n)
calc(5)
# 三元运算
calc = lambda n:3 if n<4 else n
print(calc(2))

print(14.filter.center(50,*))
# filter
# Function: 一组数据中过滤出用户想要的
res = filter(lambda n:n>5,range(10))
# 此时变成迭代器,需要循环打印
for i in res:
    print(i)

print(15.map.center(50,*))
# map
# map对传入的每一个值,按照func的形式处理
res1 = map(lambda n:n*n,range(10))
# [i*2 for i in range(10)]
for i in res1:
    print(i)
res2 = [ lambda i2:i2*2 for i2 in range(10) ]
for i in res2:
    print(i)

import functools  # 2.7还是内置方法
print(16.reduce.center(50,*))
# reduce
res = functools.reduce(lambda x,y:x+y,range(10))
# 累加器,从1加到10
print(res)
res2 = functools.reduce(lambda x,y:x*y,range(1,10))
print(res2) #362880 累乘

print(17.frozenset.center(50,*))
# frozenset
# 集合冻结
# 集合不可变
a = frozenset([1,4,333,22,112,345,551,2,34])

print(18.globles.center(50,*))
print(globals())
# 打印的是当前所有程序的keyvalue格式

print(19.hash.center(50,*))
# hash
# 散列转为有序 1 2 3 4 5
# 固定的映射关系
print(hash("哈哈哈"))
#548739204797215059 固定对应关系

print(20.hex.center(50,*))
# 转16进制
print(hex(255))
#0xff

print(21.local.center(50,*))
# local
# 局部变量
#
def test():
    local_var=333
    print(locals())
    print(globals())
    print(globals().get(local_var))
test()
print(globals())
print(globals().get(local_var))

print(22.oct.center(50,*))
# 转8进制
print(oct(1),oct(19),oct(8))
#0o1 0o23 0o10

print(23.pow.center(50,*))
# pow(x,y) x**y x的y
#
print(pow(3,5))
#243

print(24.round.center(50,*))
# round(float,N) 浮点数保留N位小数点
print(round(1.3342,3))
# 1.334

print(25.slice.center(50,*))
d = range(20)
slice(1,6,None)
k = d[slice(1,6,None)]
for i in k:
    print(i)
# Output        1
#               2
#               3
#               4
#               5

print(26.sorted.center(50,*))
a = {1:alex,2:June,3:Allen,7:Cook,10:alex,
     17:Yunny}
print(a)
# {1: ‘alex‘, 2: ‘June‘, 3: ‘Allen‘, 17: ‘Yunny‘, 7: ‘Cook‘, 10: ‘alex‘}
# 字典的无序性
# 给字典排序
print(sorted(a))
# out:[1, 2, 3, 7, 10, 17]发现排的是key值
# 列表是有序的
# 用items 将其key值对应的值取出
print(sorted(a.items()))
# [(1, ‘alex‘), (2, ‘June‘), (3, ‘Allen‘),
#  (7, ‘Cook‘), (10, ‘alex‘), (17, ‘Yunny‘)]

# 按value来排序
b = {1:12,2:50,3:47,7:58,10:96,17:99}
print(sorted(b.items(),key = lambda x:x[1]))
#  output[(1, 12), (3, 47), (2, 50), (7, 58),
# (10, 96), (17, 99)]

print(27.vars.center(50,*))
# 返回对象所有的属性名

print(28.zip.center(50,*))
a = [1,2,3,4]
b = [a,b,c,d]
# 列表a 和 b组合
zip(a,b)
print(zip(a,b))
# <zip object at 0x0000000002E9B8C8> 迭代器!
for i in zip(a,b):
    print(i)
#     (1, ‘a‘)
# (2, ‘b‘)
# (3, ‘c‘)
# (4, ‘d‘)

# a多 b少
a = [1,2,3,4,5,6]
b = [a,b,c,d]
# 列表a 和 b组合
zip(a,b)
print(zip(a,b))
# <zip object at 0x0000000002E9B8C8> 迭代器!
for i in zip(a,b):
    print(i)
# (1, ‘a‘)
# (2, ‘b‘)
# (3, ‘c‘)
# (4, ‘d‘)

print(29.import.center(50,*))
# 只知道字符串的导入
__import__(time)

2.上述程序的执行结果

F:\Python3.4\python.exe F:/PyCharm5/Code/day21/内置函数.py
**********************1.abs***********************
5
**********************2.all***********************
False
**********************3.any***********************
True
False
*********************4.ascii**********************
[1, 2, \u4f60\u597d]
<class str>
[[1, 2]]
**********************5.bin***********************
0b11111111
**********************5.bool**********************
False
True
False
*******************6.bytearray********************
babcde
bAbcde babcde
97
98
bytearray(badcde)
********************7.callable********************
False
True
*******************8.chr & ord********************
Y
c
98
******************9.classmethod*******************
********************10.compile********************
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
**********************11.dir**********************
[__class__, __contains__, __delattr__, __delitem__, __dir__, __doc__, __eq__, __format__, __ge__, __getattribute__, __getitem__, __gt__, __hash__, __init__, __iter__, __le__, __len__, __lt__, __ne__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __setitem__, __sizeof__, __str__, __subclasshook__, clear, copy, fromkeys, get, items, keys, pop, popitem, setdefault, update, values]
********************12.divmod*********************
*********************13.eval**********************
2
*********************14.exec**********************
3
5
5
3
********************14.filter*********************
6
7
8
9
**********************15.map**********************
0
1
4
9
16
25
36
49
64
81
<function <listcomp>.<lambda> at 0x00000000035ECF28>
<function <listcomp>.<lambda> at 0x000000000360E048>
<function <listcomp>.<lambda> at 0x000000000360E0D0>
<function <listcomp>.<lambda> at 0x000000000360E158>
<function <listcomp>.<lambda> at 0x000000000360E1E0>
<function <listcomp>.<lambda> at 0x000000000360E268>
<function <listcomp>.<lambda> at 0x000000000360E2F0>
<function <listcomp>.<lambda> at 0x000000000360E378>
<function <listcomp>.<lambda> at 0x000000000360E400>
<function <listcomp>.<lambda> at 0x000000000360E488>
********************16.reduce*********************
45
362880
*******************17.frozenset*******************
********************18.globles********************
{res: 45, __cached__: None, __package__: None, __builtins__: <module builtins (built-in)>, a: frozenset({1, 2, 34, 4, 551, 333, 112, 22, 345}), code: "\ndef Fibonaci(max):\n    n,a,b = 0,0,1\n    while (n < max):\n        print(b)\n        a,b = b,a + b\n        # 不相当于 a = b; b = a+b\n        # 相当于 t = (b,a+b)\n              # a = t[0]\n              # b = t[1]\n        n = n+1\n    return ‘done‘\n", __doc__: None, __spec__: None, res1: <map object at 0x00000000035FC2E8>, x: 1, __file__: F:/PyCharm5/Code/day21/内置函数.py, i: <function <listcomp>.<lambda> at 0x000000000360E488>, __name__: __main__, sayhi: <function sayhi at 0x00000000035ECC80>, functools: <module functools from F:\\Python3.4\\lib\\functools.py>, calc: <function <lambda> at 0x00000000035ECD90>, __loader__: <_frozen_importlib.SourceFileLoader object at 0x0000000003595048>, res2: 362880, b: {}, py_obj: <code object <module> at 0x00000000035F1660, file "err.log", line 2>, Fibonaci: <function Fibonaci at 0x00000000035ECD08>}
*********************19.hash**********************
-8893542816850288741
**********************20.hex**********************
0xff
*********************21.local*********************
{local_var: 333}
{__loader__: <_frozen_importlib.SourceFileLoader object at 0x0000000003595048>, __cached__: None, a: frozenset({1, 2, 34, 4, 551, 333, 112, 22, 345}), res1: <map object at 0x00000000035FC2E8>, __spec__: None, __file__: F:/PyCharm5/Code/day21/内置函数.py, __package__: None, __doc__: None, res2: 362880, py_obj: <code object <module> at 0x00000000035F1660, file "err.log", line 2>, test: <function test at 0x00000000035ECF28>, Fibonaci: <function Fibonaci at 0x00000000035ECD08>, res: 45, __builtins__: <module builtins (built-in)>, i: <function <listcomp>.<lambda> at 0x000000000360E488>, code: "\ndef Fibonaci(max):\n    n,a,b = 0,0,1\n    while (n < max):\n        print(b)\n        a,b = b,a + b\n        # 不相当于 a = b; b = a+b\n        # 相当于 t = (b,a+b)\n              # a = t[0]\n              # b = t[1]\n        n = n+1\n    return ‘done‘\n", x: 1, __name__: __main__, sayhi: <function sayhi at 0x00000000035ECC80>, functools: <module functools from F:\\Python3.4\\lib\\functools.py>, calc: <function <lambda> at 0x00000000035ECD90>, b: {}}
None
{__loader__: <_frozen_importlib.SourceFileLoader object at 0x0000000003595048>, __cached__: None, a: frozenset({1, 2, 34, 4, 551, 333, 112, 22, 345}), res1: <map object at 0x00000000035FC2E8>, __spec__: None, __file__: F:/PyCharm5/Code/day21/内置函数.py, __package__: None, __doc__: None, res2: 362880, py_obj: <code object <module> at 0x00000000035F1660, file "err.log", line 2>, test: <function test at 0x00000000035ECF28>, Fibonaci: <function Fibonaci at 0x00000000035ECD08>, res: 45, __builtins__: <module builtins (built-in)>, i: <function <listcomp>.<lambda> at 0x000000000360E488>, code: "\ndef Fibonaci(max):\n    n,a,b = 0,0,1\n    while (n < max):\n        print(b)\n        a,b = b,a + b\n        # 不相当于 a = b; b = a+b\n        # 相当于 t = (b,a+b)\n              # a = t[0]\n              # b = t[1]\n        n = n+1\n    return ‘done‘\n", x: 1, __name__: __main__, sayhi: <function sayhi at 0x00000000035ECC80>, functools: <module functools from F:\\Python3.4\\lib\\functools.py>, calc: <function <lambda> at 0x00000000035ECD90>, b: {}}
None
**********************22.oct**********************
0o1 0o23 0o10
**********************23.pow**********************
243
*********************24.round*********************
1.334
*********************25.slice*********************
1
2
3
4
5
********************26.sorted*********************
{1: alex, 2: June, 3: Allen, 17: Yunny, 7: Cook, 10: alex}
[1, 2, 3, 7, 10, 17]
[(1, alex), (2, June), (3, Allen), (7, Cook), (10, alex), (17, Yunny)]
[(1, 12), (3, 47), (2, 50), (7, 58), (10, 96), (17, 99)]
*********************27.vars**********************
**********************28.zip**********************
<zip object at 0x000000000360B9C8>
(1, a)
(2, b)
(3, c)
(4, d)
<zip object at 0x000000000360B9C8>
(1, a)
(2, b)
(3, c)
(4, d)
********************29.import*********************

Process finished with exit code 0

 

以上是关于2017/9/11——何某某更博,花时间整理了所有的Python内置方法的用法,便于日后复习的主要内容,如果未能解决你的问题,请参考以下文章

未整理算法的总结

更博更博233 总结一点点vue

特大好消息!阿里P8架构师花半年整理的java核心笔记,理论到实战全搞定

更博更博233 总结一点点vue

2017-9-11 - A - webServer

花1个月时间整理了2021年上千道Java面试题400多页文档,分享给有缘人