python命令帮助 pyc文件生成 python特殊语法 python3新特性 jupyter使用介绍

2015-04-10 22:07:00
admin
原创 3162
摘要:python命令帮助 pyc文件生成 python特殊语法 python3新特性 jupyter使用介绍

一、python命令帮助

1、python --help,查看帮助;

2、python --version,查看版本;

3、pip,python的包管理工具;

4、PyPI,Python Package Index,python官方的第三方库仓库;


执行系统模块:

1、系统模块从sys.path查找;

2、执行方法python -m module;


优雅展示json,展示中文需要使用json模块编写代码:

echo '{"key":"value"}' | python -m json.tool


离线安装模块:

pip download requests -i https://pypi.tuna.tsinghua.edu.cn/simple/,从清华源下载模块;
pip install --no-index --find-links=file:requests requests,安装下载到本地的模块;


二、pyc文件生成

1、pyc文件是py文件编译后生成,是一种跨平台的字节码,加载和执行速度非常快;

2、pyc文件不能看到源码,所以商业软件可以通过发布pyc文件防止源码泄露;

3、pyc文件跟python版本相关,基本不能向前兼容,旧版本python不能执行新版本pyc文件;


生成单个pyc文件:

import py_compile

py_compile.compile('pyfile')


批量生成pyc文件:

import compileall
compileall.compile_dir('pydir')


禁止生成pyc文件:

import sys
sys.dont_write_bytecode = True


三、python特殊语法

1、三元运算符:true_expression if condition else false_expression

2、列表乘法:ints = [i*2 for i in range(5)]


四、python3新特性

1、官方已经不再支持python2,写代码需要使用python3;

2、Scripts目录下的pip.exe、pip3.exe、pip3.x.exe是同一程序;

3、print变成函数,函数格式是print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False);

4、f-string是一种新的字符串格式化方法,可以在字符串中直接引用变量,格式化功能非常强大;

5、bytes是字节数组,string编码可以转换为bytes,bytes解码可以转换为string;

6、Union是一种类型注解,表示一个变量属于多种类型中的一种,灵活且安全;

7、默认除法是浮点数除法,结果是float类型,双斜杠除法是整数除法;


创建bytes:

bs1 = (0x0488ADE4).to_bytes(4, "big")
bs2 = bytes.fromhex("0488ADE4")
bs3 = b"\x04\x88\xad\xe4"
bs4 = bytes([i for i in range(0, 10)])
bs5 = bytes(8)#length is 8
print(bs1)
print(bs2)
print(bs3)
print(bs4)
print(bs5)


修改bytes:

msg = 'message'
bs = msg.encode()
bytearr = bytearray(bs)
bytearr[0] = ord('0')
print(bytes(bytearr))


连接bytes:

b"".join([b1,b2,b3])


五、jupyter使用介绍

1、jupyter是一个交互式笔记本,可以交互式执行命令,非常适合科学计算;

2、ipython是交互式python,内核是ipykernal,交互执行能力非常强大;

3、jupyter安装:pip.exe install jupyter

4、jupyter启动:jupyter.exe notebook --notebook-dir jupyterwork

发表评论
评论通过审核之后才会显示。