1. 虚拟环境
Python的虚拟环境可用于管理项目的依赖,解决了不同Python项目间的依赖不同版本库(甚至是不同版本的Python)的问题。在虚拟环境中安装依赖库不会影响其他的项目,也不会影响操作系统中的Python包。
虚拟环境推荐在项目的根目录下创建。
创建虚拟环境:
# Windows PowerShell Python 3.6
# 创建虚拟环境
# python -m venv [path]
PS D:\vscode\python\flask-tutorial> python -m venv venv
# 激活虚拟环境
PS D:\vscode\python\flask-tutorial> .\venv\Scripts\activate
在激活环境时,可能会碰到“禁止运行脚本”,参考Powershell中禁止执行脚本解决办法。
集合
可变有序列表 list
list是一个可变有序列表。
# list 操作
# 定义,创建
>>> classmates = ['Micheal','Bob','Tracy']
>>> classmates
['Micheal', 'Bob', 'Tracy']
>>> len(classmates)
3
# 创建空list
>>> p=[]
>>> p
[]
>>> len(p)
0
# 创建一个0-9和一个90-99的数字组成的数组,使用列表生成器range
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(90,100))
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
>>>
# 索引
# 索引号取值范围:-len()至len()-1,第一个元素的索引号:0或-len(),最后一个元素的索引号:-1,len()-1
>>> classmates[0]
'Micheal'
>>> classmates[-1]
'Tracy'
>>> classmates[3]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> classmates[-13]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> classmates[-3]
'Micheal'
# 替换
>>> classmates[0]='David'
>>> classmates
['David', 'Bob', 'Tracy']
# 添加到结尾
>>> classmates.append('Admin')
>>> classmates
['David', 'Bob', 'Tracy', 'Admin']
# 插入
>>> classmates.insert(1,'Louis')
>>> classmates
['David', 'Louis', 'Bob', 'Tracy']
# 删除
>>> classmates.pop(1)
'Louis'
>>> classmates
['David', 'Bob', 'Tracy']
# 循环迭代
>>> for mate in classmates:
... print(mate)
...
David
Bob
Tracy
Admin
# 判断是否为可迭代对象
>>> from collections import Iterable
>>> isinstance(classmates, Iterable) # classmates是否可迭代
True
# 引用两个或多个变量
>>> for i,value in enumerate(classmates):
... print(i,value)
...
0 David
1 Bob
2 Tracy
3 Admin
>>>
>>> for x,y in [(1,220),(23,90),(0,9)]:
... print(x,y)
...
1 220
23 90
0 9
>>>
# 不同元素类型
>>> lst = ['admin',1,'100']
>>> lst
['admin', 1, '100']
# 多维list
>>> lst.insert(1,['admin',1,'100'])
>>> lst
['admin', ['admin', 1, '100'], 1, '100']
>>> lst[0]
'admin'
>>> lst[1]
['admin', 1, '100']
>>> lst[1][1]
1
# 切片
>>> lst = list(range(100))
>>> lst
[0, 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, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
# 取前10
>>> lst[:10]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# 取最后10个
>>> lst[-10:]
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
# 取索引11-20
>>> lst[10:20]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
# 每5个取一个
>>> lst[::5]
[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]
# 取全部
>>> lst[:]
不可变有序列表 tuple(元组)
tuple是一种特殊的不可变list。除了定义/创建方式、不可变,其他操作都与list相同。
# tuple 操作
# 定义,创建
>>> tpl = ('David','Bob','Tracy')
>>> tpl
('David', 'Bob', 'Tracy')
>>>
# 创建空元组
>>> tpl1 = ()
>>> tpl1
()
# 创建单个元素的元组
>>> tpl2 = (1)
>>> tpl2
1
>>> tpl3 = ('admin')
>>> tpl3
'admin'
# 以上两种情况,"()"被识别成普通的括号,而不是元组的符号,两个变量也被认为是普通的变量
# 正确的创建单个元组方式
>>> tpl2 = (1,)
>>> tpl2
(1,)
>>> tpl3 = ('admin',)
>>> tpl3
('admin',)
# 多维tuple,元素可变
>>> tpl
('David', [1, 2, 3, 4], 'Tracy')
>>> tpl[1][0]=0
>>> tpl
('David', [0, 2, 3, 4], 'Tracy')
dict(字典)
dict中存储的是key-value对,key在dict中是唯一的。
# 定义创建
>>> score = {'David':100,'Louis':88,'Bob':98}
>>> score
{'David': 100, 'Louis': 88, 'Bob': 98}
# 查找
# dict只能通过[key]或get(key)查找元素,而不能通过下标索引
>>> score['David']
100
>>> score[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 0
>>> score.get('Bob')
98
# 删除元素
>>> score.pop('Bob')
98
>>> score
{'David': 100, 'Louis': 88}
# 添加元素
>>> score['Jack']=67
>>> score
{'David': 100, 'Louis': 88, 'Jack': 67}
>>>
# 判断key是否存在
>>> 'David' in score
True
# 迭代
# 迭代key
>>> for k in score:
... print(k)
...
David
Louis
Jack
# 迭代value
>>> for k in score.values():
... print(k)
...
100
88
67
# 迭代key,value
>>> for k,v in score.items():
... print(k,v)
...
David 100
Louis 88
Jack 67
set
set中存储的是单key值,在set中的key是唯一的。
# 创建,set需要一个list集合作为输入
>>> st = set (classmates)
>>> st
{'Bob', 'David', 'Admin', 'Tracy'}
>>>
# 添加
>>> st.add('Louis')
>>> st
{'Bob', 'Louis', 'Admin', 'David', 'Tracy'}
# 删除
>>> st.remove('Louis')
>>> st
{'Bob', 'Admin', 'David', 'Tracy'}
# 逻辑运算
>>> st2 = set(['Bob','Jack'])
>>> st2
{'Bob', 'Jack'}
>>> st | st2
{'Jack', 'David', 'Tracy', 'Bob', 'Admin'}
>>> st & st2
{'Bob'}