软件测试所有内容笔记正在陆续更新中,笔记已经在本地记录,全部为自己手动记录的笔记及总结,正在开始更新中,后续会逐步更新并完善到
软件测试学习内容总结
专栏。
本节内容:Python编程语言与测试框架
Python历史
Python版本选择
Python的安装
Linux下Python环境变量配置
vim ~/.bash_profile
export PATH=$PATH:/usr/local/bin
$PATH–表示已经定义好的环境变量拿过来,在后面追加想要添加的环境变量Linux查看Python环境变量path
$PATH
Mac设置Python3的软连接(默认为Python2)
ln -f /usr/local/bin/python3.8 /usr/local/bin/python
Pycharm
pip help
查看pip帮助文档
pip介绍
pip是Python中的标准库管理器
Python3的3.4版本额以及Python2的2.7.9版本开始,pip直接包括在Python的安装包内。
pypi托管了大量的流行的库 www.pypi.org
安装包
pip install selenium==2.39.0
pip3 install jupyter -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
国内的pip源
阿里云:https://mirrors.aliyun.com/pypi/simple
清华:https://pypi.tuna.tsinghua.edu.cn/simple
豆瓣:https://pypi.douban.com/simple
Jupyter安装
pip install jupyter -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
–trusted-host 表示信任镜像源
id() 打印变量的储存地址
type() 查看变量的数据类型
命名规则
常用数字类型
常用运算符
分支结构的概念
Python分支结构
多重分支
分支嵌套
练习题
哪个写法更好?
循环结构的概念
简单语句组
break 和 continue 语句
函数的作用(封装)
函数的定义
参数是否还有其他的形式?
定义函数
的时候使用k=v
的形式定义。def func2(a=1):print('参数a的值为:',a)func2(2)
关键字参数
特殊参数
列表的特性
官网参考:https://docs.python.org/zh-cn/3/tutorial/datastructures.html#list-comprehensions
list.append(x):在列表末尾添加一个元素。
list.insert(i, x):在给定的位置插入一个元素。第一个参数是索引。
list.remove(x):移除列表中第一个值为 x 的元素。如果没有这个元素,抛出 ValueError 异常
list.pop([i]):删除列表中给定位置的元素并返回它。如果没有给定位置,a.pop() 将会删除并返回列表中的最后一个元素。
list.sort(key=None,reverse=False):对列表中的元素进行排序(参数可用于自定义排序,解释请参见 sorted())
list.reverse():反转列表中的元素
注意:
列表推导式
#列表生成式:
list_square = [i ** 2 for i in range(1, 4)]
print(list_square)# for 循环
list_square2 = []
for i in range(1, 4):list_square2.append(i ** 2)
print(list_square2)
list_squre3 = [i ** 2 for i in range(1, 4) if i != 1]
print(list_squre3)list_squre4 = [i * j for i in range(1, 4) for j in range(1, 4)]
print(list_squre4)list_squre4 = []
for i in range(1, 4):for j in range(1, 4):list_squre4.append(i * j)
# 2 元祖的定义
# type() 打印数据类型
tuple_t = (1, 2, 3)
tuple_t1 = 1, 2, 3print(tuple_t)
print(type(tuple_t))print(tuple_t1)
print(type(tuple_t1))
# 元祖的不可变特性 --变量指针不变
list1 = [1, 2, 3]
list1[0] = 'q'
print(list1)
tuple1 = (1, 2, 3)
tuple1[0] = 'q'
# id() 打印变量指针(内存地址)
# 元祖的不可变特性 --'变量指针' (内存地址)不变
# 元祖中嵌套列表则可变
a = [1, 2, 3]
tuple1 = (1, 2, a)print(id(tuple1[2]))
tuple1[2][0] = 'a'
print(id(tuple1[2]))
print(tuple1)
内置函数
# 内置函数
a = (1, 2, 3, 1)
print(a.count(1)) # 计算元素 1 在元祖中出现几次
print(a.index(1)) # 求对应元素的索引
print(a.index(1, 1, 4))
print(a)
# 集合定义
a = {1}
b = set()
print(len(b))
print(type(a))
print(type(b))
# 内置函数
a = {1, 2, 3}
b = {1, 4, 5}
print(a.union(b)) # 并集
print(a.intersection(b)) # 交集
print(a.difference(b)) # 差集 --a有b没有的元素
a.add('a') # 集合添加元素
print(a)
# 字符串去重后的集合
print({i for i in 'fjajgajdaddahdfdjhgjfaaa'})
# {'a', 'f', 'g', 'd', 'h', 'j'}c = 'jgirgokafgaaaaa'
print(set(c)) # {'i', 'k', 'g', 'r', 'a', 'o', 'f', 'j'}
# 字典定义
dict_d = {'a': 1, 'b': 2}
dict_d2 = dict(a=1, b=2)print(dict_d)
print(type(dict_d))
print(dict_d2)
print(type(dict_d2))
print(dict_d.keys()) # 得到所有 key
print(dict_d.values()) # 得到所有 valueprint(dict_d.pop('a')) # 1;返回value值,并删除key为a的键值对
print(dict_d) # {'b': 2}print(dict_d.popitem()) # popitem()随机删除键值对。返回被删除的键值对
print(dict_d)
a = {}
b = a.fromkeys((1, 2, 3), 'a') # 分别把元祖里面的参数当做key值,建立一个新的列表。
print(b) # {1: 'a', 2: 'a', 3: 'a'}# 列表推导式
print({i: i * 2 for i in range(1, 3)}) # {1: 2, 2: 4}
python的程序结构
模块
模块导入
模块分类
如何使用模块
常用方法
搜索路径
使用模块的总结
字面量
字面量插值
print("we are the {} and {}".format('Tom','Jerry'))
======= =====================================
Character Meaning
------------ ---------------------------------------------------------------
'r' open for reading (default)
'w' open for writing, truncating the file first
'x' create a new file and open it for writing
'a' open for writing, appending to the end of the file if it exists
'b' binary mode
't' text mode (default)
'+' open a disk file for updating (reading and writing)
'U' universal newline mode (deprecated)
====== ====================================
文件读取
Json格式转化
json工具:bejson.com
Json使用方法
tar 解压缩 指定目录加 -C tar -xvf a.tar.gz -C ./a/
查看CPU信息:cat /proc/cpuinfo
负载信息:top
division by zero 除数为0
所有异常都继承于BaseException
错误
异常
异常与错误
常见的异常类型
dic={'name':'tom'} dic['age']
try except:正常代码写在try下面,接受到异常后在except下面进行处理
def div(a,b):return a/btry:print(div(1,1))list1 = [1,2,3]print(list1[3])
except Exception as e:print(e)
异常捕获与异常处理
使用 raise 抛出异常
class MyError(Exception):def __init__(self,value):self.value = valuedef __str__(self):return repr(self.value)
两种编程思想
什么是面向对象
面向对象程序设计规则
类里面创建方法时,要加self
self.name = name
self.name 代表类里面的name,等号后面的name是外面传过来的name
self代表实例
print(Person.name)
Person.name = 'tom' # 修改
print(Person.name)zs = Person('张三')
print(zs.name)
zs.name = 'lili' # 修改
print(zs.name)
Person.eat()
zs = Person('张三')
zs.eat()
类、实例、方法、变量