目录


Python Basics

Math Operators(数学运算符)

From Highest to Lowest precedence(优先级从高到低):

Operators   Operation       Example
**  Exponent        2 ** 3 = 8
%   Modulus/Remaider        22 % 8 = 6
//  Integer division        22 // 8 = 2
/   Division        22 / 8 = 2.75
*   Multiplication  3 * 3 = 9
-   Subtraction     5 - 2 = 3
+   Addition        2 + 2 = 4

下面是一些在交互shell中表达式的例子 Examples of expressions in the interactive shell:

>>> 2 + 3 * 6
20
>>> (2 + 3) * 6
30
>>> 2 ** 8
256
>>> 23 // 7
3
>>> 23 % 7
2
>>> (5 - 1) * ((7 + 1) / (3 - 1))
16.0

Data Types(数据类型)

Data Type   Examples
Integers    -2, -1, 0, 1, 2, 3, 4, 5
Floating-point numbers      -1.25, -1.0, --0.5, 0.0, 0.5, 1.0, 1.25
Strings     'a', 'aa', 'aaa', 'Hello!', '11 cats'

String Concatenation and Replication(字符串串联和复制)

String concatenation 字符串串联:

>>> 'Alice' 'Bob'
'AliceBob'

Note: Avoid + operator for string concatenation. Prefer string formatting. 注意:避免使用 + 运算符做字符串串联。优先使用字符串格式化。

String Replication 字符串复制:

>>> 'Alice' * 5
'AliceAliceAliceAliceAlice'

Variables(变量)

You can name a variable anything as long as it obeys the following three rules: 你可以把变量命名为任何东西,只要遵守以下规则。

  • It can be only one word.
  • 变量名可以只有一个单词。
  • It can use only letters, numbers, and the underscore (_) character.
  • 只能用字母、数字和下划线。
  • It can’t begin with a number.
  • 不能以数字开头。
  • Variable name starting with an underscore (_) are considered as "unuseful".
  • 以下划线开头的变量名被认为是无用的。

Example:

>>> spam = 'Hello'
>>> spam
'Hello'
>>> _spam = 'Hello'
_spam should not be used again in the code.

Comments(注释)

Inline comment: 单行注释

# This is a comment

Multiline comment: 多行注释

# This is a
# multiline comment

Code with a comment: 带注释的代码

a = 1  # initialization

Please note the two spaces in front of the comment. 注意注释前面的两个空格。

Function docstring:

def foo():
""" This is a function docstring You can also use: ''' Function Docstring ''' """

The print() Function

>>> print('Hello world!')
Hello world!
>>> a = 1
>>> print('Hello world!', a)
Hello world! 1

The input() Function

>>> print('What is your name?')   # ask for their name
>>> myName = input()
>>> print('It is good to meet you, {}'.format(myName))
What is your name?
Al
It is good to meet you, Al

The len() Function

Evaluates to the integer value of the number of characters in a string:

>>> len('hello')
5

Note: test of emptiness of strings, lists, dictionary, etc, should not use len, but prefer direct boolean evaluation.

>>> a = [1, 2, 3]
>>> if a:
>>>     print("the list is not empty!")

The str(), int(), and float() Functions

Integer to String or Float:

>>> str(29)
'29'
>>> print('I am {} years old.'.format(str(29)))
I am 29 years old.
>>> str(-3.14)
'-3.14'

Float to Integer:

>>> int(7.7)
7
>>> int(7.7) + 1
8

Flow Control

Comparison Operators

Operator    Meaning
==  Equal to
!=  Not equal to
<   Less than
>   Greater Than
<=  Less than or Equal to
>=  Greater than or Equal to

These operators evaluate to True or False depending on the values you give them. Examples:

>>> 42 == 42
True
>>> 40 == 42
False
>>> 'hello' == 'hello'
True
>>> 'hello' == 'Hello'
False
>>> 'dog' != 'cat'
True
>>> 42 == 42.0
True
>>> 42 == '42'
False

Boolean evaluation

Never use == or != operator to evaluate boolean operation. Use the is or is not operators, or use implicit boolean evaluation.

NO (even if they are valid Python):

>>> True == True
True
>>> True != False
True

YES (even if they are valid Python):

>>> True is True
True
>>> True is not False
True

These statements are equivalent:

>>> if a is True:
>>>    pass
>>> if a is not False:
>>>    pass
>>> if a:
>>>    pass

And these as well:

>>> if a is False:
>>>    pass
>>> if a is not True:
>>>    pass
>>> if not a:
>>>    pass

Boolean Operators(布尔操作符)

There are three Boolean operators: and, or, and not.

The and Operator’s Truth Table:

Expression  Evaluates to
True and True       True
True and False      False
False and True      False
False and False     False

The or Operator’s Truth Table:

Expression  Evaluates to
True or True        True
True or False       True
False or True       True
False or False      False

The not Operator’s Truth Table:

Expression  Evaluates to
not True    False
not False   True

Mixing Boolean and Comparison Operators

>>> (4 < 5) and (5 < 6)
True
>>> (4 < 5) and (9 < 6)
False
>>> (1 == 2) or (2 == 2)
True

You can also use multiple Boolean operators in an expression, along with the comparison operators:

>>> 2 + 2 == 4 and not 2 + 2 == 5 and 2 * 2 == 2 + 2
True

if Statements

if name == 'Alice':
    print('Hi, Alice.')

else Statements

name = 'Bob'
if name == 'Alice':
    print('Hi, Alice.')
else:
    print('Hello, stranger.')

elif Statements

name = 'Bob'
age = 5
if name == 'Alice':
    print('Hi, Alice.')
elif age < 12:
    print('You are not Alice, kiddo.')
name = 'Bob'
age = 30
if name == 'Alice':
    print('Hi, Alice.')
elif age < 12:
    print('You are not Alice, kiddo.')
else:
    print('You are neither Alice nor a little kid.')

while Loop Statements

spam = 0
while spam < 5:
    print('Hello, world.')
    spam = spam + 1

break Statements

