Anatomy of Python's built-in functions

Source: Internet
Author: User
Tags iterable ord pow

Built-in methods:

1. ABS () #取绝对值;
>>> ABS (-11)
11
>>> ABS (11)
11

2. All #非0即真 to ensure that all elements are true;
>>> all ([0,-5, 3])
False
>>> all ([1,-5, 3])
True

3. Any #非0即真 to ensure that an element is true;
>>> any ([0,-5, 3])
True
>>> any ([1,-5, 3])
True
>>> any ([0,0])
False

4. ASCII () #将内存对象转变为一个可打印的字符形式
>>> ASCII ("ABCD")
"' ABCD '"
>>> ASCII ("abcd111")
"' abcd111 '"
>>> ASCII (1)
' 1 '
>>> ASCII ([up])
' [1, 2] '

5. Bin () #将十进制数字转换二进制
>>> Bin (8)
' 0b1000 '
>>> Bin (19)
' 0b10011 '
>>>

6. BOOL (): #判断是否真, empty list, etc. false false
>>> bool ([])
False
>>> bool ([up])
True
>>>

7. ByteArray () #把字符变成asscii可以更改;
>>> B = ByteArray ("ABDCD", encoding= "Utf-8")
>>>
>>> print (b[0])
97
>>> print (b[1])
98
>>> b[1]=100
>>> print (b)
ByteArray (b ' ADDCD ')

A = bytes ("ABDCD")
>>> A
B ' ABDCD

8. chr () #将ascii值转为对应的字符.
>>> Chr (90)
Z

9. Ord () #将字符转为ascii
>>> Ord ("a")
97
>>> Ord ("Z")
90

Complie () #简单的编译反执行. can use exec can execute string source code;
>>> code = "For I in Range":p rint (i) "
>>> Code
' For I in Range ':p rint (i) '
>>>
>>> Compile (Code, "", "exec")
<code object <module> at 0x00b540d0, file "", Line 1>
>>>
>>> c = Compile (code, "", "exec")
>>> EXEC (c)
0
1
2
3
4
5
6
7
8
9
>>>

