Python Teaching | Python Built-in Functions

Table of contents

I. Introduction

2. Mathematical operation functions

Three, data type class function

4. Statistical functions

5. Helper functions

6. File read and write functions

Seven, other built-in functions

8. Conclusion

Nine, Python teaching series content

This article has a total of 3281 words, and it takes about 9 minutes to read. Corrections are welcome!

Introduction to Part1

Python can be roughly divided into three parts in terms of content, namely the core language, standard library and third-party library (“library” is also called “module” or “package”). As of this installment, our series on teaching Python has been covering the core parts of the language, which include data types, basic syntax, control structures (branches, loops, etc.), exception handling, functions, classes, and more. Most of these are must-haves no matter what you do with Python.

The standard library refers to all the basic libraries that come with Python and can be called directly. These libraries cover many commonly used basic functions, such as network requests, system interaction, regular expressions, etc. These libraries have been strictly tested and verified to ensure stability, reliability and execution efficiency, and can effectively improve development efficiency .

The third-party library is a treasure chest in the Python ecosystem. It refers to a Python library that is not officially provided by Python and developed by an organization or individual. It includes a lot of useful tools, the tool libraries of machine learning and artificial intelligence that we are familiar with, all of which come from third-party libraries. According to statistics from the official website of PyPI (Python Package Index), as of February 2023, the number of third-party libraries has exceeded 330,000! Covering a variety of different fields and application scenarios.

The Python built-in functions covered in this article are part of the core language. We have gradually learned the basic syntax of Python in the articles introducing data types and control structures, but we still need a chance to learn the scattered and disordered built-in functions in Python, which is the purpose of this article. Python officially provides 68 built-in functions. These built-in functions mainly provide simple and basic functions with high practicability. What needs to be reminded is that when we use a custom function, we should try to avoid the function name being the same as the name of the built-in function, otherwise it may cause program exceptions. Next, for the purpose of processing data, we will introduce the commonly used built-in functions in Python by category.

Part2 Mathematical Operations Function

In data research, it is often necessary to calculate some indicators or coefficients. Although ready-made modules can be used to directly calculate, when some variables need to be changed according to the actual situation, we still need to calculate according to the formula. This requires some functions of mathematical operations.

Python provides 4 built-in functions of mathematical operations, as shown in the following table.

After reading the above table, you may ask, why are there only these 4 built-in functions of mathematics in Python, shouldn’t it be enough? Of course not enough! These four functions alone are obviously not enough to meet the needs of use, but in fact there are a lot of data operation functions in Python, but these functions are all encapsulated in the standard library math, and the other functions scattered outside are only in the above table of several. When you need to use other mathematical functions, you can import the math library for use.

Part3 data type class function

When we introduced Python’s basic data types and combined data types, we have already introduced the basic functions related to data types (links to related articles have been placed at the end of the article). Here we review and summarize these functions again, and recognize several new functions. The built-in functions commonly used in Python related to data types are shown in the following table.

The zip(), enumerate() and eval() in the above table are functions that are often used in data processing; bool() is a function that is not easy to understand. We have already introduced these functions in the series of articles on Python teaching, and some of them are introduced in conjunction with data processing cases. If you want to review these knowledge points again, you can find them according to the following clues.

  • zip(): Looping constructs in Python (below), section “Synchronizing traversal of loops”.

  • enumerate(x): Looping constructs in Python (below), section “Adding indexed loops”.

  • eval(): Loop Constructs in Python (Part 2), section “Exception Handling in Loops”.

  • bool(): Branch structure (judgment statement) in Python, in the section “Judgment conditions in branch structure”, the if judgment condition: is actually equivalent to if bool (judgment condition):.

Part4 statistical functions

The commonly used statistical built-in functions in Python are shown in the following table.

The statistical functions in the above table often play a role in processing data tables. For example, use all(x) or any(x) to judge whether there are missing values in a field, or whether a field is all empty; use max(x)/min(x) to view extreme values in a field.

Part5 helper function

When we want to learn the usage and parameters of a function in Python; we want to know the properties of an object; or we are ready to learn a new third-party library, we can directly use Python’s built-in helper functions, which can help you quickly Learn about relevant information (different versions of Python or third-party libraries may give inconsistent help documents). The commonly used helper built-in functions in Python are shown in the following table.

We use help(func) in the above table to view the help document of the explode function of the DataFrame object in the data processing module pandas, and get the result as shown in the figure below.

We can quickly understand the usage, parameters and usage examples of this function through the official help documentation. However, all help documents for built-in functions, standard libraries, and third-party libraries are written in English, which is the rule of the Python community.

Part6 file read and write functions

Python built-in functions include related functions for reading and writing text files, as shown in the following table.

built-in function

Functional description

open()

It is used to open a file and return a file object, and parameters such as file path, open mode, and file encoding can be specified.

When you need to process some text files for some text analysis, such as company annual reports; or when you need to store some comparison relationships, you can use the open() function to open, or create, append, modify or read text files. For example, we need to write the comparison between the industry name and industry code in the “2017 National Economic Industry Classification” into a json file for later reading and use. At this point you can use the open() function and the standard library json. The following is the process and method of operation.

“2017 National Economic Industry Classification”:

https://images3.mca.gov.cn/www/file/201711/1509495881341.pdf

First, process the “2017 National Economic Industry Classification” and generate a comparison dictionary as shown in the figure below.

Then use the open() function to create a text object File, and finally use the json library to write the above dictionary to the file.

# import standard library json
import json # Use the open() function to create the target file, using an encoding that can display Chinese
with open("Industry_comparison.json", mode='w', encoding='utf-8-sig') as File:# Use json library to write dictionary to json file. (Actually, json files are also a kind of text files) # The variable Target_dict is the dictionary in the above picture
json.dump(Target_dict, File, ensure_ascii=False, indent=4)

The final written json file is shown in the figure below. After writing the file, when we need to use this comparison relationship again, we can directly read the written file.

In the above code, we specify the mode parameter of the open() function as ‘w’, which means to open the file in write mode. The available parameter values and meanings of the mode parameter are shown in the following table.

We can also use the help function help() mentioned above to view the documentation of the open() function, and the results are as follows.

help(open) # No need to add parentheses after open

Part7 other built-in functions

In addition to some conveniently classified built-in functions introduced above, there are other commonly used built-in functions, as shown in the following table.

Python has a total of 68 built-in functions, and those less commonly used built-in functions that have not appeared above are shown in the table below.

Part8 Conclusion

Learning Python is optional. For the purpose of data processing, this article focuses on introducing some frequently used or powerful built-in functions in the process of data processing. In the following articles, we will introduce the standard library and third-party library that data processing needs to learn.

Part9Python teaching series

  • The first step in learning Python – environment installation and configuration

  • Python primitive data types

  • Python string manipulation (on)

  • Python string manipulation (below)

  • Python variables and basic operations

  • Composite Data Type – List

  • Composite data type – collection (contains examples)

  • Composite Data Types – Dictionaries & Tuples

  • Branching structure in Python (judgment statement)

  • Loop structure in Python (Part 1)

  • Loop structure in Python (Part 2)

  • Definition and call of Python function

  • Continuously updating…

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledgePython entry skill treeBasic grammarCommon built-in functions 258507 people are studying systematically