Hi, MY name is J.J. And as a data worker, we use Python every day for most of our work. Along the way, we will continue to learn some useful tips and tricks.

I’ve tried to share some of these tips in an A-Z format here, and briefly describe them in this article. If you’re interested in one or more of them, you can check out the official documentation in resources at the end of this article. I hope it helps.

all or any

One of the many reasons the Python language is so popular is that it is very readable and expressive.

People often joke that Python is executable pseudocode. When you can write code like this, it’s hard to argue.

X = [True, True, False] if any(x): print(" at least one True") if all(x): print(" all True") if any(x) and not all(x): Print (" at least one True and one False")Copy the code

bashplotlib

Have you ever thought about drawing graphics in the console?

Bashplotlib is a Python library that helps us draw data on the command line (in a rough environment).

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

collections

Python has some great default data types, but sometimes they don’t behave exactly as you’d expect.

Fortunately, the Python standard library provides the Collections module [1]. This handy add-on gives you more data types.

From Collections import OrderedDict, Counter # Remember the order in which keys are added! X = OrderedDict(a=1, b=2, c=3) )Copy the code

dir

Ever wonder how to look inside a Python object and see what properties it has? On the command line, enter:

dir() 
dir("Hello World") 
dir(dir)
Copy the code

emoji

Emoji [3] is a visual emotional symbol used in Wireless communication in Japan. “picture” refers to pictures, while “text” refers to characters. It can be used to represent various expressions, such as smiling face for laughter, cake for food, etc. In mainland China, they are commonly known as “little yellow faces”, or emoji.

Emojize print(emojize(":thumbs_up:"))Copy the code

from future import

As a result of Python’s popularity, there are always new versions under development. New releases mean new features — unless your version is obsolete.

But don’t worry. Using the __future__ module [4] can help you import functionality with future versions of Python. Literally, it’s like time travel or magic or something

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

geogy

Geography is a challenging field for most programmers. There are also problems in obtaining geographic information or drawing maps. This Geopy module [5] makes georelated content very easy.

pip install geopy
Copy the code

It works by abstracting an API for a number of different geocoding services. It allows you to get a place’s full street address, 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)
print(location.address)
print(location.location)
Copy the code

howdoi

When you program with terminal terminal, you will search for the answer on StackOverflow after encountering a problem, and then go back to the terminal to continue programming. Sometimes you don’t remember the solution you found before, and you need to check StackOverflow again without leaving the terminal. At this point you need the useful command line tool howdoi[6].

pip install howdoi
Copy the code

No matter what questions you have, you can ask it and it will try to reply.

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

But be warned — it grabs code from StackOverflow’s best answer. It may not always provide the most useful information……

howdoi exit vim
Copy the code