一、Python环境的安装
1. 下载Python
- 访问Python官网: Welcome to Python.org
- 点击downloads按钮,在下拉框中选择系统类型(windows/Mac OS/Linux等)
- 选择下载最新版本的Python

2. 安装Python
- 双击下载好的Python安装包
- 勾选左下角 Add Python 3.7 to PATH 选项,然后选择 Install now 立刻安装Python.
- 默认安装

- 自定义安装


- 安装完成
3. 测试是否安装成功
- 点击电脑左下角开始按钮,输入 cmd 进入到windows的命令行模式。
- 在命令行中输入Python,正确显示Python版本,即表示Python安装成功

- 如果在命令行中输入python出现如下错误

可能是因为在安装Python的过程中没有勾选 Add Python 3.7 to PATH 选项,此时需要手动对Python进行配置。
4. 手动配置Python
注意:如果在安装过程中,已经勾选了 Add Python 3.7 to PATH 选项,并且在 cmd 命令模式下输入 python 指令
不报错,就不需要再手动的配置Python.
- 右键 此电脑 --> 选择 属性

- 选择 高级系统设置 --> 环境变量 -->找到并且双击 Path

- 双击 Path ,在弹框里点击新建,找到Python的安装目录,把路径添加进去

- 这里新添加的路径 E:\python 是Python安装好以后, Python.exe 这个可执行文件所在的目录。

二、pip的使用
pip 是一个现代的,通用的Python包管理工具。提供了对 Python 包的查找、下载、安装、卸载的功能,便于我们对Python的资源包进行管理。
- 安装
- 在安装Python时,会自动下载并且安装pip.
- 配置
- 在windows命令行里,输入 pip -V 可以查看pip的版本。

- 如果在命令行里,运行 pip -V ,出现如下提示:

可能是因为在安装python的过程中未勾选 Add Python 3.7 to PATH 选项,需要手动的配置pip的环境变量。
- 右键 此电脑 --> 环境变量 -->找到并且双击 Path -->在弹窗里点击新建-->找到pip的安装目录,把路径添加进去。

- 这里新添加的路径 E:\python\Scripts 是Python安装好以后, pip.exe 这个可执行文件所在的目录。

- 使用pip管理Python包
- pip install <包名> 安装指定的包
- pip uninstall <包名> 删除指定的包
- pip list 显示已经安装的包
- pip freeze 显示已经安装的包,并且以指定的格式显示
- 修改pip下载源
运行pip install 命令会从网站上下载指定的python包,默认是从 https://files.pythonhosted.org/ 网站上下
载。这是个国外的网站,遇到网络情况不好的时候,可能会下载失败,我们可以通过命令,修改pip现在软件时的
源。 格式:
pip install 包名 -i 国内源地址
示例: pip install ipython -i https://pypi.mirrors.ustc.edu.cn/simple/ 就是从中国科技大学(ustc)的服务器
上下载requests(基于python的第三方web框架)
国内常用的pip下载源列表:
阿里云 http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣(douban) http://pypi.douban.com/simple/
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
三、运行Python程序
1. 终端运行
- 直接在python解释器中书写代码

- 退出python环境
exit()
ctrl + z ==>enter
- 使用ipython解释器编写代码
使用pip命令,可以快速的安装IPython.
- 运行python文件
pip install ipython

2. 运行python文件
使用python指令运行后缀为.py的python文件

3. Pycharm
尽管上面介绍的方法已经能够提高我们的编码速度,但是仍然无法应对我们开发中更加复杂的要求。一般情况下,
我们都需要借助工具来辅助我们快速的搭建环境,编写代码以及运行程序。
IDE的概念
IDE(Integrated Development Environment)又被称为集成开发环境。说白了,就是有一款图形化界面的软件,它集成了编辑代码,编译代码,分析代码,执行代码以及调试代码等功能。在我们Python开发中,最常用的IDE是Pycharm.
pycharm由捷克公司JetBrains开发的一款IDE,提供代码分析、图形化调试器,集成测试器、集成版本控制系统等,主要用来编写Python代码。
下载地址:http://www.jetbrains.com/pycharm/download
Pycharm的安装
- 双击安装文件
- 自定义安装路径(可以不用设置

编辑设置(全部选中

安装完成后双击

设置主题

启动软件

4. Pycharm的使用介绍
运行Pycharm,选择 Create New Project ,创建一个新的Python工程

选择'Pure Python'创建一个新的纯Python工程项目, Location 表示该项目的保存路径, Interpreter 用来指定Python解释器的版本。

右击项目,选择 New ,再选择 Python File

在弹出的对话框中输入的文件名 HelloPython ,点击OK,表示创建一个Python程序的文本文件,文本文件后缀名默认.py

在新建的 01_尚硅谷_firstPythonDemo.py 文件里,输入以下代码,并在空白处右键选择 Run 运行,表示输出一段 尚硅谷爬虫 字符串。

运行成功后,Pycharm Console窗口将显示我们的输出结果.

页面布局介绍

文件导航区域 能够浏览/定位/ 打开 项目文件
文件编辑区域 能够编辑 当前打开的文件
控制台区域能够:
- 输出程序执行内容
- 跟踪调试代码的执行
四、Python
1.1 注释
1.1 注释介绍
在我们工作编码的过程中,如果一段代码的逻辑比较复杂,不是特别容易理解,可以适当的添加注释,以辅助自己
或者其他编码人员解读代码。

有注释的代码

1.2 注释的分类
在Python中支持单行注释和多行注释。
单行注释以#开头,#右边的所有东西当做说明,而不是真正要执行的程序,起辅助说明作用。
多行注释以 ''' 开始,并以 ''' 结束,我们称之为多行注释。
#开头右边的都是注释,解析器会忽略注释
print('hello world') #我的作用是在控制台输出hello world

2. 变量以及数据类型
2.1 变量的定义
思考下列代码有什么问题?


说明:
- 变量即是可以变化的量,可以随时进行修改。
- 程序就是用来处理数据的,而变量就是用来存储数据的。







4. 类型转换









5.3 复合赋值运算符

5.4 比较运算符
以下假设变量a为10,变量b为20:





在程序中,看到了%这样的操作符,这就是Python中格式化输出














python
# len length的缩写
s = 'china'
print(len(s))
s1 = 'china'
print(s1.find('c'))
s2 = 'china'
print(s2.startswith('h'))
print(s2.endswith('a'))
s3 = 'aaabbb'
print(s3.count('a'))
s4 = 'baaaa'
print(s4.replace('b','a'))
s5 = '1#2#3#4'
print(s5.split('#'))
s6 ='china'
print(s6.upper())
s7 = 'CHINA'
print(s7.lower())
s8 = ' a '
print(len(s8))
print(len(s8.strip()))
s9 = 'hello'
print(s9.join('hello'))




































