current position:Home>8 practical Python skills that are easy to use and don't have to suffer a loss for half a year

8 practical Python skills that are easy to use and don't have to suffer a loss for half a year

2022-02-02 02:51:00 jiejie

Hello everyone , I'm Jay 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 [1]. 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 


emoji[3]  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 [4] 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 [5] 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[6].

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 

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

Random recommended