SoFunction
Updated on 2024-11-17

A summary of six of the most widely used built-in functions in Python 3

1. Introduction

Little Dick: Fish, it's said that Lamdba can't be missing if you want to write good code.

Small fish: you write a few more lamdba in the project code to try and see if the architects find you for tea and water.

Little Dick: ...

Fish: Just kidding... architects don't spend their own money on tea, they just rub it in when they can...

Little Dick: ...

Fish: Are you trying to understand something with this sudden mention of the lamdba function?

Little Dick: Hey, you can't hide anything from me, so I'm just going to go over the most common built-in functions in python, and I'm going to try to make them work again.

Fish: Honestly.

Little Dick: As you know, my goddess has been learning python lately, so I thought...

Fish: Shit... knew you were like that!

Little Dick: Fish, I can't help it, otherwise... it's all tears when I say it.

Fish: Come on, don't act so pathetic, I'll just smooth it out for you.

Little Dick: Don't say anything, at this point I want to...

2、Built-in function details

2.1 Lamdba Functions

The lambda function is used to create anonymous functions, also known as lambda expressions.

In fact, it is just the existence of an expression, if you need to implement a simple function in the code writing process logic, but write a separate function and more trouble you can use lambda expressions only need 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.

Example:

Implement a simple addition calculation.

I. Basic Functions

code example

# -*- coding:utf-8 -*-
# @Time   : 2022-08-24
# @Author : Carl_DJ

# Basic functions to implement addition calculations
def add_ms(x,y):
    return x + y
print(f'output result:{add_ms(8,9)}')

running result

Output: 17

II. lamdba expressions

Use lamdba expressions for

code example

# -*- coding:utf-8 -*-
# @Time   : 2022-08-24
# @Author : Carl_DJ

#lamdba expression to achieve

add_lamd = lambda x,y:x+y
print(f'lamdbaExpression output results:{add_lamd(5,10)}')

running result

Output result of lamdba expression: 15

Parsing:

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

2.2 Map Functions

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.

2.2.1 Digital conversion

Example:

Converting an array of strings into an array of numbers is used here in two ways:

  • The traditional for loop approach
  • map function

Next, we convert in code.

I. Traditional for loop approach

code example

# -*- coding:utf-8 -*-
# @Time   : 2022-08-24
# @Author : Carl_DJ

#for loop to read the value of the res list.
strings = ['10','20','30','40','50']
res = []

for str in strings:
    (int(str))

print(f'output result:{res}')

running result

Output: [10, 20, 30, 40, 50]

Second, the map function

We use the map function approach to convert.

code example

# -*- coding:utf-8 -*-
# @Time   : 2022-08-24
# @Author : Carl_DJ

# Call the map function
strings = ['11','22','33','44','55']
res_map = map(int,strings)

print(f'mapFunction Output Results:{list(res_map)}')

running result

Output of map function: [11, 22, 33, 44, 55]

As you can see, the map function, a line of code can be converted to complete, very convenient.

2.2.2 Alphabetical Case Conversion

Parsing:

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

typical example

Converting lowercase letters to uppercase letters

code example

# -*- coding:utf-8 -*-
# @Time   : 2022-08-24
# @Author : Carl_DJ

'''
Code to implement the function:
Create function to convert convertible sequence data
Initialize make_super function to convert lowercase English to uppercase English

'''

#define the make_super function.
def make_super(text):
    res_text = ()
    return res_text

#Define the list of English words
words = ['python', 'java', 'ruby','go']
#convert
words_res = list(map(make_super, words))
print(f'Conversion results:{words_res}')

running result

Conversion result: ['PYTHON', 'JAVA', 'RUBY', 'GO']

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

2.3 Filter Functions

define

filter function: the use of this function can effectively filter out unwanted data elements in the list.

methodologies

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

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

# -*- coding:utf-8 -*-
# @Time   : 2022-08-24
# @Author : Carl_DJ

#define list
list_num = [11,22,33,44,55,66,77]
# Filter out the even numbers and keep the odd ones
new_list = filter(lambda n:n % 2 == 1,list_num)

print(f'Filtered results:{list(new_list)}')

running result

Filtered results: [11, 33, 55, 77]

By running the results, you can see that all the even elements were filtered out, leaving the odd elements.

2.4 Reduce Function

define

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.

methodologies

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

code example

Computes the result of multiplying between each element of a list:

# -*- coding:utf-8 -*-
# @Time   : 2022-08-24
# @Author : Carl_DJ

from functools import reduce
#List
list_re = [10,20,30,40,50,]
# Calculate the product of each element
print(f'calculation result:{reduce(lambda x, y: x * y, list_re)}')

running result

Calculation: 12000000

2.5 Enumerate Functions

define

enumerate function: generally used for serializable data processing, and python in the serializable data and more, so, or quite important.

code example

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

# -*- coding:utf-8 -*-
# @Time   : 2022-08-24
# @Author : Carl_DJ

#Define list content
words = ['python', 'java', 'ruby','go']
# Iterate through the execution and return a new list
for index, data in enumerate(words):
    print(f'Current Index:{index},Current data:{data}')

running result

Current index: 0,Current data: python
Current index:1,Current data:java
Current index: 2,Current data: ruby
Current index: 3,Current data: go

2.6 Zip Functions

define

zip function: you can traverse multiple lists at the same time and combine the elements in the same position into a metazoan.

code example

#Define the empty list
list_res = []

# Iterate through the list so that elements in the same position can be combined into a
for m in zip([1, 2, 3, 4, 5], ['python', 'java', 'ruby', 'go', 'C#']):
    list_res.append(m)

print(f'output result:{list_res}')

running result

Output: [(1, 'python'), (2, 'java'), (3, 'ruby'), (4, 'go'), (5, 'C#')]

3. Summary

Seeing this, I'm almost done sharing for the day.

A total of six commonly used built-in functions are shared today:

  • Lamdba Functions
  • Map function
  • Filter Functions
  • Reduce function
  • Enumerate Functions
  • Zip Functions

In practice, these are very real and commonly used functions.

So, these are all necessary knowledge as well.

To this point this article on the six Python3 in the most widely used built-in function summarizes the article is introduced to this, more related Python3 built-in function content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!