python的安装——官网下载
版本:python2.7
配置环境——可从cmd中直接打开python
未安装
在path路径中添加python的环境变量
1.计算机——属性——高级系统设置——环境变量
2.系统变量——新建pythonroot 变量值为python的安装位置
3.将pythonroot路径加在Path后面 变量值:******;%Pythonroot%;
确定即可
再次打开cmd
输入 python即可
版本:python3.10 无需配置环境 安装时勾选Add Python 3.10 to Path即可
install
命令行直接键入pip install + 回车,则 出现如下提示:ERROR: You must give at least one requirement to install (see "pip help install")。接着我们键入pip help install,就会出现pip install的使用说明了
Usage:
pip <command> [options]
Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
list List installed packages.
show Show information about installed packages.
check Verify installed packages have compatible dependencies.
config Manage local and global configuration.
search Search PyPI for packages.
cache Inspect and manage pip's wheel cache.
index Inspect information available from package indexes.
wheel Build wheels from your requirements.
hash Compute hashes of package archives.
completion A helper command used for command completion.
debug Show information useful for debugging.
help Show help for commands.
General Options:
-h, --help Show help.
--debug Let unhandled exceptions propagate outside the main subroutine, instead of logging them
to stderr.
--isolated Run pip in an isolated mode, ignoring environment variables and user configuration.
--require-virtualenv Allow pip to only run in a virtual environment; exit with an error otherwise.
-v, --verbose Give more output. Option is additive, and can be used up to 3 times.
-V, --version Show version and exit.
-q, --quiet Give less output. Option is additive, and can be used up to 3 times (corresponding to
WARNING, ERROR, and CRITICAL logging levels).
--log <path> Path to a verbose appending log.
--no-input Disable prompting for input.
--proxy <proxy> Specify a proxy in the form scheme://[user:passwd@]proxy.server:port.
--retries <retries> Maximum number of retries each connection should attempt (default 5 times).
--timeout <sec> Set the socket timeout (default 15 seconds).
--exists-action <action> Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup,
(a)bort.
--trusted-host <hostname> Mark this host or host:port pair as trusted, even though it does not have valid or any
HTTPS.
--cert <path> Path to PEM-encoded CA certificate bundle. If provided, overrides the default. See 'SSL
Certificate Verification' in pip documentation for more information.
--client-cert <path> Path to SSL client certificate, a single file containing the private key and the
certificate in PEM format.
--cache-dir <dir> Store the cache data in <dir>.
--no-cache-dir Disable the cache.
--disable-pip-version-check
Don't periodically check PyPI to determine whether a new version of pip is available for
download. Implied with --no-index.
--no-color Suppress colored output.
--no-python-version-warning
Silence deprecation warnings for upcoming unsupported Pythons.
--use-feature <feature> Enable new functionality, that may be backward incompatible.
--use-deprecated <feature> Enable deprecated functionality, that will be removed in the future.
总结来说,安装命令就是:pip install <包名> 或 pip install -r requirements.txt(对于本地安装包可以指定路径)。唯一需要特殊说明的是,安装时可以指定版本号来安装,举例如下:
pip install SomePackage # 最新版本
pip install SomePackage==1.0.4 # 指定版本
pip install 'SomePackage>=1.0.4' # 最小版本
uninstall
卸载安装包命令:pip uninstall <包名> 或 pip uninstall -r requirements.txt
升级包
pip install -U <包名> 或:pip install <包名> --upgrade
freeze
pip freeze 查看已经安装的包及版本信息。导出到指定文件中。例如,pip freeze > requirements.txt,文件名称随意;也可以使用 pip install -r requirements.txt,两者等效。
list
列出当前已经安装的包。使用命令pip list -o则可查询可升级的包。
show
显示包所在目录及信息,格式为:pip show <包名>。如果不加包名,则提示ERROR: Please provide a package name or names.。
目前,pip已经成为主流的安装工具,自Python2 >=2.7.9或者Python3.4以后默认都安装有pip。
基础语法
标识符
- 第一个字符必须是字母表中字母或下划线 _ 。
- 标识符的其他的部分由字母、数字和下划线组成。
- 标识符对大小写敏感。
数字(Number)类型
python中数字有四种类型:整数、布尔型、浮点数和复数。
- int (整数), 如 1, 只有一种整数类型 int,表示为长整型,没有 python2 中的 Long。
- bool (布尔), 如 True。
- float (浮点数), 如 1.23、3E-2
- complex (复数), 如 1 + 2j、 1.1 + 2.2j
字符串
- Python 中的字符串有两种索引方式,从左往右以 0 开始,从右往左以 -1 开始。
- Python 中的字符串不能改变。
- Python 没有单独的字符类型,一个字符就是长度为 1 的字符串。
- 字符串的截取的语法格式如下:变量[头下标:尾下标:步长]
input的语法及用法
参数说明
tips:提示信息,一般用引号引起来提示输出。
(2)用法:运行代码之后可自定义输入目标数据,返回结果为str(字符串)类型。
等待用户输入
执行下面的程序在按回车键后就会等待用户输入:
实例(Python 3.0+)
#!/usr/bin/python3 input("\n\n按下 enter 键后退出。")
以上代码中 ,\n\n 在结果输出前会输出两个新的空行。一旦用户按下 enter 键时,程序将退出。
print 输出
print 默认输出是换行的,如果要实现不换行需要在变量末尾加上 end="":