If the execution reaches a break statement, it immediately exits the while loop’s clause:

while True:
    print('Please type your name.')
    name = input()
    if name == 'your name':
        break
print('Thank you!')

continue Statements

When the program execution reaches a continue statement, the program execution immediately jumps back to the start of the loop.

while True:
    print('Who are you?')
    name = input()
    if name != 'Joe':
        continue
    print('Hello, Joe. What is the password? (It is a fish.)')
    password = input()
    if password == 'swordfish':
        break
print('Access granted.')

for Loops and the range() Function

>>> print('My name is')
>>> for i in range(5):
>>>     print('Jimmy Five Times ({})'.format(str(i)))
My name is
Jimmy Five Times (0)
Jimmy Five Times (1)
Jimmy Five Times (2)
Jimmy Five Times (3)
Jimmy Five Times (4)

The range() function can also be called with three arguments. The first two arguments will be the start and stop values, and the third will be the step argument. The step is the amount that the variable is increased by after each iteration.

>>> for i in range(0, 10, 2):
>>>    print(i)
0
2
4
6
8

You can even use a negative number for the step argument to make the for loop count down instead of up.

>>> for i in range(5, -1, -1):
>>>     print(i)
5
4
3
2
1
0

For else statement

This allows to specify a statement to execute in case of the full loop has been executed. Only useful when a break condition can occur in the loop:

>>> for i in [1, 2, 3, 4, 5]:
>>>    if i == 3:
>>>        break
>>> else:
>>>    print("only executed when no item of the list is equal to 3")

Importing Modules

import random
for i in range(5):
    print(random.randint(1, 10))

import random, sys, os, math

from random import *.

Ending a Program Early with sys.exit()

import sys

while True:
    print('Type exit to exit.')
    response = input()
    if response == 'exit':
        sys.exit()
    print('You typed {}.'.format(response))

Functions(函数)

>>> def hello(name):
>>>     print('Hello {}'.format(name))
>>>
>>> hello('Alice')
>>> hello('Bob')
Hello Alice
Hello Bob

Return Values and return Statements(返回值和返回语句)

When creating a function using the def statement, you can specify what the return value should be with a return statement.

def 定义函数时可以在 return 语句中指明 return 值

A return statement consists of the following: return 语句由以下组成

  • The return keyword.
  • return 关键字。
  • The value or expression that the function should return.
  • 函数应该返回的值或表达式。
import random
def getAnswer(answerNumber):
    if answerNumber == 1:
        return 'It is certain'
    elif answerNumber == 2:
        return 'It is decidedly so'
    elif answerNumber == 3:
        return 'Yes'
    elif answerNumber == 4:
        return 'Reply hazy try again'
    elif answerNumber == 5:
        return 'Ask again later'
    elif answerNumber == 6:
        return 'Concentrate and ask again'
    elif answerNumber == 7:
        return 'My reply is no'
    elif answerNumber == 8:
        return 'Outlook not so good'
    elif answerNumber == 9:
        return 'Very doubtful'

r = random.randint(1, 9)
fortune = getAnswer(r)
print(fortune)

The None Value(None 值)

>>> spam = print('Hello!')
Hello!
>>> spam is None
True

Note: never compare to None with the == operator. Always use is. 注意:不要用 == 来和 None 比较。应该使用 is

Keyword Arguments and print()(关键字参数和 print() )

>>> print('Hello', end='')
>>> print('World')
HelloWorld
>>> print('cats', 'dogs', 'mice')
cats dogs mice
>>> print('cats', 'dogs', 'mice', sep=',')
cats,dogs,mice

Local and Global Scope(局部和全局作用域)

Code in the global scope cannot use any local variables. 在全局作用域中的代码不能使用任何局部变量。

However, a local scope can access global variables. 但是,局部作用域可以获取到全局变量。

Code in a function’s local scope cannot use variables in any other local scope. 在函数的局部作用域中不能使用其他局部作用域的变量。

You can use the same name for different variables if they are in different scopes. That is, there can be a local variable named spam and a global variable also named spam. 可以在不同的作用域中用相同的名称命名不同的变量。也就是说,可以有名为 spam 的全局变量的同时有名为 spam 的局部变量。

The global Statement(global声明)

If you need to modify a global variable from within a function, use the global statement: 可以使用 global 声明在函数中修改全局变量

>>> def spam():
>>>     global eggs
>>>     eggs = 'spam'
>>>
>>> eggs = 'global'
>>> spam()
>>> print(eggs)
spam

There are four rules to tell whether a variable is in a local scope or global scope:

  • If a variable is being used in the global scope (that is, outside of all functions), then it is always a global variable.
  • If there is a global statement for that variable in a function, it is a global variable.
  • Otherwise, if the variable is used in an assignment statement in the function, it is a local variable.
  • But if the variable is not used in an assignment statement, it is a global variable.

判断一个变量是在局部作用域还是全局作用域有四条规则。

  • 如果一个变量是在全局范围内(即在所有函数之外),那么它总是一个全局变量。
  • 如果在函数中存在一个 global 语句,那么该变量就是一个全局变量。
  • 否则,如果在函数中的赋值语句中使用了该变量,那么它就是一个局部变量。
  • 但如果在赋值语句中没有使用该变量,那么它就是一个全局变量。

Exception Handling(异常处理)

Basic exception handling(基本异常处理)

>>> def spam(divideBy):
>>>     try:
>>>         return 42 / divideBy
>>>     except ZeroDivisionError as e:
>>>         print('Error: Invalid argument: {}'.format(e))
>>>
>>> print(spam(2))
>>> print(spam(12))
>>> print(spam(0))
>>> print(spam(1))
21.0
3.5
Error: Invalid argument: division by zero
None
42.0

Final code in exception handling (异常处理中的final代码)

Code inside the finally section is always executed, no matter if an exception has been raised or not, and even if an exception is not caught. final 部分里面的代码总是被执行。无论是否抛出了异常,即使没有 catch 到异常,也会执行。

