初学者的python技巧和窍门

In this post, we’d like to share 10 useful Python tips and tricks for beginners with you.

在本文中,我们想与您分享10个有用的Python技巧和窍门。

Python is one of the most in-demand skills for data scientists. Besides providing a free Python course for beginners, we also summarize these 10 tips and tricks, which should help you with daily data science tasks.

Python是数据科学家最需要的技能之一。 除了为初学者提供免费的Python课程之外,我们还总结了这10个技巧和窍门,这些技巧和窍门将帮助您完成日常数据科学任务。

Following this beginners’ tutorial, you’ll learn how to:

遵循本入门教程,您将学习如何:

  • format strings

    格式化字符串
  • use enumerate, sorted functions

    使用枚举,排序函数
  • return multiple values from a function

    从函数返回多个值
  • use lambda expression, list comprehensions

    使用lambda表达式,列表理解
  • Lots more!

    还有更多!

If you want to make your Python coding more efficient, don’t miss these tips/tricks!

如果您想提高Python编码的效率,请不要错过这些提示/技巧!

Let’s begin.

让我们开始。

提示1:显示方法/功能 (Tip #1: Display Methods/Functions)

With Python being so powerful, there are many different methods and functions. It’s especially hard for beginners to memorize all.

由于Python如此强大,因此有许多不同的方法和功能。 初学者很难记住所有内容。

How do we find out the available methods/functions for a Python object?

我们如何找出Python对象可用的方法/功能?

The first Python tip for beginners is about two quick ways of doing this.

针对初学者的第一个Python技巧是关于两种快速的方法。

方法1:代码完成功能 (Method #1: Code completion feature)

Many IDEs (Integrated Development Environment) or code editing applications for Python can auto-complete the code while you are typing. This feature is helpful to speed up programming.

许多用于Python的IDE(集成开发环境)或代码编辑应用程序都可以在您键入时自动完成代码。 此功能有助于加快编程速度。

For example, within Jupyter Notebook, you can type in the first few characters of a function/files, etc., and then press the Tab key to fill in the rest of the item. The screenshot below shows the available autocomplete options that start with the letter ‘p’.

例如,在Jupyter Notebook中,您可以键入函数/文件的前几个字符,等等,然后按Tab键填充项目的其余部分。 以下屏幕截图显示了以字母“ p”开头的可用自动完成选项。

Python tips and tricks for beginners tab auto complete example

方法2:dir函数 (Method #2: dir function)

The dir function returns a list of valid attributes for the object in its argument, which means we can use it to return an object’s methods.

dir函数在其参数中返回该对象的有效属性列表,这意味着我们可以使用它来返回对象的方法。

For example, let’s run the below Python code to apply dir on a string object.

例如,让我们运行以下Python代码以将dir应用于字符串对象。

s = 'a string'
print(dir(s))

This will return a long list of names.

这将返回一长串名称。

Ignoring those special methods with ‘__’ at the beginning of the list, you can find interesting methods such as capitalize, find, and lower.

忽略列表开头带有“ __”的特殊方法,您会发现有趣的方法,例如大写,查找和降低。

['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

Note: dir only provides an interesting set of names more than a full list. But it’s convenient to use when you can’t recall a method that you are aware of.Besides dir, you can also try the help function. For example, help(str) will print out the Help page on the string object, which contains more details of the methods than dir.

注意dir仅提供了一个有趣的名称集,而不是完整列表。 但是,当您无法调用自己知道的方法时,使用起来很方便。除dir外,还可以尝试使用help函数。 例如,help(str)将在字符串对象上打印出“帮助”页面,该页面包含比dir更多的方法细节。

提示2:格式化字符串 (Tip #2: Format Strings)

Printing out strings is a common task within data science projects. The format method of str can piece multiple variables and strings together and format them in specific ways.

打印字符串是数据科学项目中的常见任务。 str的format方法可以将多个变量和字符串组合在一起,并以特定方式格式化它们。

Let’s see an example.

让我们来看一个例子。

We’ll define three variables price, paid, and change (=paid — price). What if we want to print out a full sentence containing these variables formatted as dollars?

我们将定义三个变量pricepaidchange (= paid — price)。 如果我们想打印出包含这些格式为美元的变量的完整句子怎么办?

We can use the format method as below.

我们可以使用以下格式方法。

price = 9.99*1.13
paid = 20
change = paid - price
print('The item cost ${0:,.2f}. I paid ${1:,.2f}. I received ${2:,.2f} in change'.format(price, paid, change))
The item cost $11.29. I paid $20.00. I received $8.71 in change

提示3:枚举函数 (Tip #3: Enumerate Function)

When iterating over an object such as a list, dictionary, or file, enumerate is a useful function. The function returns a tuple containing both the values obtained from iterating over the object, and the loop counter (from the start position of 0). The loop counter is especially handy when you want to write code based on the index.

当遍历列表,字典或文件等对象时, 枚举是一项有用的功能。 该函数返回一个元组,其中包含从对象上迭代获得的值和循环计数器(从0的起始位置开始)。 当您要基于索引编写代码时,循环计数器特别方便。

Let’s see an example where we can do something special for the first and last element.

让我们看一个示例,其中我们可以对第一个元素和最后一个元素进行特殊处理。

lst = 'Just Into Data'
length = len(lst)
for i, element in enumerate(lst):
    print('{}: {}'.format(i, element))
    if i == 0:
        print('The first element!')
    elif i == length - 1:
        print('The last element!')

We printed strings indicating the first and the last element conveniently with enumerate.

我们方便地使用枚举打印了指示第一个和最后一个元素的字符串。

0: J
The first element!
1: u
2: s
3: t
4:
5: I
6: n
7: t
8: o
9:
10: D
11: a
12: t
13: a
The last element!

The enumerate function can also be used on files.

枚举功能也可以在文件上使用。

In the example below, we can print out the first 10 rows of the csv file before breaking out of the loop. We won’t copy the result here since it’s too long. But you can try it on any files you have.

在下面的示例中,我们可以在退出循环之前打印出csv文件的前10行。 由于时间太长,我们将不在此处复制结果。 但是您可以在任何文件上尝试使用。

with open('sberbank.csv') as f:
    for i, line in enumerate(f):
        if i == 10:
            break
        print(line)

提示4:在函数中返回多个值 (Tip #4: Return Multiple Values in a Function)

When defining functions, we often want to return more than one value. In this Python tip/trick, we’ll cover three common methods below.

在定义函数时,我们经常要返回多个值。 在此Python技巧中,我们将在下面介绍三种常见方法。

方法1:返回一个元组 (Method #1: Return a Tuple)

First, let’s look at the most convenient way: returning a tuple. We usually only use this method if there are 2 or 3 values to return. When the number of values is more, it’s easy to forget about the order of the values within the tuple.

首先,让我们看一下最方便的方法:返回一个元组。 我们通常仅在要返回2或3个值的情况下才使用此方法。 当值的数量更多时,很容易忘记元组中值的顺序。

Below is an example function get_employee, which returns the employee’s first and last name as tuples, based on their ID numbers.

下面是示例函数get_employee,该函数根据员工的ID号返回员工的名字和姓氏。

# returning a tuple.
def get_employee(id_num):
    if id_num == 0:
        return 'Jane', 'Doe'
    elif id_num == 1:
        return 'John', 'Smith'
    else:
        raise Exception('No employee with this id: {}'.format(id_num))

If we call the function with value of 0, you can see that the function returns the tuple with two values: ‘Jane’ and ‘Doe’.

如果我们调用值为0的函数,则可以看到该函数返回具有两个值的元组:“ Jane”和“ Doe”。

employee = get_employee(0)
print('first_name: {}, last_name: {}'.format(employee[0], employee[1]))
first_name: Jane, last_name: Doe

This is great, but what should we do when there are more values to return?

很好,但是当有更多值要返回时,我们该怎么办?

Let’s move on to the next method.

让我们继续下一个方法。

方法2:返回字典 (Method #2: Return a Dictionary)

The second way is to return a dictionary. Dictionaries can be thought of as key: value pairs, so we can name the values that are returned, which is more clear than tuples.

第二种方法是返回字典。 字典可以认为是键:值对,因此我们可以命名返回的值,这比元组更清晰。

The example below is similar to the previous one. But we ask the function to return a dictionary.

下面的示例与上一个示例相似。 但是我们要求函数返回字典。

# returning a dictionary
def get_employee(id_num):
    if id_num == 0:
        return {'first_name': 'Jane', 'last_name': 'Doe', 'title': 'Data Scientist', 'department': 'A', 'date_joined': '20190808'}
    elif id_num == 1:
        return {'first_name': 'John', 'last_name': 'Smith', 'title': 'Data Engineer', 'department': 'B', 'date_joined': '20190808'}
    else:
        raise Exception('No employee with this id: {}'.format(id_num))

We can call the function with id_num = 0. With the result as a dictionary, it’s easier to call the specific value with its key.

我们可以使用id_num = 0调用该函数。将结果作为字典,使用其键调用特定值会更容易。

employee = get_employee(0)
print('first_name: {},\nlast_name: {},\ntitle: {},\ndepartment: {},\ndate_joined: {}'.format(
    employee['first_name'], employee['last_name'], employee['title'], employee['department'], employee['date_joined']))
first_name: Jane,
last_name: Doe,
title: Data Scientist,
department: A,
date_joined: 20190808

方法#3:返回一个NamedTuple (Method #3: Return a NamedTuple)

The last way we’ll discuss is returning a namedtuple. The named tuples are tuples with named fields. They are immutable like tuples, but also provide naming like dictionaries.

我们将讨论的最后一种方法是返回一个namedtuple。 命名元组是具有命名字段的元组。 它们像元组一样是不可变的,但也像字典一样提供命名。

Named tuples assign meaning to each position in a tuple and allow for more readable, self-documenting code. They can be used wherever regular tuples are used, and they add the ability to access fields by name instead of position index.

命名元组将含义分配给元组中的每个位置,并允许使用更具可读性的自记录代码。 它们可以在使用常规元组的任何地方使用,并且它们增加了按名称而不是位置索引访问字段的能力。

Python docs

Python文档

The main advantage named tuples have over dictionaries is that the fields are guaranteed to be in the object. With a dictionary, we’re not sure if all the key: value pairs will be there.

与字典相比,命名元组的主要优点是可以确保字段位于对象中。 对于字典,我们不确定是否所有的key:value对都会存在。

Let’s see an example of the named tuple.

让我们看一个命名元组的例子。

We have to define the namedtuple object before using it. In the code below, we created a namedtuple object called Employee, and then used it to hold values.

我们必须在使用namedtuple对象之前定义它。 在下面的代码中,我们创建了一个名为Employee的namedtuple对象,然后使用它来保存值。

# returning a namedtuple.
import collections
Employee = collections.namedtuple('Employee', ['first_name', 'last_name', 'title', 'department', 'date_joined'])


def get_employee(id_num):
    if id_num == 0:
        return Employee(first_name='Jane', last_name='Doe', title='Data Scientist', department='A', date_joined='20190808')
    elif id_num == 1:
        return Employee(first_name='John', last_name='Smith', title='Data Engineer', department='B', date_joined='20190808')
    else:
        raise Exception('No employee with this id: {}'.format(id_num))

We can call the function again with id_num = 0 to return the named tuple.

我们可以使用id_num = 0再次调用该函数以返回命名的元组。

employee = get_employee(0)
print('first_name: {},\nlast_name: {},\ntitle: {},\ndepartment: {},\ndate_joined: {}'.format(
    employee.first_name, employee.last_name, employee.title, employee.department, employee.date_joined))
first_name: Jane,
last_name: Doe,
title: Data Scientist,
department: A,
date_joined: 20190808

We can also double check the variable type of the returned object, which is the Employee namedtuple defined earlier.

我们还可以仔细检查返回对象的变量类型,即先前定义的Employee namedtuple。

type(employee)
__main__.Employee

提示5:Lambda表达 (Tip #5: Lambda Expression)

The Lambda expressions are used to create anonymous functions, which are usually single-line.

Lambda表达式用于创建匿名函数,通常为单行。

Below is an example showing how lambda can shorten the code for creating simple functions.

以下示例显示lambda如何缩短用于创建简单函数的代码。

import pandas as pd
df = pd.DataFrame(data={'address': ['12 first St', '15 Second St', '20 ThIRd St', '2 First St', '8 THIRD St', '100 fIRST st']})


# without lambda.
def get_streetname(address):
    return address.split()[1].lower()


df['address'].map(get_streetname)


# using lambda function. Shorter and more clear.
df['address'].map(lambda address: address.split()[1].lower())

Both methods return the same result below.

两种方法在下面返回相同的结果。

0     first
1 second
2 third
3 first
4 third
5 first
Name: address, dtype: object

提示6:排序函数 (Tip #6: Sorted Function)

We’ll cover the useful sorted function for this Python tip, with examples of lists and dictionaries. It’s a common task since we often want to see the top/bottom values in a dataset.

我们将通过清单和字典的示例介绍此Python技巧的有用的排序函数。 这是一项常见的任务,因为我们经常想查看数据集中的顶部/底部值。

排序清单 (Sort Lists)

Let’s look at a simple example for a list using the sorted function.

让我们看一个使用sorted函数的列表的简单示例。

lst = [5, 5, 3, 8, 1, 9]


sorted_lst = sorted(lst)
sorted_lst
[1, 3, 5, 5, 8, 9]

Note: there’s also the list.sort() method, but we prefer sorted since it’s more general and creates a new list. See more detailed comparisons in the Python docs.

注意 :还有list.sort()方法,但是我们更喜欢sorted,因为它更通用,可以创建一个新列表。 请参阅Python文档中的更多详细比较。

排序字典 (Sort Dictionaries)

For dictionaries, sorting is a little more complicated since there are keys and values.

对于字典,由于存在键和值,因此排序要稍微复杂一些。

We can apply the sorted function directly to the dictionary, which will sort the dictionary’s keys.

我们可以将已排序的函数直接应用于字典,这将对字典的键进行排序。

# Get the keys in sorted order.
d = {'T': 3, 'Q': 7, 'A': 9, 'G': 0, 'B': 8}
sorted(d)
['A', 'B', 'G', 'Q', 'T']

Or sort the values of the dictionaries as below.

或按以下方式对字典的值进行排序。

# Get the values in sorted order.
d = {'T': 3, 'Q': 7, 'A': 9, 'G': 0, 'B': 8}
sorted(d.values())
[0, 3, 7, 8, 9]

Or sort the whole dictionaries by either its keys or values in the Python code below.

或按下面的Python代码中的键或值对整个词典进行排序。

# sort the dictionary by key.
{k:v for k,v in sorted(d.items())}


# sort the dictionary by value.
{k:v for k,v in sorted(d.items(), key=lambda it: it[1])}
{'A': 9, 'B': 8, 'G': 0, 'Q': 7, 'T': 3}
{'G': 0, 'T': 3, 'Q': 7, 'B': 8, 'A': 9}

提示7:条件表达式 (Tip #7: Conditional Expressions)

If you’ve learned the basics of Python, you should be familiar with the if-else statements. When the logic is simple, we can also use the conditional expression (or ternary operator) in one line.

如果您已经了解了Python的基础知识,则应该熟悉if-else语句。 当逻辑很简单时,我们也可以在一行中使用条件表达式 (或三元运算符)。

Let’s see an example based on the below boolean variable is_raining.

让我们看一个基于以下布尔变量is_raining的示例。

is_raining = True

The Python code below shows the traditional way of doing it.

下面的Python代码显示了执行此操作的传统方式。

if is_raining:
    action = 'Stay at home'
else:
    action = 'Go for a walk'
print(action)
Stay at home

Yet, we can also use the expression below. It’s much shorter!

但是,我们也可以使用下面的表达式。 它要短得多!

The expression x if C else y first evaluates the condition, C rather than x. If C is true, x is evaluated and its value is returned; otherwise, y is evaluated and its value is returned.

x if C else y首先计算条件C,而不是x, x if C else y表达式x。 如果C为true,则对x求值并返回其值; 否则,将评估y并返回其值。

Python docs

Python文档

action = 'Stay at home' if is_raining else 'Go for a walk'
print(action)

提示8:清单理解 (Tip #8: List Comprehensions)

We can create lists using list comprehensions, which is much more compact than the traditional method. It’s commonly used when each element of the new list results from some operations on the element of another iterable object.

我们可以使用列表理解来创建列表,它比传统方法紧凑得多。 当新列表的每个元素来自对另一个可迭代对象的元素的某些操作产生时,通常使用它。

A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it.

列表推导由包含表达式的方括号组成,后跟一个for子句,然后是零个或多个forif子句。 结果将是一个新列表,该列表是通过在其后的forif子句的上下文中评估表达式而得出的。

Python docs

Python文档

The example shows how to use it counting the length of words in a string and assign the result as a list.

该示例说明如何使用它计算字符串中单词的长度并将结果分配为列表。

cat_in_the_hat = """the sun did not shine.
it was too wet to play.
so we sat in the house
all that cold, cold, wet day.

i sat there with sally.
we sat there, we two.
and i said, 'how i wish
we had something to do!'

too wet to go out
and too cold to play ball.
so we sat in the house.
we did nothing at all.

so all we could do was to
sit!
sit!
sit!
sit!
and we did not like it.
not one little bit."""


# assume we want to count the length of each word in the string.


# long way.
len_list1 = []
for s in cat_in_the_hat.split():
    len_list1.append(len(s))
    
# using list comprehensions we can do this in one line.
len_list2 = [len(s) for s in cat_in_the_hat.split()]


print(len_list1)
print()
print(len_list2)

Both methods return the same results below, while the list comprehension code being much shorter.

两种方法在下面都返回相同的结果,而列表理解代码要短得多。

[3, 3, 3, 3, 6, 2, 3, 3, 3, 2, 5, 2, 2, 3, 2, 3, 5, 3, 4, 5, 5, 3, 4, 1, 3, 5, 4, 6, 2, 3, 6, 2, 4, 3, 1, 5, 4, 1, 4, 2, 3, 9, 2, 4, 3, 3, 2, 2, 3, 3, 3, 4, 2, 4, 5, 2, 2, 3, 2, 3, 6, 2, 3, 7, 2, 4, 2, 3, 2, 5, 2, 3, 2, 4, 4, 4, 4, 3, 2, 3, 3, 4, 3, 3, 3, 6, 4]

提示9:所有/任何功能 (Tip #9: All/Any Functions)

We’d also like to cover the all and the any functions in Python. They are convenient when making multiple comparisons.

我们还希望介绍Python中的allany函数。 当进行多个比较时,它们很方便。

The any function returns True if any element of an iterable object is true. The example below shows how it makes coding more straightforward.

如果可迭代对象的任何元素为true,则any函数将返回True。 下面的示例显示了它如何使编码更直接。

my_string = 'Toronto, Ontario'


# the long way.
if 'Montreal' in my_string or 'Toronto' in my_string or 'Vancouver' in my_string or 'Boston' in my_string or 'New York' in my_string:
    print(my_string)
    
# use the any function


# this way is shorter than the previous way.
if any([c in my_string for c in ['Montreal', 'Toronto', 'Vancouver', 'Boston', 'New York']]):
    print(my_string)
Toronto, Ontario

Similarly, the all function returns True if all elements of an iterable object are true (or if the iterable is empty). Below is an example to compare the all function and the usual method.

同样,如果可迭代对象的所有元素都为true(或者iterable为空),则all函数将返回True。 下面是比较所有功能和常用方法的示例。

my_string2 = 'Just Into Data'


# the usual way
if 'Just' in my_string2 and 'In' in my_string2 and 'Data' in my_string2:
    print(my_string2)


# use all function
if all([c in my_string2 for c in ['Just', 'Into', 'Data']]):
    print(my_string2)
Just Into Data

提示10:使用虚拟环境 (Tip #10: Use Virtual Environments)

If you are working on multiple data science projects at once, it’s critical to learn and use virtual environments.

如果您同时从事多个数据科学项目,那么学习和使用虚拟环境至关重要。

What are the virtual environments for Python?

什么是Python的虚拟环境?

A virtual environment is a Python environment such that the Python interpreter, libraries and scripts installed into it are isolated from those installed in other virtual environments, and (by default) any libraries installed in a “system” Python, i.e., one which is installed as part of your operating system.

虚拟环境是Python环境,因此安装在其中的Python解释器,库和脚本与其他虚拟环境中安装的库以及脚本(默认情况下)与安装在“系统” Python中的任何库(即已安装的库)隔离。作为操作系统的一部分。

Python docs

Python文档

You can create and manage virtual environments using tools like Anaconda (conda environments), virtualenv, pipenv.

您可以使用Anaconda(conda环境)virtualenvpipenv之类的工具来创建和管理虚拟环境。

Virtual environments allow different Python environments for each project, so that we can use different versions of Python and/or sets of libraries.

虚拟环境为每个项目提供了不同的Python环境,因此我们可以使用不同版本的Python和/或库集。

For example, let’s say we used plotly version 3 to create charts in a project. After a few months, plotly version 4 came out with new features. Since the project’s code had been working well in production, we are good to continue using Plotly version 3. But for any new projects, we’d like to use the new features in Plotly version 4.

例如,假设我们使用了plotly版本3在项目中创建图表。 几个月后,plotly version 4推出了新功能。 由于该项目的代码在生产中运行良好,因此我们很高兴继续使用Plotly版本3。但是对于任何新项目,我们都希望使用Plotly版本4中的新功能。

In this situation, we can create two virtual environments with plotly v3 and v4 separately, and use them for the old and new projects.

在这种情况下,我们可以分别使用v3和v4创建两个虚拟环境,并将它们用于新旧项目。

Problems solved!

问题解决了!

That’s it! Hope you found these Python tips and tricks for beginners useful.

而已! 希望您发现这些对初学者有用的Python技巧和窍门。

Which tip from this post do you want to try first?

您要首先尝试这篇文章的哪个技巧?

Leave a comment for any questions you may have or anything else.

对您可能有的任何其他问题发表评论。

Related resources:

相关资源

For beginners of Python, check out our pandas and numpy tutorials to continue learning:

对于Python的初学者,请查看我们的pandas和numpy教程以继续学习:

Or check out our other recent data science for beginners articles:

或查看其他有关初学者的最新数据科学文章:

For more useful data science articles, sign up our newsletter!

有关更多有用的数据科学文章,请注册我们的新闻通讯

翻译自: https://towardsdatascience.com/python-tips-and-tricks-for-beginners-62473d569d0a

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值