杂记
1、想要py程序运行时不出现控制台,只需要把后辍改成:.pyw
2、第一行是 #! python3,没有#!行,你也能从 IDLE 运行 Python 脚本,但从命令行运行它们就需要这一行
3、断言:assert 帮助程序员测试代码用的 assert 'red' in ['green', 'yellow'], '警告,列表中没有你要的值'
4、启动外部浏览器
import webbrowser
a = webbrowser.open_new_tab('http://www.baidu.com')
5、乱码:
name = '╣╪╙┌╚φ╝■▒╗╬≤▒¿▓í╢╛╡─╦╡├≈'.encode('cp437').decode('gbk') print(name) # 关于软件被误报病毒的说明
6、覆盖打印:
print("\r",object,end="",flush=True) # object是要打印的内容,flush=True可以省略 # 进度条 import time def progress_bar(): for i in range(1, 101): print('\r', end='') print('Download progress:{: >2}%'.format(i), '▉' * (i // 2), end='') time.sleep(0.5) progress_bar()
7、sys.exit()只能结束当前线程,如果程序有多个线程的话此时程序不会结束
8、打开windows应用程序:
import subprocess subprocess.Popen('C:\\Windows\\System32\\calc.exe') subprocess.Popen(['C:\\Windows\\notepad.exe', 'C:\\hello.txt']) subprocess.Popen(['C:\\python34\\python.exe', 'hello.py']) # 运行其它py角本 subprocess.Popen(['start', 'hello.txt'], shell=True) # 用windows默认程序打开文件
9、一个典型的add运算
前面假返回前面,前面真返回后面
a = lambda x: x<100 and 255 print(a(278)) def fun(): return True and True print(fun())
10、if 表达式
flag = False print('jpg' if flag else 'png') # png

浙公网安备 33010602011771号