>>> def spam(divideBy):
>>>     try:
>>>         return 42 / divideBy
>>>     except ZeroDivisionError as e:
>>>         print('Error: Invalid argument: {}'.format(e))
>>>     finally:
>>>         print("-- division finished --")
>>> print(spam(12))
>>> print(spam(0))
21.0
-- division finished --
3.5
-- division finished --
Error: Invalid argument: division by zero
-- division finished --
None
-- division finished --
42.0
-- division finished --

Lists

>>> spam = ['cat', 'bat', 'rat', 'elephant']

>>> spam
['cat', 'bat', 'rat', 'elephant']

索引

Getting Individual Values in a List with Indexes

>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam[0]
'cat'
>>> spam[1]
'bat'
>>> spam[2]
'rat'
>>> spam[3]
'elephant'

负数索引

Negative Indexes

>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam[-1]
'elephant'
>>> spam[-3]
'bat'
>>> 'The {} is afraid of the {}.'.format(spam[-1], spam[-3])
'The elephant is afraid of the bat.'

切片

Getting Sublists with Slices

>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam[0:4]
['cat', 'bat', 'rat', 'elephant']
>>> spam[1:3]
['bat', 'rat']
>>> spam[0:-1]
['cat', 'bat', 'rat']
>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam[:2]
['cat', 'bat']
>>> spam[1:]
['bat', 'rat', 'elephant']

Slicing the complete list will perform a copy:

>>> spam2 = spam[:]
['cat', 'bat', 'rat', 'elephant']
>>> spam.append('dog')
>>> spam
['cat', 'bat', 'rat', 'elephant', 'dog']
>>> spam2
['cat', 'bat', 'rat', 'elephant']

list.len()

Getting a List’s Length with len()

>>> spam = ['cat', 'dog', 'moose']
>>> len(spam)
3

用索引修改 List 中的值

Changing Values in a List with Indexes

>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam[1] = 'aardvark'

>>> spam
['cat', 'aardvark', 'rat', 'elephant']

>>> spam[2] = spam[1]

>>> spam
['cat', 'aardvark', 'aardvark', 'elephant']

>>> spam[-1] = 12345

>>> spam
['cat', 'aardvark', 'aardvark', 12345]

List 串联和复制

List Concatenation and List Replication

>>> [1, 2, 3] + ['A', 'B', 'C']
[1, 2, 3, 'A', 'B', 'C']

>>> ['X', 'Y', 'Z'] * 3
['X', 'Y', 'Z', 'X', 'Y', 'Z', 'X', 'Y', 'Z']

>>> spam = [1, 2, 3]

>>> spam = spam + ['A', 'B', 'C']

>>> spam
[1, 2, 3, 'A', 'B', 'C']

del 语句从 List 中删除值

Removing Values from Lists with del Statements

>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> del spam[2]
>>> spam
['cat', 'bat', 'elephant']
>>> del spam[2]
>>> spam
['cat', 'bat']

List 和 for 循环

Using for Loops with Lists

>>> supplies = ['pens', 'staplers', 'flame-throwers', 'binders']
>>> for i, supply in enumerate(supplies):
>>>     print('Index {} in supplies is: {}'.format(str(i), supply))
Index 0 in supplies is: pens
Index 1 in supplies is: staplers
Index 2 in supplies is: flame-throwers
Index 3 in supplies is: binders

zip() 遍历多个 List

Looping Through Multiple Lists with zip()

>>> name = ['Pete', 'John', 'Elizabeth']
>>> age = [6, 23, 44]
>>> for n, a in zip(name, age):
>>>     print('{} is {} years old'.format(n, a))
Pete is 6 years old
John is 23 years old
Elizabeth is 44 years old

in 和 not in 操作符

The in and not in Operators

>>> 'howdy' in ['hello', 'hi', 'howdy', 'heyas']
True
>>> spam = ['hello', 'hi', 'howdy', 'heyas']
>>> 'cat' in spam
False
>>> 'howdy' not in spam
False
>>> 'cat' not in spam
True

多赋值技巧

The Multiple Assignment Trick

The multiple assignment trick is a shortcut that lets you assign multiple variables with the values in a list in one line of code. So instead of doing this:

多重赋值技巧是一种快捷方式,可以让你在一行代码中分配多个变量列表中的值。所以,下述代码:

>>> cat = ['fat', 'orange', 'loud']
>>> size = cat[0]
>>> color = cat[1]
>>> disposition = cat[2]

You could type this line of code: 可以用一行代码替代

>>> cat = ['fat', 'orange', 'loud']
>>> size, color, disposition = cat

The multiple assignment trick can also be used to swap the values in two variables: 多重赋值技巧也可以用来交换两个变量中的值。

>>> a, b = 'Alice', 'Bob'
>>> a, b = b, a
>>> print(a)
'Bob'
>>> print(b)
'Alice'

增强赋值运算符

Augmented Assignment Operators

Operator    Equivalent
spam += 1   spam = spam + 1
spam -= 1   spam = spam - 1
spam \*= 1  spam = spam \* 1
spam /= 1   spam = spam / 1
spam %= 1   spam = spam % 1

Examples:

>>> spam = 'Hello'
>>> spam += ' world!'
>>> spam
'Hello world!'

>>> bacon = ['Zophie']
>>> bacon *= 3
>>> bacon
['Zophie', 'Zophie', 'Zophie']
Finding a Value in a List with the index() Method
>>> spam = ['Zophie', 'Pooka', 'Fat-tail', 'Pooka']

>>> spam.index('Pooka')
1

list.append() 和 list.insert()——向List中添加值

Adding Values to Lists with the append() and insert() Methods

append():

>>> spam = ['cat', 'dog', 'bat']
>>> spam.append('moose')
>>> spam
['cat', 'dog', 'bat', 'moose']

insert():

>>> spam = ['cat', 'dog', 'bat']
>>> spam.insert(1, 'chicken')
>>> spam
['cat', 'chicken', 'dog', 'bat']

list.remove()——从 List 中移除值

Removing Values from Lists with remove()

>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam.remove('bat')
>>> spam
['cat', 'rat', 'elephant']

