Python3 教程
- 第1节:Python3 简介
- 第2节:Python3 安装
- 第3节:Python3 使用上手
- 第4节:Python3 解释器
- 第5节:Python3 VSCode 配置
- 第6节:Python3 基础语法
- 第7节:Python3 编程第一步
- 第8节:Python3 基本数据类型
- 第9节:Python3 注释
- 第10节:Python3 运算符
- 第11节:Python3 数字
- 第12节:Python3 序列
- 第13节:Python3 字符串
- 第14节:Python3 列表
- 第15节:Python3 元组
- 第16节:Python3 字典
- 第17节:Python3 集合
- 第18节:Python3 条件控制
- 第19节:Python3 循环
- 第20节:Python3 迭代器与生成器
- 第21节:Python3 函数
- 第22节:Python3 数据结构
- 第23节:Python3 模块
- 第24节:Python3 输入和输出
- 第25节:Python3 File 方法
- 第26节:Python3 OS 文件/目录方法
- 第27节:Python3 错误和异常
- 第28节:Python3 面向对象
- 第29节:Python3 标准库概览
- 第30节:Python3 正则表达式
- 第31节:Python3 CGI 编程
- 第32节:Python3 MySQL 数据库连接
- 第33节:Python3 网络编程
- 第34节:Python3 SMTP发送邮件
- 第35节:Python3 多线程
- 第36节:Python3 XML解析
- 第37节:Python3 JSON 数据解析
- 第38节:Python3 日期和时间
- 第39节:Python3 内置函数
- 第40节:Python3 MongoDB
- 第41节:Python3 urllib模块
- 第42节:Python3 uWSGI 安装配置
- 第43节:Python3 pip
- 第44节:Python3 operator 模块
- 第45节:Python3 math 模块
- 第46节:Python3 requests 模块
- 第47节:Python3 random 模块
Python3 解释器
Linux/Unix 的系统上,Python 解释器通常被安装在 /usr/local/python3 这样的有效路径(目录)里。
我们可以将路径 /usr/local/python3/bin 添加到您的 Linux/Unix 操作系统的环境变量中(最好参照您 Python 的安装路径进行添加),这样您就可以通过 shell 终端输入下面的命令来启动 Python 。
$ PATH=$PATH:/user/local/python3/bin/python #设置环境变量
$ python3 --vesion
Python 3.7.6
在 Window 系统下你可以通过以下命令来设置 Python 的环境变量,假设你的 Python 安装在 C:\Python37 下:
set path=%path%;C:\python37
交互式编程
打开 IDLE,默认的就是交互式编程,类似于一个 cmd 窗口。
我们可以在命令提示符中输入 "Python" 命令来启动 Python 解释器:
$ python3
执行以上命令后,出现如下窗口信息:
$ python3
Python 3.4。0 (default, Mar 16 2014, 09:25:04)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
在 Python 提示符中输入以下语句,然后按回车键查看运行效果:
print ("Welcome to test!");
以上命令执行结果如下:
当键入一个多行结构时,续行是必须的。我们可以看下如下 if 语句:
>>> the_world_is_flat = True
>>> if the_world_is_flat:
... print("Be careful not to fall off!")
...
Be careful not to fall off!
脚本式编程
点击 File→New 后新建脚本文件,在此文件下进行的是脚本式编程。
将如下代码拷贝至 W3C.py 文件中:
print ("Welcome to test!");
通过以下命令执行该脚本:
python W3C.py
输出结果为:
Welcome to test!
在 Linux/Unix 系统中,你可以在脚本顶部添加以下命令让 Python 脚本可以像 SHELL 脚本一样可直接执行:
#! /usr/bin/env python3.7
然后修改脚本权限,使其有执行权限,命令如下:
$ chmod +x W3C.py
执行以下命令:
./hello.py
输出结果为:
Welcome to test!