I've been programming in Python for many years, and even today I'm amazed at how clean the language can make code appear and how well it applies DRY programming principles. Over the years, I have learned a lot of tips and knowledge, most of which were obtained by reading popular open source software, such as Django, Flask, and Requests.
The techniques I have selected below are often overlooked by people, but they can really help us a lot in daily programming.
1. Dictionary comprehensions and Set comprehensions
Most Python programmers know and have used list comprehensions. If you're not familiar with the concept of list comprehensions - a list comprehension is a shorter, more concise way of creating a list.
>>> some_list = [1, 2, 3, 4, 5] >>> another_list = [ x + 1 for x in some_list ] >>> another_list [2, 3, 4, 5, 6]
Since Python 3.1 (and even Python 2.7), we can use the same syntax to create sets and dictionaries:
>>> # Set Comprehensions >>> some_list = [1, 2, 3, 4, 5, 2, 5, 1, 4, 8] >>> even_set = { x for x in some_list if x % 2 == 0 } >>> even_set set([8, 2, 4]) >>> # Dict Comprehensions >>> d = { x: x % 2 == 0 for x in range(1, 11) } >>> d {1: False, 2: True, 3: False, 4: True, 5: False, 6: True, 7: False, 8: True, 9: False, 10: True}
In the first example, we create a set with unique elements based on some_list, and the set only contains even numbers. In the example of the dictionary table, we created a key that is a non-repeating integer between 1 and 10, and the value is a Boolean type that indicates whether the key is an even number.
Another thing worth noting here is the literal representation of sets. We can simply create a collection using this method:
>>> my_set = {1, 2, 1, 2, 3, 4} >>> my_set set([1, 2, 3, 4])
There is no need to use the built-in function set().
2. Use Counter counting object when counting.
This sounds obvious, but is often forgotten. Counting something is a common task for most programmers, and in most cases not very challenging - here are a few ways to make it easier.
Python’s collections class library has a built-in subclass of the dict class, which is specially designed to do this kind of thing:
>>> from collections import Counter >>> c = Counter('hello world') >>> c Counter({'l': 3, 'o': 2, ' ': 1, 'e': 1, 'd': 1, 'h': 1, 'r': 1, 'w': 1}) >>> c.most_common(2) [('l', 3), ('o', 2)]
3. Beautifully print out JSON
JSON is a very good form of data serialization and is widely used by various APIs and web services today. Using Python's built-in json processing can make the JSON string readable to a certain extent, but when encountering large data, it appears as a long, continuous line, which is difficult for the human eye to view.
In order to make JSON data more friendly, we can use the indent parameter to output beautiful JSON. This is especially useful when programming interactively at the console or logging:
>>> import json >>> print(json.dumps(data)) # No indention {"status": "OK", "count": 2, "results": [{"age": 27, "name": "Oz", "lactose_intolerant": true}, {"age": 29, "name": "Joe", "lactose_intolerant": false}]} >>> print(json.dumps(data, indent=2)) # With indention { "status": "OK", "count": 2, "results": [ { "age": 27, "name": "Oz", "lactose_intolerant": true }, { "age": 29, "name": "Joe", "lactose_intolerant": false } ] }
Similarly, using the built-in pprint module can also make anything else print more beautifully.
4. Create a one-time, fast small web service
Sometimes, we need to do some simple, very basic RPC-like interactions between two machines or services. We want to use program B to call a method in program A in a simple way - sometimes on another machine. Internal use only.
I do not encourage the use of the methods described here for non-internal, one-off programming. We can use a protocol called XML-RPC (corresponding to this Python library) to do this kind of thing.
The following is an example of using the SimpleXMLRPCServer module to build a fast small file reading server:
from SimpleXMLRPCServer import SimpleXMLRPCServer def file_reader(file_name): with open(file_name, 'r') as f: return f.read() server = SimpleXMLRPCServer(('localhost', 8000)) server.register_introspection_functions() server.register_function(file_reader) server.serve_forever()
Client:
import xmlrpclib proxy = xmlrpclib.ServerProxy('http://localhost:8000/') proxy.file_reader('/tmp/secret.txt')
In this way, we get a remote file reading tool, with no external dependencies and only a few lines of code (of course, without any security measures, so you can only do this at home).
5. Python’s amazing open source community
The several things I mentioned here are all in the Python standard library. If you have Python installed, you can already use it in this way. For many other types of tasks, there are a large number of community-maintained third-party libraries you can use.
The following list is what I consider necessary for a useful and robust open source library:
A good open source library must...
Include a clear permission statement that applies to your use case.
The development and maintenance work is active (or, you can participate in the development and maintenance of it.)
Can be easily installed or deployed repeatedly using pip.
Have a test suite with adequate test coverage.
If you find a good library that meets your requirements, don't be embarrassed - most open source projects welcome code donations and help - even if you are not a Python master.
Original link: Improving Your Python Productivity
The above is the detailed content of Introducing efficient programming techniques in Python. For more information, please follow other related articles on the PHP Chinese website!

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version
SublimeText3 Linux latest version

Atom editor mac version download
The most popular open source editor

SublimeText3 Chinese version
Chinese version, very easy to use