If the value appears multiple times in the list, only the first instance of the value will be removed. 如果该值在列表中出现多次,则只有该值的第一个实例将被删除。

list.sort()——对 List 中的值排序

Sorting the Values in a List with the sort() Method

>>> spam = [2, 5, 3.14, 1, -7]
>>> spam.sort()
>>> spam
[-7, 1, 2, 3.14, 5]
>>> spam = ['ants', 'cats', 'dogs', 'badgers', 'elephants']
>>> spam.sort()
>>> spam
['ants', 'badgers', 'cats', 'dogs', 'elephants']

You can also pass True for the reverse keyword argument to have sort() sort the values in reverse order:

你也可以为 reverse 关键字参数传递 True,让 list.sort() 排序值按相反的顺序排列。

>>> spam.sort(reverse=True)
>>> spam
['elephants', 'dogs', 'cats', 'badgers', 'ants']

If you need to sort the values in regular alphabetical order, pass str.lower for the key keyword argument in the sort() method call: 如果你需要按照常规的字母顺序排序,在调用排序方法中传递str.lower 作为 sort() 方法调用中的关键字参数。

>>> spam = ['a', 'z', 'A', 'Z']
>>> spam.sort(key=str.lower)
>>> spam
['a', 'A', 'z', 'Z']

You can use the built-in function sorted to return a new list: 你可以使用内置的排序函数来返回一个新的列表。

>>> spam = ['ants', 'cats', 'dogs', 'badgers', 'elephants']
>>> sorted(spam)
['ants', 'badgers', 'cats', 'dogs', 'elephants']

Tuple(图元组)类型

Tuple Data Type

>>> eggs = ('hello', 42, 0.5)
>>> eggs[0]
'hello'
>>> eggs[1:3]
(42, 0.5)
>>> len(eggs)
3

The main way that tuples are different from lists is that tuples, like strings, are immutable. 图元组与列表的主要区别在于,图元组和字符串一样,是不可变的。

list() 和 tuple()——转换类型

Converting Types with the list() and tuple() Functions

>>> tuple(['cat', 'dog', 5])
('cat', 'dog', 5)
>>> list(('cat', 'dog', 5))
['cat', 'dog', 5]
>>> list('hello')
['h', 'e', 'l', 'l', 'o']

Dictionaries and Structuring Data(字典和结构化数据)

Example Dictionary:

myCat = {'size': 'fat', 'color': 'gray', 'disposition': 'loud'}

dict.keys() dict.values()和 dict.items()方法

The keys(), values(), and items() Methods

values():

>>> spam = {'color': 'red', 'age': 42}
>>> for v in spam.values():
>>>     print(v)
red
42

keys():

>>> for k in spam.keys():
>>>     print(k)
color
age

items():

>>> for i in spam.items():
>>>     print(i)
('color', 'red')
('age', 42)

Using the keys(), values(), and items() methods, a for loop can iterate over the keys, values, or key-value pairs in a dictionary, respectively. 使用keys()、value()和 items()方法,一个 for 循环可以迭代在字典中的键、值或键-值对上。

>>> spam = {'color': 'red', 'age': 42}
>>>
>>> for k, v in spam.items():
>>>     print('Key: {} Value: {}'.format(k, str(v)))
Key: age Value: 42
Key: color Value: red

key 或 value 是否存在

Checking Whether a Key or Value Exists in a Dictionary

>>> spam = {'name': 'Zophie', 'age': 7}
>>> 'name' in spam.keys()
True
>>> 'Zophie' in spam.values()
True
>>> # You can omit the call to keys() when checking for a key
>>> 'color' in spam
False
>>> 'color' not in spam
True

dict.get()方法

The get() Method

>>> picnic_items = {'apples': 5, 'cups': 2}
>>> 'I am bringing {} cups.'.format(str(picnic_items.get('cups', 0)))
'I am bringing 2 cups.'
>>> 'I am bringing {} eggs.'.format(str(picnic_items.get('eggs', 0)))
'I am bringing 0 eggs.'

dict.serdefault()方法

The setdefault() Method

Let's consider this code:

spam = {'name': 'Pooka', 'age': 5}
if 'color' not in spam:
    spam['color'] = 'black'

Using setdefault we could write the same code more succinctly: 使用setdefault,我们可以更简洁地写出同样的代码。

>>> spam = {'name': 'Pooka', 'age': 5}
>>> spam.setdefault('color', 'black')
'black'
>>> spam
{'color': 'black', 'age': 5, 'name': 'Pooka'}
>>> spam.setdefault('color', 'white')
'black'
>>> spam
{'color': 'black', 'age': 5, 'name': 'Pooka'}

美观打印

Pretty Printing

>>> import pprint
>>>
>>> message = 'It was a bright cold day in April, and the clocks were striking
>>> thirteen.'
>>> count = {}
>>>
>>> for character in message:
>>>     count.setdefault(character, 0)
>>>     count[character] = count[character] + 1
>>>
>>> pprint.pprint(count)
{' ': 13,
',': 1,
'.': 1,
'A': 1,
'I': 1,
'a': 4,
'b': 1,
'c': 3,
'd': 3,
'e': 5,
'g': 2,
'h': 3,
'i': 6,
'k': 2,
'l': 3,
'n': 4,
'o': 2,
'p': 1,
'r': 5,
's': 3,
't': 6,
'w': 2,
'y': 1}

合并两个字典

Merge two dictionaries

# in Python 3.5+: >>> x = {'a': 1, 'b': 2} >>> y = {'b': 3, 'c': 4} >>> z = {**x, **y} >>> z {'c': 4, 'a': 1, 'b': 3}

# in Python 2.7 >>> z = dict(x, **y) >>> z {'c': 4, 'a': 1, 'b': 3}

Sets(集)

From the Python 3 documentation

A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference. 集是一个没有重复元素的无序集合。基本用途包括成员检验和消除重复的条目。集对象还支持联合、交集、差分和对称差分等数学运算。

集初始化

Initializing a set

