Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods built into Python

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "what are the built-in methods in Python", the content is simple and clear, and I hope it can help you solve your doubts. Let me lead you to study and learn this article "what are the built-in methods in Python".

Abs () takes an absolute value

Convert dict () data into a dictionary

Min () takes the minimum value from the list

> a = [1, min, 4, 5, 5, 5, 5, 5, 1, 1, 5, 1, 1, > max (a) 5 >

All () returns True if all data in the collection is True, otherwise FALSE (0 is false, everything else is True), and true if the collection is empty.

All (iterable, /) Return True if bool (x) is True for all values x in the iterable. If the iterable is empty, return True. > > a = [1 all (a) True > > a.append (0) > > a [1 all (a) False > a = [] > > all (a) True > bool (a) # Test Boolean judgment is FALSEFalse > >

Any () is True as long as one value is True, if the collection is empty False.

Any (iterable, /) Return True if bool (x) is True for any x in the iterable. If the iterable is empty, return False. > a = [False,0] > any (a) False > > a = [False,0,1] > any (a) True >

Dir () prints all current variables

Hex () digits converted to hexadecimal

Slice () slice

Integers and residuals divided by divmod ()

> 10% 31 > > 10 divmod 33 > 10% 31 > 10% 31 > 10% 31 > > 10% 31 > > 10% 31 > > 10% 31 > > 10% 31 > > 10% 31 > > 10% 31 > > 10% 31 > > 10% 31 > > 10% 31 > > 10% 31 > > 10% 31 > > 10% 31 > > 10% 31 > 10% 31 > > 10% 31 > > 10% 31 > > 10% 31 > > 10% 31 > > 10% 31 > 10% 31 > > 10% 31 > > 10% 31 > > 10% 31 >

Id () to find the data memory address

Item () dictionary variable list

Sort by sorted ()

> > l [0,1,2,3,55,6,7,8,9] > > sorted (l) [0,1,2,3,5,6,7,8,9,55]

Enumerate () enumeration

Oct () to Octal

Bin () to 2jinzhi

Eval () transforms a string into a code according to the interpreter rules and can only turn a single line.

> > eval ('print ("hello the world")') hello the world > eval

The function of exec () is the same as that of eval, except that it can have multiple lines

> code = '... If 3 > 5:. Print ('aaaa'). Else:. Print ('bbbb'). '' > exec (code) bbbb > another difference between # exec and eval > res = eval ('1x 2'3') > res2 = exec ('4'5'6') > print (res,res2) 6 None # exec cannot get the returned value >

Ord () query ascill code location

Chr () ASCII code position returns specific value

> ord ('a') 97 > chr (97)'a'

Summation of sum () set

> l [0,1,2,3,55,6,7,8,9] > > sum (l) 96 > >

Bytearray ()

Map ()

The map function in Python applies to each iterable item and returns a result list. If other iterable parameters are passed in, the map function iterates each parameter with the corresponding handler. The map () function takes two arguments, one is the function and the other is the sequence. Map acts the passed function on each element of the sequence in turn and returns the result as a new list.

There is a list, L = [1 map 2 3 4 5 5 6 7 8], we want to apply f (x) = x ^ 2 to this list, then we can use the map function to deal with it.

> L = [1, def pow2 2, 3, 4,] > Return xonomx. > > map (pow2,L) [1, 4, 9, 16] # eg2 > list (map (lambda x: Xerox, [1, 2, 3, 4, 5])) [1,4,9,16, 25]

Filter ()

> list (filter (lambda XRIX > 3, [1 recorder 2 pr 3 4 Jol 5]) [4JE 5]

Redue

Import functools # phthon2 function 3 needs to be imported > functools.reduce () > functools.reduce (lambda x recorder, [1Zhi 2 Zhi 3, 4 Magi 22, 3]) 35 > functools.reduce (lambda x recorder, [1 Min 2 recorder 3, 4 Magi 22], 50) 85 > reduce (...) Reduce (function, sequence [, initial]) > value Apply a function of two arguments cumulatively to the items of a sequence, from left to right, so as to reduce the sequence to a single value. For example, reduce (lambda x, y: X roomy, [1,2,3,4,5]) calculates (1x 2) + 3) + 4) + 5). If initial is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty. The above is all the content of the article "what are the built-in methods in Python?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report