SoFunction
Updated on 2024-10-29

Six of Python Programming's Most Popular Built-in Functions Used in Detail

Use these functions in everyday python programming to simplify our programming work, and regular use can make programming much more efficient.

1. Map function

The map function can use another function to convert the entire iterable object function, including the conversion of strings to numbers, rounding of numbers and so on.

The reason why using the map function to do these things saves memory, makes the code run faster, and uses less code.

For example, here you need to convert an array of strings into an array of numbers.

Parsing in the traditional way would take several lines of code to complete using a for loop.

strings = ['1', '2', '3', '4']

res = []

for str_ in strings:
    (int(str_))

print(res)

# [1, 2, 3, 4]

Using the map function takes one line of code directly.

strings = ['5', '6', '7', '8']

res_map = map(int, strings)

print(list(res_map))

# [5, 6, 7, 8]

Use map(int,strings), where int is passed in as a function as a parameter, and strings are objects that can be iterated over.

Here again, we use a function we created ourselves to convert serializable data.

Initialize a make_super function to convert English strings to 'uppercase' strings.

def make_super(text):
    res_text = ()

    return res_text


words = ['python', 'java', 'scala']

words_res = list(map(make_super, words))

print(words_res)


# ['PYTHON', 'JAVA', 'SCALA']

Use map(make_super, words), where make_super is passed in as a function and words as serializable data.

2. Lamdba functions

The lambda function is used to create anonymous functions, also known as lambda expressions. In fact, it is just an expression of the existence of the code writing process, if you need to implement a simple function of logic, but write a separate function and more trouble you can use lambda expression only requires a line of code can be completed.

For example, you need to implement a simple addition calculation, using the basic function to implement the need to create an add_1 function.

def add_1(a, b):
    return a + b

print(add_1(5, 2))

And with lambda expressions, a single line of code is straightforward and can be written like the following.

add_2 = lambda a, b: a + b

print(add_2(10, 10))

lambda a,b: a + b means that a,b are taken as arguments and a + b is executed as the arithmetic logic of the function.

3. Enumerate function

The enumerate function is generally used for processing serializable data, and as there is a lot of serializable data in python, the importance of it is understandable.

You can use this function to directly iterate through the subscript index of a serializable data and the corresponding data.

An example using a list data.

words = ['java', 'python', 'scala']

Use the enumerate function to perform a bit of a traversal of the whole thing, eventually returning a new list.

for index, data in enumerate(words):
    print(f'Current Index:{index},Current data:{data}')

# Current index: 0,Current data: java
# Current index: 1,Current data: python
# Current index: 2, current data: scala

Seeing that the returned results contain indexes, data, which facilitates the organization of data and statistics, therefore, the function is also listed as one of the more commonly used functions.

4. Reduce function

The reduce function is usually used to compute the logical operations of an entire list, i.e. a function whose operations can be added to each element of the list.

'''
reduce(function, iterable[, initializer])
'''

For example, if you need to calculate the result of multiplying between each element in a list, you can write it like this.

from functools import reduce

list_ = [10, 20, 30, 40]

print(reduce(lambda a, b: a * b, list_))

Here a lambda expression is used to represent the multiplication of two elements because of the simplicity of the logical operations of the function.

Eventually then use the reduce function, its effect is equivalent to 10 * 20 * 30 * 40, the result is 240,000, and we expect to get the same calculation results.

5. Filter function

filter function, from the literal meaning of it can be seen that it is the meaning of filtering, the use of this function can effectively filter out the unwanted list of data elements.

'''
filter(function, iterable)
'''

In logical processing, again, a handler function and a serializable data are required.

nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 20]

nums_new = filter(lambda m: m % 2 == 0, nums)

print(list(nums_new))

# [2, 4, 6, 8, 20]

Here we have successfully filtered out all the odd data elements and ended up with only the even elements.

6. Zip function

zip function in the process of assembling multiple lists used very much, you can traverse multiple lists at the same time and will be the same position of the elements into a combination of metazoos.

list_res = []

for n in zip([1, 2, 3, 4, 5], ['python', 'java', 'scala', 'c++', 'C#']):

    list_res.append(n)

print(list_res)

# [(1, 'python'), (2, 'java'), (3, 'scala'), (4, 'c++'), (5, 'C#')]

When using the zip function, combinations of data like this can be easily implemented.

By looking at the above operations, we find that they are basically operations on serializable data, because most of the data processing operations in python coding are based on serializable data.

Above is a detailed explanation of the use of the six Python programming most popular built-in functions, more information about Python built-in functions please pay attention to my other related articles!