There are two ways to create sets: using curly braces {} and the bult-in function set() 有两种创建集的方法:使用大括号{}和bult-in函数set()。

>>> s = {1, 2, 3}
>>> s = set([1, 2, 3])

When creating an empty set, be sure to not use the curly braces {} or you will get an empty dictionary instead.

创建空集时,请确保不要使用大括号{},否则你会得到一个空的字典。

>>> s = {}
>>> type(s)
<class 'dict'>

集:不重复元素的无序集合

sets: unordered collections of unique elements

A set automatically remove all the duplicate values. 集自动删除所有重复元素。

>>> s = {1, 2, 3, 2, 3, 4}
>>> s
{1, 2, 3, 4}

And as an unordered data type, they can't be indexed. 因为是无序数据类型,所以不能索引访问。

>>> s = {1, 2, 3}
>>> s[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'set' object does not support indexing

set.add() 和 set.update()

Using the add() method we can add a single element to the set. 使用add()方法,我们可以向集合中添加一个元素。

>>> s = {1, 2, 3}
>>> s.add(4)
>>> s
{1, 2, 3, 4}

And with update(), multiple ones . 而使用update()方法,可以添加多个元素。

>>> s = {1, 2, 3}
>>> s.update([2, 3, 4, 5, 6])
>>> s
{1, 2, 3, 4, 5, 6}  # remember, sets automatically remove duplicates

set.remove() 和 set.discard()

Both methods will remove an element from the set, but remove() will raise a key error if the value doesn't exist. 这两种方法都会从集合中删除一个元素。但如果值不存在,remove()会抛出 key error。

>>> s = {1, 2, 3}
>>> s.remove(3)
>>> s
{1, 2}
>>> s.remove(3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 3

discard() won't raise any errors. discard()不抛出任何错误。

>>> s = {1, 2, 3}
>>> s.discard(3)
>>> s
{1, 2}
>>> s.discard(3)
>>>

set.union()——并集

union() or | will create a new set that contains all the elements from the sets provided. union() 或|将创建一个新的集,其中包含所有提供的集中的元素。 .. code :: sh

>>> s1 = {1, 2, 3}
>>> s2 = {3, 4, 5}
>>> s1.union(s2)  # or 's1 | s2'
{1, 2, 3, 4, 5}

set.intersection()——交集

intersection or & will return a set containing only the elements that are common to all of them. 交集或 & 将返回一个只包含所有元素共性的集合。

>>> s1 = {1, 2, 3}
>>> s2 = {2, 3, 4}
>>> s3 = {3, 4, 5}
>>> s1.intersection(s2, s3)  # or 's1 & s2 & s3'
{3}

set.difference()——差

difference or - will return only the elements that are in one of the sets. difference 或 - 将只返回其中一个集合中的元素。

>>> s1 = {1, 2, 3}
>>> s2 = {2, 3, 4}
>>> s1.difference(s2)  # or 's1 - s2'
{1}

set.symetric_difference()——对称差集

symetric_difference or ^ will return all the elements that are not common between them. symetric_difference或^将返回它们之间不通用的所有元素。

>>> s1 = {1, 2, 3}
>>> s2 = {2, 3, 4}
>>> s1.symmetric_difference(s2)  # or 's1 ^ s2'
{1, 4}

Itertools Module(itertools 迭代器工具模块)

The itertools module is a colection of tools intented to be fast and use memory efficiently when handling iterators (like lists or dictionaries). itertools 模块是一组工具的集合,其目的是在处理迭代器 (如列表或字典) 时,快速、高效地使用内存。

From the official Python 3.x documentation:

The module standardizes a core set of fast, memory efficient tools that are useful
by themselves or in combination. Together, they form an “iterator algebra”
making it possible to construct specialized tools succinctly and efficiently in pure Python.
该模块标准化了一组快速、高效内存的核心工具,这些工具本身或组合在一起都很有用。
它们共同组成了一个 "迭代器代数",使得在纯Python中简洁高效地构造专门的工具成为可能。

The itertools module comes in the standard library and must be imported. itertools 模块在标准库中,必须导入。

The operator module will also be used. This module is not necessary when using itertools, but needed for some of the examples below. 运算符模块也会被使用。在使用 itertools 时,这个模块不是必须的,但在下面的一些例子中是需要的。

itertools.accumulate()——累计

accumulate()

Makes an iterator that returns the results of a function.

itertools.accumulate(iterable[, func])

Example:

>>> data = [1, 2, 3, 4, 5]
>>> result = itertools.accumulate(data, operator.mul)
>>> for each in result:
>>>    print(each)
1
2
6
24
120
The operator.mul takes two numbers and multiplies them:

operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120

Passing a function is optional: 传入函数是可选的。

>>> data = [5, 2, 6, 4, 5, 9, 1]
>>> result = itertools.accumulate(data)
>>> for each in result:
>>>    print(each)
5
7
13
17
22
31
32

If no function is designated the items will be summed: 如果没有指定函数,项目将被累加。

5 5 + 2 = 7 7 + 6 = 13 13 + 4 = 17 17 + 5 = 22 22 + 9 = 31 31 + 1 = 32

itertools.combinations()——组合

combinations()

Takes an iterable and a integer. This will create all the unique combination that have r members. 取一个迭代数和一个整数r。这将创建所有有 r 个成员的唯一组合。

itertools.combinations(iterable, r)

Example:

>>> shapes = ['circle', 'triangle', 'square',]
>>> result = itertools.combinations(shapes, 2)
>>> for each in result:
>>>    print(each)
('circle', 'triangle')
('circle', 'square')
('triangle', 'square')

itertools.combinations_with_replacement()——替换组合

combinations_with_replacement()

Just like combinations(), but allows individual elements to be repeated more than once. 就像 combinations(),但允许单个元素重复多次。

itertools.combinations_with_replacement(iterable, r)

Example:

>>> shapes = ['circle', 'triangle', 'square']
>>> result = itertools.combinations_with_replacement(shapes, 2)
>>> for each in result:
>>>    print(each)
('circle', 'circle')
('circle', 'triangle')
('circle', 'square')
('triangle', 'triangle')
('triangle', 'square')
('square', 'square')

itertools.count()

count()

Makes an iterator that returns evenly spaced values starting with number start. 制作一个迭代器,返回从数字开始的均匀间隔的值。

itertools.count(start=0, step=1)

Example:

>>> for i in itertools.count(10,3):
>>>    print(i)
>>>    if i > 20:
>>>        break
10
13
16
19
22

itertools.cycle()

This function cycles through an iterator endlessly. 这个函数在一个迭代器中无休止的循环。

itertools.cycle(iterable)

Example:

>>> colors = ['red', 'orange', 'yellow', 'green', 'blue', 'violet']
>>> for color in itertools.cycle(colors):
>>>    print(color)
red
orange
yellow
green
blue
violet
red
orange

When reached the end of the iterable it start over again from the beginning. 当到达 iterable 终点时,可以从头开始重新开始。

itertools.chain()

Take a series of iterables and return them as one long iterable. 取一系列的 iterables,并将其作为一个长的 iterables 返回。

itertools.chain(*iterables)

Example:

>>> colors = ['red', 'orange', 'yellow', 'green', 'blue']
>>> shapes = ['circle', 'triangle', 'square', 'pentagon']
>>> result = itertools.chain(colors, shapes)
>>> for each in result:
>>>    print(each)
red
orange
yellow
green
blue
circle
triangle
square
pentagon

itertools.compress()

Filters one iterable with another. 用一个 iterable 过滤另一个。

itertools.compress(data, selectors)

Example:

>>> shapes = ['circle', 'triangle', 'square', 'pentagon']
>>> selections = [True, False, True, False]
>>> result = itertools.compress(shapes, selections)
>>> for each in result:
>>>    print(each)
circle
square

itertools.dropwhile()

Make an iterator that drops elements from the iterable as long as the predicate is true; afterwards, returns every element. 做一个迭代器,只要 predicate 为真,就从 iterable 中丢弃元素;之后,返回每一个元素。

itertools.dropwhile(predicate, iterable)

Example:

>>> data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1]
>>> result = itertools.dropwhile(lambda x: x<5, data)
>>> for each in result:
>>>    print(each)
5
6
7
8
9
10
1

itertools.filterfalse()

Makes an iterator that filters elements from iterable returning only those for which the predicate is False.

制作一个迭代器,从 iterable 中筛选元素,只返回那些 predicate 为False的元素。

itertools.filterfalse(predicate, iterable)

Example:

>>> data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> result = itertools.filterfalse(lambda x: x<5, data)
>>> for each in result:
>>>    print(each)
5
6
7
8
9
10

itertools.groupby()

Simply put, this function groups things together. 简单的说,这个功能将事物分组。

itertools.groupby(iterable, key=None)

Example:

>>> robots = [{
    'name': 'blaster',
    'faction': 'autobot'
}, {
    'name': 'galvatron',
    'faction': 'decepticon'
}, {
    'name': 'jazz',
    'faction': 'autobot'
}, {
    'name': 'metroplex',
    'faction': 'autobot'
}, {
    'name': 'megatron',
    'faction': 'decepticon'
}, {
    'name': 'starcream',
    'faction': 'decepticon'
}]
>>> for key, group in itertools.groupby(robots, key=lambda x: x['faction']):
>>>    print(key)
>>>    print(list(group))
autobot
[{'name': 'blaster', 'faction': 'autobot'}]
decepticon
[{'name': 'galvatron', 'faction': 'decepticon'}]
autobot
[{'name': 'jazz', 'faction': 'autobot'}, {'name': 'metroplex', 'faction': 'autobot'}]
decepticon
[{'name': 'megatron', 'faction': 'decepticon'}, {'name': 'starcream', 'faction': 'decepticon'}]

itertools.islice()

This function is very much like slices. This allows you to cut out a piece of an iterable.

这个功能很像切片。这样,你就可以在一个迭部中切出一片片。

itertools.islice(iterable, start, stop[, step])

Example:

>>> colors = ['red', 'orange', 'yellow', 'green', 'blue',]
>>> few_colors = itertools.islice(colors, 2)
>>> for each in few_colors:
>>>    print(each)
red
orange

itertools.permutations()

itertools.permutations(iterable, r=None)

Example:

>>> alpha_data = ['a', 'b', 'c']
>>> result = itertools.permutations(alpha_data)
>>> for each in result:
>>>    print(each)
('a', 'b', 'c')
('a', 'c', 'b')
('b', 'a', 'c')
('b', 'c', 'a')
('c', 'a', 'b')
('c', 'b', 'a')

itertools.product()

Creates the cartesian products from a series of iterables.

从一系列 iterables 中创建 笛卡尔积

>>> num_data = [1, 2, 3]
>>> alpha_data = ['a', 'b', 'c']
>>> result = itertools.product(num_data, alpha_data)
>>> for each in result:
    print(each)
(1, 'a')
(1, 'b')
(1, 'c')
(2, 'a')
(2, 'b')
(2, 'c')
(3, 'a')
(3, 'b')
(3, 'c')

itertools.repeat()

This function will repeat an object over and over again. Unless, there is a times argument.

这个函数将反复重复一个对象。除非,有一个 times 次数参数。

itertools.repeat(object[, times])

Example:

>>> for i in itertools.repeat("spam", 3):
    print(i)
spam
spam
spam

itertools.starmap()

Makes an iterator that computes the function using arguments obtained from the iterable.

制作一个迭代器,使用从迭代器中获得的参数计算函数。

itertools.starmap(function, iterable)

Example:

>>> data = [(2, 6), (8, 4), (7, 3)]
>>> result = itertools.starmap(operator.mul, data)
>>> for each in result:
>>>    print(each)
12
32
21

itertools.takewhile()

The opposite of dropwhile(). Makes an iterator and returns elements from the iterable as long as the predicate is true.

与 dropwhile()相反。做一个迭代器并从迭代器中返回元素,只要谓词为真。

itertools.takwwhile(predicate, iterable)

Example:

>>> data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1]
>>> result = itertools.takewhile(lambda x: x<5, data)
>>> for each in result:
>>>    print(each)
1
2
3
4

