从A到Z,26个实用Python模块/函数速览

2018-10-31 14:05:00
刘大牛
转自文章
230

花下猫说:今天听了左耳朵耗子的《左耳听风》专栏,我受到启发,所以尝试转载一篇英文技术文章和大家分享。获取第一手的信息源,锻炼英文阅读能力,以期长足的技术进步。文末也附上了一篇翻译文章的链接,方便大家对照阅读。这种形式是一个尝试,若你觉得有帮助, 麻烦在文末点个赞,这样我会更有动力,继续采集优秀的英文技术文章与大家共读。

原文地址:http://t.cn/RFbYlD1

Python is one of the world’s most popular, in-demand programming languages. This is for many reasons:

  • it’s easy to learn

  • it’s super versatile

  • it has a huge range of modules and libraries

I use Python daily as an integral part of my job as a data scientist. Along the way, I’ve picked up a few useful tricks and tips.

Here, I’ve made an attempt at sharing some of them in an A-Z format.

Most of these ‘tricks’ are things I’ve used or stumbled upon during my day-to-day work. Some I found while browsing the Python Standard Library docs. A few others I found searching through PyPi.

However, credit where it is due — I discovered four or five of them over at awesome-python.com. This is a curated list of hundreds of interesting Python tools and modules. It is worth browsing for inspiration!

all or any

One of the many reasons why Python is such a popular language is because it is readable and expressive.

It is often joked that Python is ‘executable pseudocode ’. But when you can write code like this, it’s difficult to argue otherwise:

x = [True, True, False]

                
if any(x):
    print("At least one True")

                
if all(x):
    print("Not one False")

                
if any(x) and not all(x):
    print("At least one True and one False")

                

bashplotlib

You want to plot graphs in the console?

$ pip install bashplotlib

                

You can have graphs in the console.

collections

Python has some great default datatypes, but sometimes they just won’t behave exactly how you’d like them to.

Luckily, the Python Standard Library offers the collections module. This handy add-on provides you with further datatypes.

from collections import OrderedDict, Counter

                
# Remembers the order the keys are added!
x = OrderedDict(a=1, b=2, c=3)

                
# Counts the frequency of each character
y = Counter("Hello World!")

                

dir

Ever wondered how you can look inside a Python object and see what attributes it has? Of course you have.

From the command line:

>>> dir()
>>> dir("Hello World")
>>> dir(dir)

                

This can be a really useful feature when running Python interactively, and for dynamically exploring objects and modules you are working with.

Read more here.

emoji

Yes, really.

$ pip install emoji

                

Don’t pretend you’re not gonna try it out…

from emoji import emojize

                
print(emojize(":thumbs_up:"))

                

发表评论
评论通过审核后显示。
文章分类
联系我们
联系人: 透明七彩巨人
Email: weok168@gmail.com
© 2013- 2024 透明七彩巨人-tmqcjr.com   
SQL查询: 26
内存占用: 8.00MB
PHP 执行时间: 0.3