Python has 35 built-in functions. Are you ok ay?

Keywords: Python ascii encoding Lambda

Python has more than 60 built-in functions, of which 35 are combed today

1 abs()

Modulus of Absolute or Complex Numbers

.

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

2 all()  

Accepts an iterator and returns True if all elements of the iterator are true or False otherwise

In [2]: all([1,0,3,6])
Out[2]: False

In [3]: all([1,2,3])
Out[3]: True

3 any()  

Accepts an iterator that returns True if an element in the iterator is true, or False if it is not

In [4]: any([0,0,0,[]])
Out[4]: False

In [5]: any([0,0,1])
Out[5]: True

4 ascii()  

Call the repr() method of the object 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.name

In [33]: print(xiaoming)
id = 001, name = xiaoming

In [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([0,0,0])
Out[38]: True

In [39]: bool([])
Out[39]: False

In [40]: bool([1,0,1])
Out[40]: True

9 bytes()  

Convert a string to a byte type

In [44]: s = "apple"

In [45]: bytes(s,encoding='utf-8')
Out[45]: b'apple'

10 str()  

Convert character type, numeric type, etc. to string type

In [46]: integ = 100

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

11 callable()  

To determine whether an object can be called, a callable object, such as a function str, int, and so on, is called. However, the instance of xiaoming in Example 4 is not callable:

In [48]: callable(str)
Out[48]: True

In [49]: callable(int)
Out[49]: True

In [50]: xiaoming
Out[50]: id = 001, name = xiaoming

In [51]: callable(xiaoming)
Out[51]: False

12 chr()

View ASCII characters corresponding to decimal integers

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

13 ord()

View a decimal number corresponding to an ascii

In [60]: ord('A')
Out[60]: 65

14 classmethod()  

The function corresponding to the classmethod modifier does not require instantiation and the self parameter, but the first parameter needs to be a cls parameter representing its own class, which can be used to call the properties of the class, the methods of the class, the instantiated object, 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 python may recognize or execute, or read the text as a string and recompile.

In [74]: s  = "print('helloworld')"

In [75]: r = compile(s,"<string>", "exec")

In [76]: r
Out[76]: <code object <module> at 0x0000000005DE75D0, file "<string>", line 1>

In [77]: exec(r)
helloworld

16  complex()

Create a complex number

In [81]: complex(1,2)
Out[81]: (1+2j)

17 delattr()  

Delete properties of objects

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'],[1,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 range without parameters; returns properties of parameters with parameters, 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()  

Remainder and quotient

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

21 enumerate()  

Returns an enumerable object whose next() method returns a tuple.

In [98]: s = ["a","b","c"]
    ...: for i ,v in enumerate(s,1):
    ...:     print(i,v)
    ...:
1 a
2 b
3 c

22 eval()  

Evaluates the string str as a valid expression and returns the result of the calculation; takes out the contents of the string

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

23 exec()  

Execute string or complie method compiled string, no return value

In [74]: s  = "print('helloworld')"

In [75]: r = compile(s,"<string>", "exec")

In [76]: r
Out[76]: <code object <module> at 0x0000000005DE75D0, file "<string>", line 1>

In [77]: exec(r)
helloworld

24 filter()  

Filter, construct a sequence, equivalent to

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

Setting filter conditions in the function loops through the elements in the iterator one by one, leaving behind the elements when the return value is True to form a filter-type data.

In [101]: fil = filter(lambda x: x>10,[1,11,2,45,7,6,13])

In [102]: list(fil)
Out[102]: [11, 45, 13]

25 float()  

Convert a string or integer to a floating point number

In [103]: float(3)
Out[103]: 3.0

26 format()  

Formatting the output string, format(value, format_spec) is essentially a call to 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 a collection that cannot be modified.

In [105]: frozenset([1,1,3,2,3])
Out[105]: frozenset({1, 2, 3})

28 getattr()  

Get the properties of the object

In [106]: getattr(xiaoming,'name')
Out[106]: 'xiaoming'

29 globals()  

Returns a dictionary describing the current global variable

30 hasattr()

In [110]: hasattr(xiaoming,'name')
Out[110]: True

In [111]: hasattr(xiaoming,'id')
Out[111]: False

31 hash()  

Returns the hash value of an object

In [112]: hash(xiaoming)
Out[112]: 6139638

32 help()  

Return help documentation for objects

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 [115]: id(xiaoming)
Out[115]: 98234208

34 input()  

Get user input

In [116]: input()
aa
Out[116]: 'aa'

35  int()  

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

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

Posted by OriginalSunny on Mon, 06 Jan 2020 07:04:23 -0800