Python3
基础语法
保留字
顾名思义,这些名称已被使用,可查看保留字列表
import keyword
print(keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
注释
使用#注释
# 这是单行注释
使用'''或者"""代表多行注释
'''
我就看看不做事
'''
行与缩进
用缩进来代表代码块,如果缩进不对,程序报错
用户输入
input()
x = input('请输入姓名')
print('输入内容:' + x)
同一行显示多条语句
用;分隔
代码组
像if、while、def和class这样的复合语句,首行以关键字开始,以冒号( : )结束
输出
print(),默认换行,不换行需要在变量末尾加上 end=""
print('hello world!')
print('hello world!', end="")
print('你好')
hello world!
hello world!你好
导入模块
用 import 或者 from...import
导入模块import somemodule
导入模块中某个方法from somemodule import somefunction
import sys
print ('path', sys.path)
from sys import path
print ('path', path)
基本数据类型
变量无需定义,但需要在使用变量前使用=定义
多个变量赋值
a = b = c = 1
print(a)
print(b)
print(c)
a,b,c = 1,2,'test'
print(a)
print(b)
print(c)
标准数据类型
- 不可变数据(3 个):Number(数字)、String(字符串)、Tuple(元组)
- 可变数据(3 个):List(列表)、Dictionary(字典)、Set(集合)
Number 数字
支持 int、float、bool、complex(复数)
- type() 可以查询类型
- isinstance()判断类型
isinstance 和 type 的区别在于:
- type()不会认为子类是一种父类类型。
- isinstance()会认为子类是一种父类类型。
>>> a=1
>>> isinstance(a, int)
True
>>> type(a)
<class 'int'>
>>> isinstance(a, bool)
False
>>> class A:
... pass
...
>>> class B(A):
... pass
...
>>> isinstance(B(), B)
True
>>> isinstance(B(), A)
True
>>> type(B()) == B
True
>>> type(B()) == A
False
bool 是 int 的子类,True 和 False 可以和数字相加, True==1、False==0 会返回 True。
>>> issubclass(bool, int)
True
>>> True + 1
2
del语句删除单个或多个对象
>>> a = 1
>>> del a
>>> a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
数值运算
- +,-,*,**,/,//,%
- / 返回一个浮点数,// 返回一个整数
>>> 1+1
2
>>> 2-1
1
>>> 2*3
6
>>> 2**3
8
>>> 4/3
1.3333333333333333
>>> 4//3
1
>>> 6%5
1
String(字符串)
用单引号 ' 或双引号 " 括起来,同时使用反斜杠 \ 转义特殊字符
字符串的截取:变量[头下标:截取长度]
,第二个参数默认是字符串长度
>>> a = 'hello,world'
>>> print(a[0:5])
hello
>>>
加号 + 是字符串的连接符, 星号 * 表示复制当前字符串,与之结合的数字为复制的次数
>>> a = 'hello,world'
>>> b = 'test'
>>> print(a + ',' + b)
hello,world,test
使用反斜杠 \ 转义特殊字符,如果不想转义,在字符串前面添加一个r,表示原始字符串
>>> print('ab\nc')
ab
c
>>> print(r'ab\nc')
ab\nc
List(列表)
- 列表是写在方括号 [] 之间、用逗号分隔开的元素列表
- 列表被截取后返回一个包含所需元素的新列表,变量[头下标:截取长度]
- 加号 + 是列表连接运算符,星号 * 是重复操作
- 列表中的元素是可以改变的
>>> a = [1,2,3,4]
>>> print(a[0:2])
[1, 2]
>>> b = [5,6,7,8]
>>> print(a+b)
[1, 2, 3, 4, 5, 6, 7, 8]
>>> print(a*3)
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
>>> a[1]=3
>>> print(a)
[1, 3, 3, 4]
Tuple(元组)
元组写在小括号 () 里,元素之间用逗号隔开
- 元组不可修改
- 可以被索引和切片
- 加号 + 是列表连接运算符,星号 * 是重复操作
- 构造包含 0 个或 1 个元素的元组比较特殊
>>> a = (1,) # 1个元素需要加逗号
>>> a
(1,)
>>> b = ()
>>> b
()
>>> c = (1, 1.1, 'hello')
>>> c[0:1]
(1,)
>>> c[0:2]
(1, 1.1)
>>> c[0:3]
(1, 1.1, 'hello')
>>> c[1:1]
()
>>> c[1:2]
(1.1,)
>>> c[1:3]
(1.1, 'hello')
Set(集合)
- 进行成员关系测试和删除重复元素
- 大括号 { } 或者 set() 函数创建集合,注意:创建空集合必须用 set()
- 集合运算,- a 和 b 的差集,|并集,& 交集,^ 不同时存在
>>> a = set('abcda')
>>> a
{'d', 'b', 'c', 'a'}
>>> 'a' in a
True
>>> b = set('adfg')
>>> a - b
{'c', 'b'}
>>> a | b
{'b', 'a', 'g', 'c', 'f', 'd'}
>>> a & b
{'d', 'a'}
>>> a ^ b
{'b', 'g', 'c', 'f'}
Dictionary(字典)
- 字典用 { } 标识,它是一个无序的 键(key) : 值(value) 的集合
- 字典的关键字必须为不可变类型,且不能重复
- dict() 可以直接从键值对序列中构建字典
>>> a = {'name':'jack','age':20}
>>> a['name']
'jack'
>>> a.keys()
dict_keys(['name', 'age'])
>>> a.values()
dict_values(['jack', 20])
>>> dict(a=1, b=2, c=3)
{'a': 1, 'b': 2, 'c': 3}
数据类型转换
- 隐式类型转换 - 自动完成
- 显式类型转换 - 需要使用类型函数来转换
显式类型转换
- int() 强制转换为整型
- float() 强制转换为浮点型
- str() 强制转换为字符串类型
>>> int(1.5)
1
>>> int(1)
1
>>> float(1)
1.0
>>> float(1.44)
1.44
>>> str(1)
'1'
>>> str('a')
'a'
推导式
- 列表(list)推导式
- 字典(dict)推导式
- 集合(set)推导式
- 元组(tuple)推导式
>>> a = ['a','b']
>>> b = [x*2 for x in a]
>>> b
['aa', 'bb']
>>> c = {key:len(key) for key in b}
>>> c
{'aa': 2, 'bb': 2}
>>> d = {i**2 for i in [1,2,3]}
>>> d
{1, 4, 9}
>>> a = (x for x in range(10))
>>> a
<generator object <genexpr> at 0x1078a8eb0>
>>> tuple(a)
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
流程控制
if
if可以有多个elif(else if的缩写)只能有1个else
if x < 0:
x = 0
elif x < 10:
x = 2 * x
else:
x = 100
for
arr = [1,2,3]
for x in arr:
print(x)
1
2
3
range
range()用于数值序列
- range(stop)
- range(start, stop[, step])
list()用于生成数组
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(1, 11))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> list(range(0, 30, 5))
[0, 5, 10, 15, 20, 25]
break 和 continue
break跳出循环
for x in range(10):
if x % 2:
print('no ok')
break
else:
print('ok')
continue就是继续
for x in range(10):
if x % 2:
print('ok')
continue
print(x)
函数定义
def 关键词开头,后接函数标识符名称和圆括号 ()
>>> def hello():
... print('hi')
...
>>> hello()
hi
参数
必需参数
>>> def printme(str):
... print(str)
...
>>> printme()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: printme() missing 1 required positional argument: 'str'
关键字参数
传参数顺序可以变化
>>> def printme(x, y):
... print(x+y)
...
>>> printme(y='a',x='b')
ba
默认参数
如果没有传递参数,则会使用默认参数
>>> def hi(str='hi'):
... print(str)
...
>>> hi()
hi
不定长参数
星号 * 的参数会以元组(tuple)的形式导入,存放所有未命名的变量参数
>>> def printinfo(arg, *args):
... print(arg)
... for x in args:
... print(x)
...
>>> printinfo('a', 'b', 'c')
a
b
c
两个星号 ** 的参数会以字典的形式导入
>>> def star(x,**y):
... print(x)
... print(y)
...
>>> star(1, a=2, b=3)
1
{'a': 2, 'b': 3}
匿名函数
lambda 来创建匿名函数
>>> sun = lambda x,y:x+y
>>> sun(10,20)
30
return 语句
>>> def x(a,b):
... return a+b
...
>>> b = x('a', 'b')
>>> b
'ab'
强制位置参数
/ 用来指明函数形参必须使用指定位置参数,星号 * 的参数必须为关键字形参:
>>> def abc(a,b,/,c,d,*,e):
... print(a+b+c)
...
>>> abc(1,2,3,d=4,e=5)
6
File(文件) 方法
open() 函数常用形式是接收两个参数:文件名(file)和模式(mode)。
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
f = open('README.txt', 'w')
f.write('下班啦')
f.close()
正则表达式
re 模块,它提供 Perl 风格的正则表达式模式
re.match函数
re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回None。
import re
>>> res = re.match('WWW', 'www.baidu.com', re.I).span()
>>> res
(0, 3)
>>> a = re.match('x', 'abc')
>>> print(a)
None
re.search方法
re.search 扫描整个字符串并返回第一个成功的匹配。
re.match与re.search的区别,re.match 只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回 None,而 re.search 匹配整个字符串,直到找到一个匹配。
>>> a = re.search('x', 'abc')
>>> print(a)
None
>>> res = re.search('com', 'www.baidu.com', re.I)
>>> print(res)
<re.Match object; span=(10, 13), match='com'>
>>> res = re.match('com', 'www.baidu.com', re.I)
>>> print(res)
None
检索和替换
re.sub用于替换字符串中的匹配项。
>>> phone='电话号码:123456789'
>>> print(re.sub(r'\d', '*', phone))
电话号码:*********
compile 函数
编译正则表达式,生成一个正则表达式( Pattern )对象,供 match() 和 search() 这两个函数使用
findall
在字符串中找到正则表达式所匹配的所有子串,并返回一个列表
>>> re.compile('x').findall('xyxy')
['x', 'x']
finditer
和 findall 类似,在字符串中找到正则表达式所匹配的所有子串,并把它们作为一个迭代器返回。
>>> it = re.finditer('x', 'xyzxabcx')
>>> for x in it:
... print(x.group())
...
x
x
x
re.split
split 方法按照能够匹配的子串将字符串分割后返回列表
>>> it = re.split('x', 'xyzxabcx')
>>> it
['', 'yz', 'abc', '']
发送邮件
使用内置smtplib工具,官方文档
电子邮件消息 官方文档
发送邮件,使用到2个核心模块
- email.mime: 从头创建电子邮件和 MIME 对象
- email.header: 国际化标头
发送邮件,包含本地图片,本地附件的完整示例
import smtplib
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
# 第三方 SMTP 服务
host = 'smtp.xxx.com' # 设置服务器
user = 'xxx@xxx.com' # 用户名
pwd = 'xxxxxxxxxxx' # 口令
port = 465 # 端口号
# 接收邮件
receivers = ['xxx@xxx.com']
# 邮件正文内容
Msg = """
<p>Python 邮件发送测试...</p>
<p>图片演示:</p>
<p><img src="cid:image1" /></p>
"""
# 发件人,收件人,标题
From,To,Subject = 'Python','小白鼠','Python SMTP 邮件发送'
Filename,Imagename = 'test.txt','test.png'
# 指定图片为当前目录
fp = open(Imagename, 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
# 定义图片 ID,在 HTML 文本中引用
msgImage.add_header('Content-ID', '<image1>')
# 创建一个复杂实例
message = MIMEMultipart()
# 构造附件1,传送当前目录下的文件
att1 = MIMEText(open(Filename, 'rb').read(), 'base64', 'utf-8')
att1.add_header('Content-Disposition', 'attachment', filename=Filename)
message.attach(att1)
message.attach(MIMEText(Msg, 'html', 'utf-8'))
message.attach(msgImage)
message['From'] = Header(From, 'utf-8')
message['To'] = Header(To, 'utf-8')
message['Subject'] = Header(Subject, 'utf-8')
try:
server = smtplib.SMTP_SSL(host, port)
server.login(user, pwd)
server.sendmail(user, receivers, message.as_string())
server.quit()
print('邮件发送成功')
except smtplib.SMTPException as e:
print(str(e))
JSON 数据解析
- json.dumps 转为json字符串
- json.loads 转为dics
- json.dump 输出json文件
- json.load 读取json文件
import json
# Python 字典类型转换为 JSON 对象
data1 = { 'no': 1, 'name': 'xiaoming', 'age': 20 }
json_str = json.dumps(data1, sort_keys=True, indent=2)
print(json_str)
# 写入 JSON 数据
with open('data.json', 'w') as f:
json.dump(data1, f)
# 读取数据
with open('data.json', 'r') as f:
data = json.load(f)
日期和时间
Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间
时间值由 gmtime(),localtime() 和 strptime() 返回,并被 asctime(), mktime() 和 strftime() 接受,是一个 9 个整数的序列。
time
当前时间time.time()
本地时间time.localtime()
格式化时间time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
calendar
获取某月的日历
import calendar
cal = calendar.month(2022, 7)
print ("以下输出2022年7月份的日历:")
print (cal)
July 2022
Mo Tu We Th Fr Sa Su
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
pip
pip 是 Python 包管理工具
- python3 -m pip install xxx
- python3 -m pip uninstall xxx
- python3 -m pip list
math
import math
dir(math)
requests
需要安装依赖包 requests
python3 -m pip install requests
# 导入 requests 包
import requests
# 发送请求
x = requests.get('https://www.runoob.com/')
# 返回网页内容
print(x.text)
爬虫
python3 -m pip install beautifulsoup4
解析网页数据,find_all查找内容
import requests
from bs4 import BeautifulSoup
# 发送请求
x = requests.get('https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/')
#创建一个BeautifulSoup解析对象
soup = BeautifulSoup(x.text, "html.parser")
codes = soup.find_all('div', class_='highlight')
print('保存演示代码')
f = open('code.txt', 'w')
for code in codes:
f.write(code.get_text())
f.close()