SoFunction
Updated on 2025-03-02

The 8 most commonly used built-in functions that Python novices must have (recommended)

Python has built-in a large number of functional functions for us. The official document lists 69, some of which we often encounter in development, and some functions are rarely used. Here we list the 8 functions that are most frequently used by developers and their detailed usage.

print()

The print function is the first function you come into contact with when learning Python. It outputs objects to the standard output stream and can print out any number of objects. The specific definition of the function:

print(*objects, sep=' ', end='', file=, flush=False)

Objects are mutable parameters, so you can print out any number of objects at the same time.

>>> print(1,2,3)1 2 3

By default, each object is separated by spaces, and by specifying the sep parameter, you can use commas to separate it.

>>> print(1,2,3, sep=',')1,2,3

The default output of the object is a standard output stream, and you can save the content to a file.

>>> print(1,2,3, sep=',', file=open("", "w"))

isinstance()

You can use the isinstance function to determine whether an object belongs to an instance of a certain class. The definition of the function

isinstance(object, classinfo)

classinfo can be either a single type object or a tuple composed of multiple type objects. It returns True as long as the object's type is any of the tuples, otherwise it returns False

>>> isinstance(1, (int, str))True>>> isinstance("", (int, str))True>>> isinstance([], dict)False

range()

The range function is a factory method used to construct a continuous immutable integer sequence object from [start, stop) (not including stop). This sequence is very similar to a list in function. The function definition:

range([start,] stop [, step]) -> range object

start optional parameter, the starting point of the sequence, default is 0

stop Required parameter, end point of the sequence (not included)

step optional parameter, the step size of the sequence is 1 by default, and the generated element rule is r[i] = start + step*i

Generate a list of 0~5

>>> >>> range(5)range(0, 5)>>> >>> list(range(5))[0, 1, 2, 3, 4]>>>

The default starts from 0, generates 5 integers between 0 and 4, and does not include 5. The default step is 1, and each time is added 1 in the previous time.

If you want to repeat an operation n times, you can use the for loop to configure the range function to implement it

>>> for i in range(3):...   print("hello python")...hello pythonhello pythonhello python

Step size is 2

>>> range(1, 10, 2)range(1, 10, 2)>>> list(range(1, 10, 2))[1, 3, 5, 7, 9]

The starting point starts from 1, the end point is 10, and the step length is 2. Each time, add 2 on the basis of the previous element, forming an odd number between 1 and 10.

enumerate()

It is used to enumerate iterable objects, and can also obtain the following table index value of each element, function definition:

enumerate(iterable, start=0)

For example:

>>> for index, value in enumerate("python"):...   print(index, value)...0 p1 y2 t3 h4 o5 n

index starts from 0 by default. If the parameter start is explicitly specified, the index index starts from start

>>> for index, value in enumerate("python", start=1):...   print(index, value)...1 p2 y3 t4 h5 o6 n

If you do not use the enumerate function, you need more code to get the subscript index of an element:

def my_enumerate(sequence, start=0):  n = start  for e in sequence:    yield n, e    n += 1
>>> for index, value in my_enumerate("python"):  print(index, value)0 p1 y2 t3 h4 o5 n

len

len is used to get the number of elements in the container object. For example, you can use the len function to determine whether the list is empty.

>>> len([1,2,3])3>>> len("python")6>>> if len([]) == 0:    pass

Not all objects support len ​​operations, for example:

>>> len(True)Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: object of type 'bool' has no len()

In addition to sequence objects and collection objects, custom classes must implement the __len__ method that can be used on the len function.

reversed()

reversed() inverts the sequence object, you can invert the string, invert the list, and invert the tuple

>>> list(reversed([1,2,3]))[3, 2, 1]

open()

The open function is used to construct a file object, and after construction, it can read and write contents.

open(file, mode='r', encoding=None)

Read operation

# Open the file from the current path, by default, read

>>>f = open("")>>>()...

Sometimes you need to specify the encoding format, otherwise you will encounter garbled code.

f = open("", encoding='utf8')

Write operation

>>>f = open("", 'w', encoding='utf8')>>>("hello python"))

When the content exists in the file, the original content will not be overwritten. If you do not want to be overwritten, you can directly append the new content to the end of the file. You can use the a mode

f = open("", 'a', encoding='utf8')("!!!")

sorted()

sroted is to reorder the list. Of course, other iterable objects support re-emission, returning a new object, and the original object remains unchanged

>>> sorted([1,4,2,1,0])[0, 1, 1, 2, 4]

The above is a detailed explanation and integration of commonly used built-in functions in Python introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!