current position:Home>24 useful Python tips

24 useful Python tips

2022-02-01 18:40:50 Machine Learning Institute

This article is published in my official account 『 data STUDIO』:24 A good one Python Practical skills

Python Is the most popular in the world 、 One of the most popular programming languages , Yes, there are many reasons .

  • It's easy to learn
  • There are super many functions
  • It has a large number of modules and Libraries

As a data worker , We use it every day Python Handle most of the work . In the process , We will continue to learn some useful skills and tricks .

ad locum , I tried to A - Z Share some of these tips in the beginning format , And these methods are briefly introduced in this paper , If you are interested in one or more of them , You can check the official documents through the references at the end of the article . I hope it can help you .

all or any

Python One of the many reasons why languages are so popular , Because it has good readability and expressiveness .

People often joke that Python yes Executable pseudocode . When you can write code like this , It's hard to refute .

x = [True, True, False]
if any(x):
    print(" At least one True")
if all(x):
    print(" Is full of True")
if any(x) and not all(x):
    print(" At least one True And a False")
 Copy code 


Have you ever thought about drawing graphics in the console ?

Bashplotlib It's a Python library , He can help us on the command line ( A rough environment ) Draw data in .

#  Module installation 
pip install bashplotlib
#  Draw examples 
import numpy as np
from bashplotlib.histpgram import plot_hist
arr = np.ramdom.normal(size=1000, loc=0, scale=1)
plot_hist(arr, bincount=50)
 Copy code 


Python There are some great default data types , But sometimes their behavior doesn't exactly meet your expectations .

Fortunately, ,Python The standard library provides collections modular . This convenient add-on provides you with more data types .

from collections import OrderedDict, Counter
#  Remember the order in which keys are added !
x = OrderedDict(a=1, b=2, c=3)
#  Count the frequency of each character 
y = Counter("Hello World!")
 Copy code 


Have you ever thought about how to check Python Object and see what properties it has ? Enter... On the command line :

dir("Hello World") 
 Copy code 

When running interactively Python And dynamically explore the objects and modules you are using , This can be a very useful feature . Read more here functions Related content .


emoji It is a visual emotional symbol used in wireless communication in Japan , Drawing refers to drawing , Words refer to characters , Can be used to represent a variety of expressions , A smiling face means a smile 、 Cake means food, etc . On Chinese mainland ,emoji Is often called “ Little yellow face ”, Or call it emoji.

#  Install the module 
pip install emoji
#  Make a try 
from emoji import emojize
 Copy code 

from __future__ import

Python One of the results of popularity , There are always new versions under development . The new version means new features —— Unless your version is out of date .

But don't worry . Use this __future__ modular Can help you use Python Future version import function . Literally , It's like time travel 、 Magic or something .

from __future__ import print_function
print("Hello World!")
 Copy code 


Geography , This is a challenging area for most programmers . When getting geographic information or drawing maps , There will be many problems . This geopy modular Make geography related content very easy .

pip install geopy
 Copy code 

It abstracts a series of different geocoding services API Come to work . Through it , You can get the full street address of a place 、 latitude 、 Longitude and even altitude .

There is also a useful distance class . It calculates the distance between two positions in your preferred unit of measurement .

from geopy import GoogleV3
place = "221b Baker Street, London"
location = GoogleV3().geocode(place)
 Copy code 


When you use terminal When programming the terminal , By having a problem in StackOverflow Search for answers on , After that, it will return to the terminal to continue programming , Sometimes you don't remember the solution you found before , You need to review StackOverflow, But I don't want to leave the terminal , Then you need to use this useful command line tool howdoi.

pip install howdoi
 Copy code 

Whatever problems you have , You can ask it , It will try its best to reply .

howdoi vertical align css
howdoi for loop in java
howdoi undo commits in git
 Copy code 

But please pay attention to —— It will be from StackOverflow Grab the code from the best answer . It may not always provide the most useful information ......

howdoi exit vim
 Copy code 


Python Of inspect modular Perfect for understanding what's going on behind the scenes . You can even call its own methods !

The following code example inspect.getsource() For printing your own source code . inspect.getmodule() It is also used to print the module that defines it .

