Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the useful Python built-in functions

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what are the useful Python built-in functions". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1 ️set ()

The set () function comes in handy when you need to deduplicate a list.

Set ([iterable]) is used to create a collection. The elements in the collection are unordered and cannot be repeated.

After you create a collection object, you can also use union, intersection, and difference functions.

2 ️eval ()

Someone asked how to write a calculator in python, enter a string formula, and then produce the result directly.

Eval (expression [, globals [, locals]]) converts a string to an expression and executes it.

This function takes three parameters:

Expression (which contains a string to evaluate as an Python expression)

Global variables (the other two parameters are optional and contain global / local methods and variables.)

Local variable

We can also call methods and functions through the eval () function

You can change the start index by specifying the index (integer) in the second parameter (start)

3 ️sorted ()

In the process of processing data, we often use sorting operations, such as forward / reverse sorting of elements in lists, dictionaries, and tuples.

At this point, you need sorted (), which can sort any iterable object and return a list.

List of operations in ascending order:

Reverse tuple operation:

Use the parameter: key, sorted by string length, according to custom rules, for example:

Sort the tuple list according to custom rules:

4 ️reversed ()

If you need to reverse the elements of a sequence, the reversed () function can help you.

Reversed () accepts the sequence, reverses the elements in the sequence, and finally returns the iterator.

5 ️map ()

When dealing with text, assume that you want to capitalize the conversion of each word in the sequence.

At this point, you can use the map () function.

Map () maps the specified sequence based on the function provided and returns to the iterator.

For example, square each number in the list:

6 ️bin ()

The bin (x) function converts integers to binary strings prefixed with'0b'.

Like slicing number [2:], you can simply extract binary numbers by slicing strings.

Throws a TypeError exception highlighting that if no integer is specified, the type cannot be interpreted as an integer

7 ️filter ()

What if I want to remove even numbers from the list of numbers?

Now the filter (function,iterable) function is starting to work. It takes a function and an Iterable as input and returns elements from the Iterable that the function returns True.

The filter () function makes it easy to accomplish the task. It is used to filter sequences, filter elements that do not meet the criteria, and return iterator objects.

The filter () function is similar to the map () and reduce () functions in that it maps each element in the sequence to a function and returns the result.

The filter function accepts any iterable type (such as a list, string, tuple, or dictionary) as the second argument

Let's try again how to choose a word that contains the letter "b" from many words.

8 ️contacts enumerate ()

If this occurs, each element in the sequence and its corresponding serial number will be printed at the same time. We use the enumerate () function as the appearance.

The enumerate () function is used to sequentially mark elements in a sequence and returns an iterator consisting of (element,index).

As another example, mark a string and return each letter and its index.

Enumerated objects support iterative protocols, so we can use the next () function

This is the end of the content of "what are the useful Python built-in functions"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report