Python’s 5 super practical skills, there are definitely things you can’t think of!

Article directory

  • Preface
  • 1. Dictionary comprehensions and Set comprehensions
  • 2. Use Counter counting object when counting
  • 3. Beautifully print out JSON
  • 4. Create small, one-off, fast web services
  • 5. Python’s amazing open source community
      • About Python technical reserves
        • 1. Learning routes in all directions of Python
        • 2. Python basic learning video
        • 3. Excellent Python learning books
        • 4. Python toolkit + project source code collection
        • ①Python toolkit
        • ②Python practical case
        • ③Python mini game source code
        • 5. Interview materials
        • 6. Python part-time channels

Foreword

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 this way:

>>> 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.

There is a built-in subclass of the dict class in Python’s collections library, which is specifically 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 user-friendly, we can use the indent parameter to output beautiful JSON. This is especially useful when programming or logging interactively at the console:

>>> 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 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

Several of the things I mentioned here are 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.
  • Development and maintenance work is active (or, you can participate in developing and maintaining 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 expert.

About Python technical reserves

Learning Python well is good whether you are getting a job or doing a side job to make money, but you still need to have a learning plan to learn Python. Finally, we share a complete set of Python learning materials to give some help to those who want to learn Python!

CSDN gift package: “Python introductory information & amp; practical source code & amp; installation tools] free of charge (Safe link, click with confidence)

1. Learning routes in all directions of Python

The technical points in all directions of Python are organized to form a summary of knowledge points in various fields. Its usefulness is that you can find corresponding learning resources according to the above knowledge points to ensure that you learn more comprehensively.

2. Python basic learning video

② Route corresponding learning video

There are also many learning videos suitable for beginners. With these videos, you can easily get started with Python ~ Insert picture description here

③Practice questions

After each video lesson, there are corresponding exercises to test your learning results haha!

Due to limited space, only part of the information is shown

3. High-quality Python learning books

When I learn a certain basic and have my own understanding ability, I will read some books or handwritten notes compiled by my seniors. These notes record their understanding of some technical points in detail. These understandings are relatively unique and can be learned. to a different way of thinking.

4. Python toolkit + project source code collection
①Python toolkit

The commonly used development software for learning Python is here! Each one has a detailed installation tutorial to ensure you can install it successfully!

②Python practical case

Optical theory is useless. You must learn to type code along with it and practice it in order to apply what you have learned to practice. At this time, you can learn from some practical cases. 100+ practical case source codes are waiting for you!

③Python mini game source code

If you feel that the practical cases above are a bit boring, you can try writing your own mini-game in Python to add a little fun to your learning process!

5. Interview materials

We must learn Python to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and Alibaba bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.

6. Python part-time channels

Moreover, after learning Python, you can also take orders and make money on major part-time platforms. I have compiled various part-time channels + part-time precautions + how to communicate with customers into documents.


This complete version of the complete set of Python learning materials has been uploaded to CSDN. If you need it, friends can scan the CSDN official certification QR code below on WeChat to get it for free [Guaranteed 100% Free]