itertools.tee()

Return n independent iterators from a single iterable.

从单个迭代器中返回n个独立的迭代器。

tee(iterable, n=2)

Example:

>>> colors = ['red', 'orange', 'yellow', 'green', 'blue']
>>> alpha_colors, beta_colors = itertools.tee(colors)
>>> for each in alpha_colors:
>>>    print(each)
red
orange
yellow
green
blue
>>> colors = ['red', 'orange', 'yellow', 'green', 'blue']
>>> alpha_colors, beta_colors = itertools.tee(colors)
>>> for each in beta_colors:
>>>    print(each)
red
orange
yellow
green
blue

itertools.zip_longest()

Makes an iterator that aggregates elements from each of the iterables. If the iterables are of uneven length, missing values are filled-in with fillvalue. Iteration continues until the longest iterable is exhausted.

制作一个迭代器,从每个迭代项中聚合元素。如果迭代项的长度不均匀,则用 fillvalue 来填充缺失的值。 迭代继续进行,直到最长的迭代项用完为止。

itertools.zip_longest(\*iterables, fillvalue=None)

Example:

>>> colors = ['red', 'orange', 'yellow', 'green', 'blue',]
>>> data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,]
>>> for each in itertools.zip_longest(colors, data, fillvalue=None):
>>>    print(each)
('red', 1)
('orange', 2)
('yellow', 3)
('green', 4)
('blue', 5)
(None, 6)
(None, 7)
(None, 8)
(None, 9)
(None, 10)

