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 built-in functions of Python

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

Share

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

This article shows you what the built-in functions of Python are, which are concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

The most complete summary of Python built-in functions

1 abs ()

The module of an absolute or complex number.

In [1]: abs (- 6) Out [1]: 6

2 all ()

Accept an iterator and return True if all elements of the iterator are true, otherwise return False

In [2]: all Out [2]: FalseIn [3]: all ([1m 2meme 3]) Out [3]: True

3 any ()

Accepts an iterator, returns True if an element in the iterator is true, otherwise returns False

In [4]: any ([0jue 0jue 0, []]) Out [4]: FalseIn [5]: any ([0rect 0je 1]) Out [5]: True

4 ascii ()

Call the object's repr () method to get the return value of the method

In [30]: class Student ():...: def _ init__ (self,id,name):...: self.id = id.: self.name = name.: def _ repr__ (self):...: return\'id =\'+ self.id +\', name =\'+ self.nameIn [33]: print (xiaoming) id = 001 Name = xiaomingIn [34]: ascii (xiaoming) Out [34]:\'id = 001, name = xiaoming\'

5 bin ()

Convert decimal to binary

In [35]: bin (10) Out [35]:\ '0b1010\'

6 oct ()

Convert decimal to octal

In [36]: oct (9) Out [36]:\ '0o11\'

7 hex ()

Convert decimal to hexadecimal

In [37]: hex (15) Out [37]:\ '0xf\'

8 bool ()

Test whether an object is True or False.

In [38]: bool ([0je 0je 0]) Out [38]: TrueIn [39]: bool ([]) Out [39]: FalseIn [40]: bool ([1meme 0je 1]) Out [40]: True

9 bytes ()

Convert a string to a byte type

In [44]: s = "apple" In [45]: bytes (utf-8 encoding =\ 'utf-8\') Out [45]: B\ 'apple\'

10 str ()

Convert character types, numeric types, and so on to string types

In [46]: integ = 100In [47]: str (integ) Out [47]:\ '100\'

11 callable ()

To determine whether an object can be called, the object that can be called is a callable object, such as functions str, int, etc., but the instance xiaoming in example 4 is not callable:

In [48]: callable (str) Out [48]: TrueIn [49]: callable (int) Out [49]: TrueIn [50]: xiaomingOut [50]: id = 001, name = xiaomingIn [51]: callable (xiaoming) Out [51]: False

12 chr ()

View the ASCII characters corresponding to decimal integers

In [54]: chr (65) Out [54]:\'A\'

13 ord ()

View the decimal number corresponding to an ascii

14 classmethod ()

The function corresponding to the classmethod modifier does not need to be instantiated or self parameter, but the first parameter needs to be a cls parameter that represents its own class, which can be used to call class properties, class methods, instantiated objects, and so on.

In [66]: class Student ():...: def _ init__ (self,id,name):...: self.id = id.: self.name = name.: def _ repr__ (self):...: return\'id =\'+ self.id +\' Name =\'+ self.name.: @ classmethod.: def f (cls):...: print (cls)

15 complie ()

Compile the string into code that python can recognize or execute, or you can read the text into a string and compile it.

In [74]: s = "print (\ 'helloworld\')" In [75]: r = compile (s, "", "exec") In [76]: rOut [76]: In [77]: exec (r) helloworld

16 complex ()

Create a plural

In [81]: complex (1jue 2) Out [81]: (1pm 2j)

17 delattr ()

Delete the properties of an object

In [87]: delattr (xiaoming,\'id\') In [88]: hasattr (xiaoming,\'id\') Out [88]: False

18 dict ()

Create a data dictionary

In [92]: dict () Out [92]: {} In [93]: dict (a =\'a\', b =\'b\') Out [93]: {\ a\':\'a\',\'b\':\'b\'} In [94]: dict (zip ([\ a\',\'b\'], [1jue 2]) Out [94]: {\ a\': 1 \'b\': 2} In [95]: dict ([(\'a\', 1), (\'b\', 2)]) Out [95]: {\ a\': 1,\'b\': 2}

19 dir ()

Returns a list of variables, methods and defined types in the current scope without parameters; returns properties of parameters with parameters, and a list of methods.

In [96]: dir (xiaoming) Out [96]: [\ _ _ class__\',\'_ _ delattr__\',\'_ _ dict__\',\'_ _ dir__\',\'_ _ doc__\',\'_ _ eq__\',\'_ _ format__\',\'_ _ ge__\',\'_ _ getattribute__\',\'_ _ gt__\' \'_ _ hash__\',\'_ _ init__\',\'_ _ init_subclass__\',\'_ _ le__\',\'_ _ lt__\',\'_ _ module__\',\'_ ne__\',\'_ _ new__\',\'_ _ reduce__\',\'_ _ reduce_ex__\',\'_ repr__\' \'_ _ setattr__\',\'_ _ sizeof__\',\'_ _ str__\',\'_ _ subclasshook__\',\'_ _ weakref__\',\ 'name\']

20 divmod ()

Take quotient and remainder respectively

In [97]: divmod (10jol 3) Out [97]: (3,1)

21 enumerate ()

Returns an object that can be enumerated, whose next () method returns a tuple.

In [98]: s = ["a", "b", "c"].: for iMagazine v in enumerate (s Magazine 1):...: print (iMagne v).: 1 a2 b3 c

22 eval ()

Evaluate the string str as a valid expression and return the result to extract the contents of the string

In [99]: s = "1 + 3 + 5"...: eval (s)...: Out [99]: 9

23 exec ()

String compiled by executing a string or complie method, with no return value

In [74]: s = "print (\ 'helloworld\')" In [75]: r = compile (s, "", "exec") In [76]: rOut [76]: In [77]: exec (r) helloworld

24 filter ()

Filter, construct a sequence, which is equivalent to

[item for item in iterables if function (item)]

Set the filter condition in the function, loop through the elements in the iterator one by one, and leave the elements when the return value is True, forming a filter type data.

In [101l]: fil = filter (lambda x: X > 10,] In [102l]: list (fil) Out [102]: [11,45,13]

25 float ()

Convert a string or integer to a floating point number

In: float (3) Out

26 format ()

Formatting the output string, format (value, format_spec) essentially calls the format (format_spec) method of value.

In [104]: print ("I am {0}, age {1}" .format ("tom", 18)) I am tom,age18

27 frozenset ()

Create an immutable collection.

In: frozenset, Out: frozenset ({1,2)

28 getattr ()

Get the properties of an object

29 globals ()

Returns a dictionary that describes the current global variable

30 hasattr ()

In: hasattr (xiaoming,\ 'name\') Out: TrueIn [111l]: hasattr (xiaoming,\'id\') Out [111l]: False above is what Python built-in functions have. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are 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