Dir () #查看对象的方法:
>>> dir ({})
[' __class__ ', ' __contains__ ', ' __delattr__ ', ' __delitem__ ', ' __dir__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' __ge__ ', ' __ getattribute__ ', ' __getitem__ ', ' __gt__ ', ' __hash__ ', ' __init__ ', ' __init_subclass__ ', ' __iter__ ', ' __le__ ', ' __len__ ' , ' __lt__ ', ' __ne__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __setitem__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' clear ', ' copy ', ' Fromkeys ', ' get ', ' items ', ' Keys ', ' Pop ', ' Popitem ', ' setdefault ', ' UPDA ' Te ', ' Values ']
>>> dir ([])
[' __add__ ', ' __class__ ', ' __contains__ ', ' __delattr__ ', ' __delitem__ ', ' __dir__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' _ _ge__ ', ' __getattribute__ ', ' __getitem__ ', ' __gt__ ', ' __hash__ ', ' __iadd__ ', ' __imul__ ', ' __init__ ', ' __init_subclass_ ' _ ', ' __iter__ ', ' __le__ ', ' __len__ ', ' __lt__ ', ' __mul__ ', ' __ne__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ' , ' __reversed__ ', ' __rmul__ ', ' __setattr__ ', ' __setitem__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' Append ', ' Clear ', ' copy ', ' Count ', ' extend ', ' index ', ' Insert ', ' pop ', ' remove ', ' reverse ', ' sort ']
>>>


Divmod () #返回商和余数
>>> Divmod (5,1)
(5, 0)
>>> Divmod (5,2)
(2, 1)
>>> Divmod (5,3)
(1, 2)
>>>

Enumerate (): #
>>> sersons = ["Spring", "Summer", "Autron", "Wintor"]
>>>
>>> List (Enumerate (sersons))
[(0, ' Spring '), (1, ' Summer '), (2, ' Autron '), (3, ' wintor ')]
>>>
>>> List (Enumerate (sersons, start=1))
[(1, ' Spring '), (2, ' Summer '), (3, ' Autron '), (4, ' wintor ')]
>>>
>>>
>>> for K,v in enumerate (sersons, start=1):p rint ("%d--%s"% (k, v))
...
1--spring
2--summer
3--autron
4--wintor
>>>

Eval () #将字符串转为数据类型, no statement, EXEC with statement
>>> x = "[1,2,3,4]"
>>> x
' [1,2,3,4] '
>>>
>>> X[0]
‘[‘
>>>
>>> y = eval (x)
>>> y
[1, 2, 3, 4]
>>> Y[0]
1
>>>

. EXEC () # Execute string source code
>>> x = "For I in Range":p rint (i) "
>>> x
' For I in Range ':p rint (i) '
>>>
>>>
>>> EXEC (x)
0
1
2
3
4
5
6
7
8
9
>>>

Filter (): #按条件进行过滤
>>> x = filter (lambda n:n>5, Range (10))
>>> for I in X:print (i)
...
6
7
8
9


Take the following values out to the front processing
>>> x = map (lambda n:n*n, Range) #按照范围的输出, equivalent to: x = [Lambda n:n*n for I in range (10)]
>>> for I in X:print (i)
...
0
1
4
9
16
25
36
49
64
81

anonymous function: can only handle March operations
>>> Lambda N:print (n)
<function <lambda> at 0x0368bdb0>
>>>
>>> (lambda n:print (n)) (5)
5
>>>
>>> X=lambda n:print (n)
>>> x (5)
5
>>> Lambda m:m*2
<function <lambda> at 0x03716198>
>>> Y=lambda m:m*2
>>> y (5)
10
>>>
>>> z = Lambda N:3 if n<4 else n
>>> Z (2)
3
>>> Z (5)
5

Frozenset () #集合冻结, cannot be modified after

>>> A=set ([12,2,12,12,12,12])
>>> A
{2, 12}
>>> A.add (13)
>>> A
{2, 12, 13}
>>>
>>> B = Frozenset (a)
>>> b
Frozenset ({2, 12, 13})
>>>
>>>
>>> B.add (3)
Traceback (most recent):
File "<stdin>", line 1, in <module>
Attributeerror: ' Frozenset ' object has no attribute ' add '
>>>


Globals () #返回整个程序所有的全局变量值:
>>> Globals ()
{' __name__ ': ' __main__ ', ' __doc__ ': None, ' __package__ ': None, ' __loader__ ': <class ' _frozen_importlib. Builtinimporter ', ' __spec__ ': None, ' __annotations__ ': {}, ' __builtins__ ': <module ' builtins ' (built-in);, ' Iterable ': <class ' collections.abc.Iterable ', ' Iterator ': <class ' collections.abc.Iterator ', ' X ': < Map object at 0x00b56030>, ' a ': {2, +,--}, ' B ': Frozenset ({2, B, D}), ' Code ': ' For I in Range ':p rint (i) ', ' C ': & Lt;code object <module> at 0x00b54128, file "", line 1>, ' I ': Bayi, ' sersons ': [' Spring ', ' Summer ', ' Autron ', ' Wint or '], ' K ': 4, ' V ': ' wintor ', ' y ': <function <lambda> at 0x036ab6a8>, ' z ': <function <lambda> at 0x037 16150>}
>>>

Hash () # returns the hash value;
>>> Hash ("1")
-585053941
>>>
>>> Hash ("QWQW")
1784621666
>>>

Hex () #抓16进制
>>> Hex (100)
' 0x64 '
>>> Hex (15)
' 0xf '
>>>

Oct () #转为8进制
>>> Oct (10)
' 0o12 '

Locals () #打印本地变量;
>>> locals ()
{' __name__ ': ' __main__ ', ' __doc__ ': None, ' __package__ ': None, ' __loader__ ': <class ' _frozen_importlib. Builtinimporter ', ' __spec__ ': None, ' __annotations__ ': {}, ' __builtins__ ': <module ' builtins ' (built-in);, ' Iterable ': <class ' collections.abc.Iterable ', ' Iterator ': <class ' collections.abc.Iterator ', ' X ': < Map object at 0x00b56030>, ' a ': {2, +,--}, ' B ': Frozenset ({2, B, D}), ' Code ': ' For I in Range ':p rint (i) ', ' C ': & Lt;code object <module> at 0x00b54128, file "", line 1>, ' I ': Bayi, ' sersons ': [' Spring ', ' Summer ', ' Autron ', ' Wint or '], ' K ': 4, ' V ': ' wintor ', ' y ': <function <lambda> at 0x036ab6a8>, ' z ': <function <lambda> at 0x037 16150>}
>>>

Max (): #返回最大值
>>> Max ([1,2,3,4,5])
5 23. Min (): #返回最小值
>>> min ([1,2,3,4,5])
1

Pow (x, y) #返回多次幂, x y-square
>>> Pow (2,2)
4
>>> Pow (2,3)
8

Range (Start,stop,step) 26. Repr () #用字符串表示一个对对象:

Reversed (seq) #反转

Round () #浮点数按照规定四舍五入舍弃,
>>> Round (1.232442)
1
>>> Round (1.232442,2)
1.23
>>> Round (1.237442,2)
1.24
>>>

ID () #取内存id值
>>> ID ("1")
56589120
>>> ID ("a")
56501888

Sorted (Iterable[,key][,reverse]) #排序
>>> a = {5:1,9:2,1:3,8:4,3:9}
>>>
>>> Sorted (a)
[1, 3, 5, 8, 9]
>>> Sorted (A.items ())
[(1, 3), (3, 9), (5, 1), (8, 4), (9, 2)]

>>> Sorted (A.items (), Key=lambda x:x[1])
[(5, 1), (9, 2), (1, 3), (8, 4), (3, 9)]
>>> A.items () #将字典转化为列表.
Dict_items ([(5, 1), (9, 2), (1, 3), (8, 4), (3, 9)])
>>>
>>> List (a)
[5, 9, 1, 8, 3]


SUM (Iterable[,start]) #求和, start is the sum initial value
>>> sum ([1, 3, 5, 8, 9])
26
>>> sum ([1, 3, 5, 8, 9],3)
29
>>> sum ([1, 3, 5, 8, 9],30)
56

Tuple (iterable): #转化为元组
>>> tuple ([1, 3, 5, 8, 9])
(1, 3, 5, 8, 9)

A. zip () #中文就是拉链的意思 that corresponds
>>> a = (1, 3, 5, 8, 9)
>>> B = ("A", "B", "C", "D", "E")
>>>
>>> Zip (A, B)
<zip Object at 0x00b53990>
>>> for n in Zip (A, B):
... print (n)
...
(1, ' a ')
(3, ' B ')
(5, ' C ')
(8, ' d ')
(9, ' e ')
>>>


__import__ ("module name") #只知道模块名时导入模块就方法:

Anatomy of Python's built-in functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.