Comprehensions

List comprehension 列表自解析

>>> a = [1, 3, 5, 7, 9, 11]

>>> [i - 1 for i in a]
[0, 2, 4, 6, 8, 10]

Set comprehension 集自解析

>>> b = {"abc", "def"}
>>> {s.upper() for s in b}
{"ABC", "DEF}

Dict comprehension 字典自解析

>>> c = {'name': 'Pooka', 'age': 5}
>>> {v: k for k, v in c.items()}
{'Pooka': 'name', 5: 'age'}

A List comprehension can be generated from a dictionary:

list 自解析可以由字典生成

>>> c = {'name': 'Pooka', 'first_name': 'Oooka'}
>>> ["{}:{}".format(k.upper(), v.upper()) for k, v in c.items()]
['NAME:POOKA', 'FIRST_NAME:OOOKA']

Manipulating Strings 处理字符串

Escape Characters 转义字符

Escape character    Prints as
\'  Single quote
\"  Double quote
\t  Tab
\n  Newline (line break)
\\  Backslash

Example:

>>> print("Hello there!\nHow are you?\nI\'m doing fine.")
Hello there!
How are you?
I'm doing fine.

Raw Strings

A raw string completely ignores all escape characters and prints any backslash that appears in the string.

原始字符串完全忽略所有转义字符,并打印出字符串中出现的任何反斜线。

>>> print(r'That is Carol\'s cat.')
That is Carol\'s cat.

Note: mostly used for regular expression definition (see re package) 注意:常用在正则表达式定义中

Multiline Strings with Triple Quotes

>>> print('''Dear Alice,
>>>
>>> Eve's cat has been arrested for catnapping, cat burglary, and extortion.
>>>
>>> Sincerely,
>>> Bob''')
Dear Alice,

Eve's cat has been arrested for catnapping, cat burglary, and extortion.

Sincerely,
Bob

To keep a nicer flow in your code, you can use the dedent function from the textwrap standard package. 为了在你的代码中保持一个更好的 flow,你可以使用来自 textwrap 标准包的 dedent函数。

>>> from textwrap import dedent
>>>
>>> def my_function():
>>>     print('''
>>>         Dear Alice,
>>>
>>>         Eve's cat has been arrested for catnapping, cat burglary, and extortion.
>>>
>>>         Sincerely,
>>>         Bob
>>>         ''').strip()

This generates the same string than before. 和之前生成同样的字符串

Indexing and Slicing Strings

H e l l o w o r l d ! 0 1 2 3 4 5 6 7 8 9 10 11

>>> spam = 'Hello world!'
>>> spam[0]
'H'
>>> spam[4]
'o'
>>> spam[-1]
'!'

Slicing:

>>> spam[0:5]
'Hello'
>>> spam[:5]
'Hello'
>>> spam[6:]
'world!'
>>> spam[6:-1]
'world'
>>> spam[:-1]
'Hello world'
>>> spam[::-1]
'!dlrow olleH'
>>> spam = 'Hello world!'
>>> fizz = spam[0:5]
>>> fizz
'Hello'

The in and not in Operators with Strings

>>> 'Hello' in 'Hello World'
True
>>> 'Hello' in 'Hello'
True
>>> 'HELLO' in 'Hello World'
False
>>> '' in 'spam'
True
>>> 'cats' not in 'cats and dogs'
False
The in and not in Operators with list
>>> a = [1, 2, 3, 4]
>>> 5 in a
False
>>> 2 in a
True

The upper(), lower(), isupper(), and islower() String Methods

upper() and lower():

>>> spam = 'Hello world!'
>>> spam = spam.upper()
>>> spam
'HELLO WORLD!'
>>> spam = spam.lower()
>>> spam
'hello world!'

isupper() and islower():

>>> spam = 'Hello world!'
>>> spam.islower()
False
>>> spam.isupper()
False
>>> 'HELLO'.isupper()
True
>>> 'abc12345'.islower()
True
>>> '12345'.islower()
False
>>> '12345'.isupper()
False

The isX String Methods

  • isalpha() returns True if the string consists only of letters and is not blank.
  • isalnum() returns True if the string consists only of lettersand numbers and is not blank.
  • isdecimal() returns True if the string consists only ofnumeric characters and is not blank.
  • isspace() returns True if the string consists only of spaces,tabs, and new-lines and is not blank.
  • istitle() returns True if the string consists only of wordsthat begin with an uppercase letter followed by onlylowercase letters.
  • isalpha() 返回 True,如果字符串只由字母组成且不是空的,则返回true。
  • isalnum() 如果字符串仅由字母和数字组成且不为空,则返回 True。
  • isdecimal() 返回 True,如果字符串只由数字字符组成且不是空的,则返回true。
  • isspace() 如果字符串仅由空格、制表符和新行组成且不是空的,则返回 True。
  • istitle() 返回 True,如果字符串只由大写字母开头的单词组成,后面只有小写字母。

The startswith() and endswith() String Methods

>>> 'Hello world!'.startswith('Hello')
True
>>> 'Hello world!'.endswith('world!')
True
>>> 'abc123'.startswith('abcdef')
False
>>> 'abc123'.endswith('12')
False
>>> 'Hello world!'.startswith('Hello world!')
True
>>> 'Hello world!'.endswith('Hello world!')
True

合并分割 The join() and split() String Methods

join():

>>> ', '.join(['cats', 'rats', 'bats'])
'cats, rats, bats'
>>> ' '.join(['My', 'name', 'is', 'Simon'])
'My name is Simon'
>>> 'ABC'.join(['My', 'name', 'is', 'Simon'])
'MyABCnameABCisABCSimon'

split():

>>> 'My name is Simon'.split()
['My', 'name', 'is', 'Simon']
>>> 'MyABCnameABCisABCSimon'.split('ABC')
['My', 'name', 'is', 'Simon']
>>> 'My name is Simon'.split('m')
['My na', 'e is Si', 'on']

对齐 Justifying Text with rjust(), ljust(), and center()

rjust() and ljust():

>>> 'Hello'.rjust(10)
'     Hello'
>>> 'Hello'.rjust(20)
'               Hello'
>>> 'Hello World'.rjust(20)
'         Hello World'
>>> 'Hello'.ljust(10)
'Hello     '

An optional second argument to rjust() and ljust() will specify a fill character other than a space character.

rjust()和ljust()的可选的第二个参数将指定一个填充字符,而非一个空格字符。

Enter the following into the interactive shell:

>>> 'Hello'.rjust(20, '*')
'***************Hello'
>>> 'Hello'.ljust(20, '-')
'Hello---------------'

center():

>>> 'Hello'.center(20)
'       Hello       '
>>> 'Hello'.center(20, '=')
'=======Hello========'

移除空格 Removing Whitespace with strip(), rstrip(), and lstrip()

>>> spam = '    Hello World     '
>>> spam.strip()
'Hello World'
>>> spam.lstrip()
'Hello World '
>>> spam.rstrip()
'    Hello World'
>>> spam = 'SpamSpamBaconSpamEggsSpamSpam'
>>> spam.strip('ampS')
'BaconSpamEggs'

复制粘贴 Copying and Pasting Strings with the pyperclip Module (need pip install)

>>> import pyperclip
>>> pyperclip.copy('Hello world!')
>>> pyperclip.paste()
'Hello world!'

String Formatting 字符串格式化

% operator % 操作符

>>> name = 'Pete'
>>> 'Hello %s' % name
"Hello Pete"

We can use the %x format specifier to convert an int value to a string: 我们可以使用%x格式指定器将int值转换为字符串

>>> num = 5
>>> 'I have %x apples' % num
"I have 5 apples"

Note: For new code, using str.format or f-strings (Python 3.6+) is strongly recommended over the % operator.

注意: 对于新代码,强烈推荐使用 str.format 或 f-strings (Python 3.6+) 而不是 % 操作符。

String Formatting (str.format) 字符串格式化

Python 3 introduced a new way to do string formatting that was later back-ported to Python 2.7. This makes the syntax for string formatting more regular.

Python 3 引入了一种新的字符串格式化方法,后来被向后移植到 Python 2.7 中。这使得字符串格式化的语法更加规范。

>>> name = 'John'
>>> age = 20'

>>> "Hello I'm {}, my age is {}".format(name, age)
"Hello I'm John, my age is 20"
>>> "Hello I'm {0}, my age is {1}".format(name, age)
"Hello I'm John, my age is 20"

The official Python 3.x documentation recommend str.format over the % operator:

Python 3.x 官方文档推荐使用 str.format,而不是%操作符

The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly).
Using the newer formatted string literals or the str.format() interface helps avoid these errors.
These alternatives also provide more powerful, flexible and extensible approaches to formatting text.

