What can Python do with just one line of code?

Preface

The text and pictures in this article are from the Internet and are for learning and communication purposes only. They do not have any commercial use. If you have any questions, please contact us for processing.

PS: If you need Python learning materials, you can click on the link below to get it yourself

Python free learning materials and group communication answers Click to join


Today we are going to have fun, with only one line of Python code or commands to see what can be played. For example, we have played with a line of Python commands to implement http services:

HTTP server

FTP server

Let me introduce you again, you can use pyftpdlib to directly implement an FTP server for file transfer:

 

Format Json

Sometimes you may encounter Json data displayed in a mess like this:

 

 

A little upset? You can use json.tool to format Json:

 

python -c

Use this -c parameter to use Python simple code directly in the terminal:

 

One line of code to implement the function

Using lambda, you can implement an anonymous function with one line of code. For example, if you want to perform calculation operations on the elements in the list, you can directly do this:

 

Variable exchange

A line of code exchanges two variables, it's a commonplace:

 

One-line code generation list

When you need to calculate some values ​​frequently and then append to an empty list, it is more convenient to do this directly:

 

 

You can also read files in this way:

 


You can use pprint for better output:

Analyze performance

You can use cProfile to analyze your py performance:

 

if..in..else

Sometimes some simple judgments can be done directly with a line of code. For example, if you want to judge whether an element is in the list, you can perform the corresponding operation according to whether it is or not:

 

ok, the above are more commonly used, I hope it will help you, do you know any small operations that can be achieved with a line of code or commands?

Guess you like

Origin blog.csdn.net/pythonxuexi123/article/details/112973714