判断是否为空:
if example is None:
pass
if example is not None:
pass
# 必须清楚example等于None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()时对判断没有影响才行
判断是否在某个列表里:
example = [ 1, 6, 3, 5, 3, 4 ]
if 5 in example:
pass
写文件
with open("文件名", 'wb', encoding='utf8') as file:
file.write(...)
wb 写进去的是二进制格式,所以这里写进的字符有时候需要.encode()一下,只能写进字符串,别把数组啊、列表啊放进来了
如果写进去的是个字符串,也可以file.write(b"xxx")
w 覆盖原内容的写
w+ 覆盖原内容的读写
r 只读
a 追加
将列表转成字符串
example = [ 1, 6, 3, 5, 3, 4 ] # 列表、序列均可以
str = "-".join(example)
print(str) # 输出的是字符串:1-6-3-5-3-4
== ( != ) 和is ( is not )的区别
example = []
if example is []:
print("相同")
print(example == [])
# 输出的是:True
# 也就是说,==比较的是值,is比较的是值和内存地址
String操作
print(str[0:-1]) # 输出str字符串的 第1个 到 倒数第2个 的所有字符
print(str[2:5]) # 输出str字符串的 第3个 到 第5个 的字符
print(str[2:]) # 输出str字符串的第3个开始后的所有字符
r 屏蔽字符串中的转义字符
print('hello\n') # 输出hello和下一个空行
print(r'hello\n') # 输出hello\n
输入
str = input("请输入:")
print(str)