The last line of code prints its own line number .

import inspect
 Copy code 

Of course , Except for these trivial uses ,inspect Modules can prove useful for understanding what your code is doing . You can also use it to write self documenting code .


Jedi Library is an autocomplete and code analysis library . It makes writing code faster 、 More efficient .

Unless you're developing your own IDE, Otherwise you may use Jedi I'm interested in being an editor plug-in . Fortunately, , There are already loads available !


When learning any language , There will be many milestones . Use Python And understand the mysterious **kwargs Grammar may count as an important milestone .

Double asterisk in front of dictionary object **kwargs Allows you to pass the contents of the dictionary as named parameters to the function .

The key of the dictionary is the parameter name , Value is the value passed to the function . You don't even need to call it kwargs

dictionary = {"a": 1, "b": 2}
def someFunction(a, b):
    print(a + b)
#  These do the same thing :
someFunction(a=1, b=2)
 Copy code 

When you want to write a function that can handle undefined named parameters , It's very useful .

list (list) The derived type

About Python Programming , One of my favorite things is its List derivation .

These expressions make it easy to write very smooth code , Almost like natural language .

numbers = [1,2,3,4,5,6,7]
evens = [x for x in numbers if x % 2 is 0]
odds = [y for y in numbers if y not in evens]
cities = ['London', 'Dublin', 'Oslo']

def visit(city):
    print("Welcome to "+city)
for city in cities:
 Copy code 


Python Support functional programming with many built-in features . The most useful map() One of the functions is the function —— Especially with lambda function When used in combination .

x = [1, 2, 3] 
y = map(lambda x : x + 1, x)
#  Print out  [2,3,4]
 Copy code 

In the example above ,map() Put a simple lambda Function applied to x. It returns a mapping object , The object can be converted into some iteratable objects , For example, list or tuple .


If you haven't seen it yet , Then be ready to be Python newspaper module The module shocked to . It enables you to retrieve news articles and related metadata from a series of leading international publications . You can retrieve images 、 Text and author's name . It even has some Built in NLP function .

therefore , If you are considering using in your next project BeautifulSoup Or something else DIY Web crawling Library , Using this module can save you a lot of time and energy .

pip install newspaper3k
 Copy code 

Operator overloading

Python Provide right Operator overloaded Support , This is one of the terms that makes you sound like a legitimate computer scientist .

This is actually a simple concept . Have you ever thought about why Python Allow you to use + Operator to add numbers and connection strings ? This is what operator overloading does .

You can define how to use... In your own specific way Python The object of the standard operator symbol . And you can use them in the context of the object you are using .

class Thing:
    def __init__(self, value):
        self.__value = value
    def __gt__(self, other):
        return self.__value > other.__value
    def __lt__(self, other):
        return self.__value < other.__value
something = Thing(100)
nothing = Thing(0)
# True
something > nothing
# False
something < nothing
# Error
something + nothing
 Copy code 


Python Default print The function does its job . But if you try to use print Function prints out any large nested objects , The result is rather ugly . The beautiful printing module of this standard library pprint You can print out complex structured objects in an easy to read format .

This is anything that uses non trivial data structures Python A must-have for developers .

import requests
import pprint
url = ''
users = requests.get(url).json()
 Copy code 


Python Standard library Queue The module implementation supports multithreading . This module allows you to implement queue data structures . These are data structures that allow you to add and retrieve entries according to specific rules .

“ fifo ”(FIFO) Queues allow you to retrieve objects in the order they are added .“ Last in, first out ”(LIFO) Queues allow you to access recently added objects first .

Last , Priority queues allow you to retrieve objects according to their sort order .

This is a how in Python Queue is used in Queue Examples of multithreaded programming .


stay Python When a class or object is defined in , Provides an... That represents the object as a string “ official ” This method is very useful . for example :

>>> file = open('file.txt', 'r') 
>>> print(file) 
<open file 'file.txt', mode 'r' at 0x10d30aaf0>
 Copy code 

This makes it easier to debug code . Add it to your class definition , As shown below :

class someClass: 
    def __repr__(self): 
        return "<some description here>"
