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

Summary of built-in functions in Python

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

Share

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

This article mainly introduces "Python built-in function summary". In daily operation, I believe many people have doubts about the built-in function summary of Python. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "Python built-in function summary"! Next, please follow the editor to study!

31 hash ()

Returns the hash value of the object

In: hash (xiaoming) Out: 6139638

32 help ()

Returns the help document for the object

In [113]: help (xiaoming) Help on Student in module _ _ main__ object:class Student (builtins.object) | Methods defined here: | | _ _ init__ (self, id) Name) | | _ repr__ (self) |-| Data descriptors defined here: | | _ _ dict__ | dictionary for instance variables (if defined) | | _ _ weakref__ | List of weak references to the object (if defined)

33 id ()

Returns the memory address of the object

In: id (xiaoming) Out: 98234208

34 input ()

Get user input

In: input () aaOut [116]:\'aa\'

35 int ()

Int (x, base = 10), x may be a string or numeric value, converting x to a normal integer. If the parameter is a string, it may contain symbols and decimal points. If the representation range of an ordinary integer is exceeded, a long integer is returned.

In [120]: int (\'12\, 16) Out [120]: 18

36 isinstance (object, classinfo)

To determine whether object is an instance of class classinfo, true is returned.

In [20]: class Student ():...:: def _ _ init__ (self,id Name):...: self.id = id.:: self.name = name.:: def _ repr__ (self):.:: return\'id =\'+ self.id +\', name =\'+ self.name.: In [21]: xiaoming = Student (\ '001\' \ 'xiaoming\') In [22]: isinstance (xiaoming,Student) Out [22]: True

37 issubclass (class, classinfo)

If class is a subclass of the classinfo class, return True:

In [27]: class undergraduate (Student):...: def studyClass (self):...: pass...: def attendActivity (self):...: pass...: In [28]: issubclass (undergraduate,Student) Out [28]: TrueIn [29]: issubclass (object,Student) Out [29]: FalseIn [30]: issubclass (Student,object) Out [30]: True

If class is a subclass of an element in the classinfo tuple, True is also returned

In [26]: issubclass (int, (int,float)) Out [26]: True

38 iter (object, sentinel)

Returns an iterable object that can be omitted by sentinel

39 len (s)

Returns the length of the object (number of elements)

In [83]: dic = {\ a\': 1,\'b\': 3} In [84]: len (dic) Out [84]: 2

40 list ([iterable])

Returns a variable sequence type

In [85]: list (map (lambda x: X% 2)) Out [85]: [True, True, False, False, True]

41 map (function, iterable, …)

Returns an iterator that applies function to each item in iterable and outputs its results:

In [85]: list (map (lambda x: X% 2)) Out [85]: [True, True, False, False, True]

You can pass in multiple iterable objects, and the output length is equal to the length of the shortest sequence:

In [88]: list (map (lambda XMague y: X% 2jurisdiction 1 and y% 2Q 0, [1Med 3 Magi 2 Jing 4 Jing 1], [3 Ling 2 Jing 1 Jing 1])) Out [88]: [False, True, False, False]

42 max (iterable,* [, key, default])

Returns the maximum:

In [99]: max: max ((), default=0) Out [99]: 0In [89]: di = {\ a\': 3,\'b1\': 1,\'c\': 4} In [90]: max (di) Out [90]:\'c\'In [90]: a = [{\ 'name\':\ 'xiaoming\',\ 'age\': 18,\ 'gender\':\ 'male\'} {\ 'name\':: xiaohong\',\ 'age\': 20,\ 'gender\':\ 'female\'}] In [104]: max (max keyword lambda x: X [\ 'age\']) Out [104]: {\ 'name\':\ 'xiaohong\',\ 'age\': 20,\ 'gender\':\ 'female\'}

43 min (iterable,* [, key, default])

Return to the minimum

44 memoryview (obj)

Returns the "memory View" object created by a given argument, and the Python code accesses the internal data of an object, as long as the object supports the buffer protocol without copying

45 next (iterator, [, default])

Returns the next element of an iterable object

In: it = iter: next (it) Out: 5In: next (it) Out: 3In: next (it) Out: 4In: next (it) Out: 1In: next (it,0) The default return value is 0Out: 0In: next (it)-StopIteration Traceback (most recent call last) in-> 1 next (it) StopIteration:

46 object ()

Returns a new object with no features. Object is the base class for all classes.

In: O = object () In: type (o) Out: object

47 open (file)

Returns the file object

In: fo = open (\ 'D:/a.txt\', mode=\'r\', encoding=\ 'utf-8\') In [147]: fo.read () Out [147]:\'\ ufefflife is not so long,\ nI use Python to play.

Mode value table:

48 pow (base, exp [, mod])

Base is the exp power of the base, if mod gives, take the remainder.

In: pow (3,2,4) Out: 1

49 print (objects)

Print object, this function does not explain

50 class property (fget=None, fset=None, fdel=None, doc=None)

Returns the property property, typical usage:

Class C: def _ init__ (self): self._x = None def getx (self): return self._x def setx (self, value): self._x = value def delx (self): del self._x # uses the property class to create the property attribute x = property (getx, setx, delx, "I\ m the\'x\ 'property.")

Using the python decorator, implement exactly the same effect code as above:

Class C: def _ _ init__ (self): self._x = None @ property def x (self): return self._x @ x.setter def x (self, value): self._x = value @ x.deleter def x (self): del self._x

51 range (stop)

Range (start, stop [, step])

Generate an immutable sequence:

In [153l]: range (11) Out [153i]: range (0Magne11) In [154]: range (0mag11) Out [154]: range (0mai 11)

52 reversed (seq)

Returns a reverse iterator:

In: rev = reversed.) In: for i in rev:...: print (I)...: 13241

53 round (number [, ndigits])

Rounded up, ndigits stands for keeping a few digits after the decimal point:

In [157]: round (10.0222222, 3) Out [157]: 10.022

54 class set ([iterable])

Returns a set object that removes duplicates:

In: a = [1, 2, 3, 1] In: set (a) Out [160]: {1,2, 3, 4}

55 class slice (stop)

Returns a slice object that represents the index set specified by range (start, stop, step)

In [170,170,1] In: a [slice] # is equivalent to a [0: 5:2] Out: [1,2,1]

56 sorted (iterable, *, key=None, reverse=False)

Sort:

In [174a]: In: sorted: [4,3,2,1,1] In: a = [{\ 'name\':\ 'xiaoming\',\ 'age\': 18,\ 'gender\':\ 'male\'}, {\ 'name\':\': xiaohong\',\ 'age\': 20 \ 'gender\':\ 'female\'}] In [180]: sorted (sorted keyword lambda x: X [\ 'age\'], reverse=False) Out [180]: [{\ 'name\':\ 'xiaoming\',\ 'age\': 18,\ 'gender\':\ 'male\'}, {\ 'name\':\ 'xiaohong\',\ 'age\': 20,\ 'gender\':\ 'female\}]

57 @ staticmethod

Convert a method to a static method without explanation

58 class str (object=\'\)

Returns a str version of object,str that is the built-in string class

59 sum (iterable, /, start=0)

Summation:

In [181a]: a = [1Magne 4Jing 2Jing 3Jue 1] In [182]: sum (a) Out [182]: 11In [185A]: sum (aPower10) # the initial value of summation is 10Out [185]: 21

60 super ([type [, object-or-type]])

Returns a proxy object that delegates the method call to the parent or sibling class of type

61 tuple ([iterable])

Although it is called a function, tuple is actually an immutable sequence type

62 class type (object)

Class type (name, bases, dict)

When a parameter is passed, the type of object is returned:

In: type (xiaoming) Out: _ _ main__.StudentIn [187]: type (tuple ()) Out [187]: tuple

63 zip (* iterables)

Create an iterator that aggregates elements from each iterable object:

In [188x]: a = range (5) In [199a]: B = list (\ 'abcde\') In [1993]: bOut [193a],\ b\'b\',\ c\',\ d\' \'e\'] In [194]: [str (y) + str (x) for xdirection y in zip (Arecedence b)] Out [194]: [\'a0\',\'b1\',\'c2\',\'D3\',\'e4\'] so far On the "Python built-in function summary" study is over, I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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