这里描述的格式化操作表现出各种怪异的地方,导致了很多常见的错误(例如无法正确显示图元和字典)。
使用较新的格式化字符串字段或 str.format() 接口有助于避免这些错误。
这些替代方法还提供了更强大、更灵活、更可扩展的文本格式化方法。

Lazy string formatting

You would only use %s string formatting on functions that can do lazy parameters evaluation, the most common being logging:

只能在可以进行 lazy parameters evaluation 的函数上使用%s字符串格式化。最常见的是日志。

Prefer:

>>> name = "alice"
>>> logging.debug("User name: %s", name)

Over:

>>> logging.debug("User name: {}".format(name))

Or:

>>> logging.debug("User name: " + name)

Formatted String Literals or f-strings (Python 3.6+) f-字符串

>>> name = 'Elizabeth'
>>> f'Hello {name}!'
'Hello Elizabeth!

It is even possible to do inline arithmetic with it:

甚至可以用它来做单行算术

>>> a = 5
>>> b = 10
>>> f'Five plus ten is {a + b} and not {2 * (a + b)}.'
'Five plus ten is 15 and not 30.'

Template Strings 模板字符串

A simpler and less powerful mechanism, but it is recommended when handling format strings generated by users. Due to their reduced complexity template strings are a safer choice.

一个比较简单,功能不强的机制,但在处理用户生成的格式字符串时,建议使用。由于其复杂性降低,模板字符串是一个比较安全的选择。

>>> from string import Template
>>> name = 'Elizabeth'
>>> t = Template('Hey $name!')
>>> t.substitute(name=name)
'Hey Elizabeth!'