Python 备忘单是 Python 3 编程语言的单页参考表
">>>"是Python 交互式解释器(命令行) 的提示符
| :- | :- |
|---|---|
str | Text |
int, float, complex | Numeric |
list, tuple, range | Sequence |
dict | Mapping |
set, frozenset | Set |
bool | Boolean |
bytes, bytearray, memoryview | Binary |
查看: Data Types
>>> msg = "Hello, World!"
>>> print(msg[2:5])
llo
查看: Strings
mylist = []
mylist.append(1)
mylist.append(2)
for item in mylist:
print(item) # 打印输出 1,2
查看: Lists
num = 200
if num > 0:
print("num is greater than 0")
else:
print("num is not greater than 0")
查看: 流程控制
for item in range(6):
if item == 3: break
print(item)
else:
print("Finally finished!")
查看: Loops
>>> def my_function():
... print("来自函数的你好")
...
>>> my_function()
来自函数的你好
查看: Functions
with open("myfile.txt", "r", encoding='utf8') as file:
for line in file:
print(line)
查看: 文件处理
result = 10 + 30 # => 40
result = 40 - 10 # => 30
result = 50 * 5 # => 250
result = 16 / 4 # => 4.0 (Float Division)
result = 16 // 4 # => 4 (Integer Division)
result = 25 % 2 # => 1
result = 5 ** 3 # => 125
/ 表示 x 和 y 的商,// 表示 x 和 y 的底商,另见 StackOverflow
┌───┬───┬───┬───┬───┬───┬───┐
| m | y | b | a | c | o | n |
└───┴───┴───┴───┴───┴───┴───┘
0 1 2 3 4 5 6 7
-7 -6 -5 -4 -3 -2 -1
>>> s = 'mybacon'
>>> s[2:5]
'bac'
>>> s[0:2]
'my'
>>> s = 'mybacon'
>>> s[:2]
'my'
>>> s[2:]
'bacon'
>>> s[:2] + s[2:]
'mybacon'
>>> s[:]
'mybacon'
>>> s = 'mybacon'
>>> s[-5:-1]
'baco'
>>> s[2:6]
'baco'
>>> s = '12345' * 5
>>> s
'1234512345123451234512345'
>>> s[::5]
'11111'
>>> s[4::5]
'55555'
>>> s[::-5]
'55555'
>>> s[::-1]
'5432154321543215432154321'
>>> website = 'Reference'
>>> f"Hello, {website}"
"Hello, Reference"
>>> num = 10
>>> f'{num} + 10 = {num + 10}'
'10 + 10 = 20'
>>> f"""He said {"I'm John"}"""
"He said I'm John"
>>> f'5 {"{stars}"}'
'5 {stars}'
>>> f'{{5}} {"stars"}'
'{5} stars'
>>> name = 'Eric'
>>> age = 27
>>> f"""Hello!
... I'm {name}.
... I'm {age}."""
"Hello!\n I'm Eric.\n I'm 27."
它从 Python 3.6 开始可用,另见: 格式化的字符串文字
>>> f'{-12345:0=10}' # 负数
'-000012345'
>>> f'{12345:010}' # [0] 快捷方式(不对齐)
'0000012345'
>>> f'{-12345:010}'
'-000012345'
>>> import math # [.precision]
>>> math.pi
3.141592653589793
>>> f'{math.pi:.2f}'
'3.14'
>>> f'{1000000:,.2f}' # [分组选项]
'1,000,000.00'
>>> f'{1000000:_.2f}'
'1_000_000.00'
>>> f'{0.25:0%}' # 百分比
'25.000000%'
>>> f'{0.25:.0%}'
'25%'
列表切片的语法:
a_list[start:end]
a_list[start:end:step]
>>> a = ['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a[2:5]
['bacon', 'tomato', 'ham']
>>> a[-5:-2]
['egg', 'bacon', 'tomato']
>>> a[1:4]
['egg', 'bacon', 'tomato']
>>> a[:4]
['spam', 'egg', 'bacon', 'tomato']
>>> a[0:4]
['spam', 'egg', 'bacon', 'tomato']
>>> a[2:]
['bacon', 'tomato', 'ham', 'lobster']
>>> a[2:len(a)]
['bacon', 'tomato', 'ham', 'lobster']
>>> a
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a[:]
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a[0:6:2]
['spam', 'bacon', 'ham']
>>> a[1:6:2]
['egg', 'tomato', 'lobster']
>>> a[6:0:-2]
['lobster', 'tomato', 'egg']
>>> a
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a[::-1]
['lobster', 'ham', 'tomato', 'bacon', 'egg', 'spam']