Python 之 for…else循环
用于判定 循环是否触发内部的break
import math
for n in range(99,0,-1):
root = math.sqrt(n)
if root==int(root):
print (n)
break
else:
print("触发 break, 不输出")
for n in range(99,81,-1):
root = math.sqrt(n)
if root==int(root):
print (n)
break
else:
print("循环完毕,没有触发break")
Python 之 repr格式输出
输出带引号的字串
a = "Hello Word"
print (repr(a))
Python 之 过滤器
import copy
res = [n for n in dir(copy) if not n.startswith("_")]
reload
python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报错UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0x?? in position 1: ordinal not in range(128),python没办法处理非ascii编码的,此时需要自己设置python的默认编码,一般设置为utf8的编码格式。
在程序中加入以下代码:即可将编码设置为utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')