someInstance = someClass()
#  Print  <some description here>
 Copy code 


Python It's a great scripting language . Sometimes standard os and subprocess Ku may have a headache .

The SH library Allows you to call any program like a normal function —— Useful for automating workflows and tasks .

import sh
sh.echo('This is great!')
 Copy code 

Type hints

Python It is a dynamically typed language . Defining variables 、 function 、 Class does not need to specify the data type . This allows fast development time . however , Nothing is more annoying than runtime errors caused by simple input problems .

from Python 3.5 Start , You can choose to provide type hints when defining functions .

def addTwo(x : Int) -> Int:
    return x + 2
 Copy code 

You can also define type aliases .

from typing import List
Vector = List[float]
Matrix = List[Vector]
def addMatrix(a : Matrix, b : Matrix) -> Matrix:
  result = []
  for i,row in enumerate(a):
    result_row =[]
    for j, col in enumerate(row):
      result_row += [a[i][j] + b[i][j]]
    result += [result_row]
  return result
x = [[1.0, 0.0], [0.0, 1.0]]
y = [[2.0, 1.0], [0.0, -2.0]]
z = addMatrix(x, y)
 Copy code 

Although not mandatory , But type annotations can make your code easier to understand .

They also allow you to use type checking tools , Catch those stray before running TypeError. If you are dealing with large 、 Complex projects , It's very useful !


adopt Python Standard library uuid modular Generate universal unique ID( or “UUID”) A quick and simple method .

import uuid
user_id = uuid.uuid4()
 Copy code 

This will create a random 128 Digit number , That number is almost certainly unique . in fact , Can generate more than 2¹²² It's possible UUID. This is more than five decimal ( or 5,000,000,000,000,000,000,000,000,000,000,000,000).

The probability of finding duplicates in a given set is very low . Even if there are a trillion UUID, The possibility of repetition is also far less than one in a billion .

Virtual environments

You may be in multiple... At the same time Python Work on projects . Unfortunately , Sometimes two projects will depend on different versions of the same dependency . What did you install on your system ?

Fortunately, ,Python Support for A virtual environment So you can have the best of both worlds . From the command line :

python -m venv my-project 
source my-project/bin/activate 
pip install all-the-modules
 Copy code 

Now? , You can run on the same machine Python Stand alone version and installation of .


Wikipedia has a great API, It allows users to programmatically access unparalleled and completely free knowledge and information . stay wikipedia modular Make access to the API Very convenient .

import wikipedia
result ='freeCodeCamp')
for link in result.links:
 Copy code 

Just like the real site , The module provides multilingual support 、 Page disambiguation 、 Random page search , There's even one donate() Method .


Humor is Python A key feature of language , It is based on English comedy skits Python Flying Circus Named .Python Many official documents cite the program's most famous sketches . however ,Python Humor is not limited to documents . Try running the following line :

import antigravity
 Copy code 


YAML refer to “ Unmarked language ” . It's a data format language , yes JSON Superset .

And JSON Different , It can store more complex objects and reference its own elements . You can also write notes , Make it especially suitable for writing configuration files . The PyYAML modular You can use YAML Use Python.

Install and then import into your project :

pip install pyyaml

import yaml
 Copy code 

PyYAML Allows you to store... Of any data type Python object , And any instances of user-defined classes .


The finale is also a great module . Have you ever encountered the need to form a dictionary from two lists ?

keys = ['a', 'b', 'c']
vals = [1, 2, 3]
zipped = dict(zip(keys, vals))
 Copy code 

The zip() Built in functions require a series of iteratable objects , And return a tuple list . Each tuple groups the elements of the input object according to the location index .

You can also call objects to “ decompression ” object *zip().

At the end

Python It is a very diverse and well developed language , So there will certainly be many functions I haven't considered . If you want to know more about python modular , You can refer to awesome-python.

Copyright notice

Welcome to share , Please indicate the author and the original link in the article , Thank you for your respect for knowledge and affirmation of this article .

Original author : data STUDIO
Link to the original text…

The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source , Relevant responsibilities will be investigated for infringement reprint

copyright notice
author[Machine Learning Institute],Please bring the original link to reprint, thank you.

Random recommended