一张页面显示Python所有重要语法,便于速查,以下是摘录部分,全部速查表点击标题,Download text file
#Contents
ToC = { |
#Main
if <strong>name</strong> == '<strong>main</strong>': # Runs main() if file wasn't imported. |
#List
<list> = <list>[<slice>] # Or: <list>[from_inclusive : to_exclusive : ±step] |
<list>.insert(<int>, <el>) #在索引处插入项目,并将其他项目移到右边。 |
.
#Dictionary
<view> = <dict>.keys() # Coll. of keys that reflects changes. |
Counter
>>> from collections import Counter |
#Set
<set> = set() # <code>{}</code> returns a dictionary. |
#Tuple
Tuple元组是一个不可变且可散列的列表。
<tuple> = () # Empty tuple. |
Named Tuple
具有命名元素的元组子类。
>>> from collections import namedtuple |
#Range
不可变且可哈希的整数序列。
<range> = range(stop) # range(to_exclusive) |
#Enumerate
for i, el in enumerate(<collection> [, i_start]): |
#Iterator
<iter> = iter(<collection>) # <code>iter(<iter>)</code> returns unmodified iterator. |
Itertools
import itertools as it |
#Generator
任何包含yield语句的函数都返回生成器。
生成器和迭代器可以互换。
def count(start, step): |
#Type
一切都是对象。
每个对象都有一个类型。
类型和类是同义词。
<type> = type(<el>) # Or: <el>.<strong>class</strong> |
>>> type('a'), 'a'.<strong>class</strong>, str |
有些类型没有内置的名称,所以必须导入:
from types import FunctionType, MethodType, LambdaType, GeneratorType, ModuleType |
#String
<str> = <str>.strip() # 从两端去除所有空白字符。. |