学习记录-接口测试
python的三方包安装工具pip
pip提供对Python相关模块的查找、下载、安装和卸载等功能。
pip list #打开命令行直接输入即可查看已安装的包
Python -m pip install --upgrade pip #打开命令行直接运行,它自动收集pip新版本卸载旧版本安装新版本
pip install requests #安装requests包
在Python环境输入import requests将不错
C:\Users\admin>python
Python 3.9.5 (tags/v3.9.5:0a7dcbd, May 3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>>
pycharm的使用指导
New Project,在project name点击右键new-- python file(文件名:helloworld),输入测试脚本print('hello world')
怎样运行.py文件
在菜单栏中选择Run->Run...->2.helloworld

C:\Users\admin\AppData\Local\Programs\Python\Python39\python.exe C:/EEEE_script/python_script/FirstPrj/helloworld.py
这是我的第一个脚本
hello world
为什么要强调命令行窗口中的执行语句呢?这为后续在自动化测试框架、持续集成等情况下调用、执行脚本做一些铺垫。
Python的变量命名
变量命名是大小写英文字母、数字或下划线(_)的组合,
不能以数字开头,且字母区分大小写。
说明:Python是支持汉子变量(即中文字符,例如姓名='张三')
Python的数据类型
字符串:
在字符串里有一个需要注意的地方,就是对'\'的处理
>>> print('c:\nows')
c:
ows
备注:可以看出\作换行符了。
\经常会用作转义字符,如\t 水平制表符,\r 回车符 \?代表一个问号 \0代表空字符
若要输出\本身,需要用到\\,或者在整个字符串前加1个r
>>> print('c:\\nows')
c:\nows
>>> print(r'c:\nows')
c:\nows
数值类型:整型和浮点型
时间戳是指格林尼治时间自1970-1-1 00:00:00至当前时间的总秒数。
布尔类型:
真:True 假:False
它是一个特殊的类型,可以像整型一样参与运算,true=1,false=0
>>> print(True+3)
4
>>> print(true+3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'true' is not defined
>>> print(False+3)
3
它更多用于表达式的判断,当表达式为真时,程序该如何处理。。
temp=3
if (temp==3):
print('你猜对了')
else:
print('你猜错了')
数据类型转换
例如:数值不可以和字符串直接拼接在一起,这是可以先把数值的类型转换为字符串
>>> print('age'+3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
>>> print('age'+str(3)) #str()将里面的变量转换为了字符串类型,再拼接的。
age3
除了str(),常用的还有int() float()。
帮助:查看变量的数据类型print(type(变量名))
缩进:
缩进让Python代码的层次结构变得清晰
在Python中用来标识不同的代码块的,
通过不同的缩进来判断代码之间的关系。
内置函数:
print()函数就是一个内置函数
内置函数是为方便程序设计人员快速编写程序而提供的函数,可以直接使用
可以通过dir(__builtins__)查看所有内置函数
>>> dir(__builtins__)
[………………'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']
help(函数名)可以帮助了解函数的作用及参数
>>> help(max)
Help on built-in function max in module builtins:
max(...)
max(iterable, *[, default=obj, key=func]) -> value
max(arg1, arg2, *args, *[, key=func]) -> value
With a single iterable argument, return its biggest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the largest argument.
列表[]
列表作用:用于存储数据,以备后续使用。
列表是有序的序列,元素可以是不同的数据类型(有别于C语言的数组,即C语言数组只存储同类型数据)
可以定义空列表alist=[]
通过索引获取列表元素(正序、或倒序)
alist=[1,23,5]
第1个元素,用alist[0]
alist[-1],从末尾的元素开始获取,即取得5
通过切片获取列表元素
使用场景:一次获取列表中的多个元素 或1个元素
>>> cList=[2,3,45,6,7,7,8,9]
>>> print(cList[1:5])
[3, 45, 6, 7]
备注:切片的结束位置是cList[m-1],假设切片范围 cList[n:m]
>>> print(cList[:])
[2, 3, 45, 6, 7, 7, 8, 9]
>>> print(cList[::-1])
[9, 8, 7, 7, 6, 45, 3, 2]
>>> print(cList[::2])
[2, 45, 7, 8]
备注:n为空时,会默认n=0;m为空,会默认剩余所有元素;t为空,默认t=1;t=-1,会反转 假设切片范围 cList[n:m:t]
添加列表元素
在末尾追加clist.append('smile')
再有指定位置插入元素,clist.insert(1,'Oct')
原来index=1的元素及其后面的元素,均往后移动1位。
>>> cList=[2,3,45,6,7,7,8,9]
>>> cList.append('smile')
>>> cList.insert(1,'Oct')
>>> print(cList[:])
[2, 'Oct', 3, 45, 6, 7, 7, 8, 9, 'smile']
>>>
字典
可以创建一个空的字典,empty={}
也可以创建包含一个或多个键值对的非空字典(key和value)
说明:
1、字典中的键值是不能重复的,但值可以重复没有限制
2、字典的值不仅可以为字符串,还可以是数字,甚至可以是个元组、列表等。
获取字典元素
可以通过使用“字典名称[键]”的形式来获得
该字典中这个键对应的值的信息
dict1={3:'熊猫地图',4:'地图鱼',5:'熊猫鼠',2:888,0:[32,87,345],'f':[23,'a','bc']}
print(dict1[2])
print(dict1['f'])
也可以使用字典的get()方法来达到同样的目的
dict1={3:'熊猫地图',4:'地图鱼',5:'熊猫鼠',2:888,0:[32,87,345],'f':[23,'a','bc']}
print(dict1.get(2))
print(dict1.get('f'))
使用字典的items()方法,可以获得字典的所有键值对信息
dict1={3:'熊猫地图',4:'地图鱼',5:'熊猫鼠',2:888,0:[32,87,345],'f':[23,'a','bc']}
print(dict1.items())
#输出结果:dict_items([(3, '熊猫地图'), (4, '地图鱼'), (5, '熊猫鼠'), (2, 888), (0, [32, 87, 345]), ('f', [23, 'a', 'bc'])])
备注:items()返回的是一个列表,列表中的元素是元组,元组里是每个键及对应的值
使用字典的values()方法,可以获取字典的所有值
dict1={3:'熊猫地图',4:'地图鱼',5:'熊猫鼠',2:888,0:[32,87,345],'f':[23,'a','bc']}
print(dict1.values())
#输出结果:dict_values(['熊猫地图', '地图鱼', '熊猫鼠', 888, [32, 87, 345], [23, 'a', 'bc']])
备注:values()返回的是一个列表,列表中的元素就是字典的值
修改字典
可以用update()方法,里面是键值对的字典,或者直接找到对应键再赋值
dict1={3:'熊猫地图',4:'地图鱼',5:'熊猫鼠',2:888,0:[32,87,345],'f':[23,'a','bc']}
dict1[3]='斑马鱼'
print(dict1.items())
#输出dict_items([(3, '斑马鱼'), (4, '地图鱼'), (5, '熊猫鼠'), (2, 888), (0, [32, 87, 345]), ('f', [23, 'a', 'bc'])])
dict1.update({3:'海马',8:'蜗牛'})
print(dict1.items())
#输出dict_items([(3, '海马'), (4, '地图鱼'), (5, '熊猫鼠'), (2, 888), (0, [32, 87, 345]), ('f', [23, 'a', 'bc']), (8, '蜗牛')])
备注:有键3,因此更新它的值,没有键8,因此新增了一个键值对。
----------------------------
>>> dir(dict)
['__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__ior__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__ror__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
>>> help(dict.update)
Help on method_descriptor:
update(...)
D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]
If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v
In either case, this is followed by: for k in F: D[k] = F[k]
----------------------------
字典元素计数
len(dict1)函数可以获取dict1对象的长度,即它包含了多少个键值对
print('包含了键值对个数: ',len(dict1))
删除字典元素
可以用pop(键) 它是删除指定键值对
popitem() 它是删除最后一个键值对
或者clear()来删除整个字典
dict1={3:'熊猫地图',4:'地图鱼',5:'熊猫鼠',2:888,0:[32,87,345],'f':[23,'a','bc']}
dict1.pop('f')
print(dict1.items())
#输出:dict_items([(3, '熊猫地图'), (4, '地图鱼'), (5, '熊猫鼠'), (2, 888), (0, [32, 87, 345])])
dict1={3:'熊猫地图',4:'地图鱼',5:'熊猫鼠',2:888,0:[32,87,345],'f':[23,'a','bc']}
dict1.clear()
print('clear',dict1.items())
#输出:clear dict_items([])
For循环
一般格式如下:
for 变量 in 序列:
语句
tlist=range(10) #等价于range(0,10,1),即默认从0开始,步长默认为1
print(tlist[0],tlist[9])
#tlist为:0,1,2,3,4,5,6,7,8,9
下面用for循环语句来计算1-100的整数
#准备1-100的列表
tlist=range(1,101)
print(len(tlist))
print(tlist[0],tlist[99]) # 输出1 100
------------
sum1=0
for i in tlist:
sum1=sum1+i
print('1-100的整数求和:',sum1)
-------------
sum2=0
for i in range(100):
sum2=sum2+(i+1)
print('1-100的整数求和:',sum2)
-------------------------
while循环
一般的格式如下:
while 判断条件:
语句
备注:判断条件为真时,才会执行语句块内的语句。
sum3=0 i=1 while i<=100: sum3=sum3+i i=i+1 print('用while循环计算1-100的整数求和结果是:',sum3)
break语句,它实现从循环体中跳出
continue语句,它是终止本轮循环并开始下一轮循环
if……else条件语句
if 判断条件1:
语句1
elif 判断条件2:
语句2
else:
语句3
#输出1-10的偶数和非偶数 for i in range(1,11): if i%2==0: print('偶数:',i) elif i%2!=0: print(str(i)+'是奇数')
------------------------------------------------------------------------------------------------------------
待熟悉:pycharm的单步执行、断点等调试手段。
待学习: Python的的calendar和time模块,用于格式化日期和时间,以及模块time的strftime和localtime方法

浙公网安备 33010602011771号