SoFunction
Updated on 2024-11-19

Python Built-in Functions Built_in Funtions Usage Examples

preamble

In the Python official documentation of the standard library chapter, the first section is an introduction, the second section is Built_in Functions, built-in functions can be seen as an important part of the Python standard library, and there are a lot of built-in functions that we usually rarely use or simply do not know that there are such a good function can actually be used directly.

Built_in Funtions

Next, I'll introduce you to some built-in functions that I think are overlooked.

all

Returns True if all values in the list or iterator are true or null, which is equivalent to

def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True

any

Returns True if at least one of the values in the iterator is true, and False if the iterator is empty, which is equivalent to

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False

dir

Returns all the names of the current scope without parameters, and all the properties of the parameter with parameters.

>>> dir(int)
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']

divmod

Returns both the quotient and remainder of an integer division.

>>> divmod(11,3)       
(3, 2)

enumerate

Returns both the index and the value of the iterator element. The initial value of the index can be set, which is useful in for loops where you need to know the position of the element.

>>> for index, value in enumerate('ABCDEFG'):
...     print(index, value)
...
0 A
1 B
2 C
3 D
4 E
5 F
6 G

id

For CPython it's the memory location of the object

>>> x, y = 1, 2
>>> id(x), id(y)
(1666253264, 1666253296)

isinstance

An instance of determining whether the first parameter is the second parameter; do not use it in the futuretype(1) == int(modal particle intensifying preceding clause)

>>> isinstance('A',str)
True

concluding remarks

I hope you will use these built-in functions wisely in your future development.

The above is Python Built-in Functions Built_in Funtions usage examples explained in detail, more information about Python Built-in Functions Built_in Funtions please pay attention to my other related articles!