Python测试人员需要掌握的知识


Posted in Python onFebruary 08, 2018

a、字符串的定义方法

使用单引号(')

你可以用单引号指示字符串,就如同'Quote me on this'这样。所有的空白,即空格和制表符都照原样保留。

使用双引号(")

在双引号中的字符串与单引号中的字符串的使用完全相同,例如"What's your name?"。

使用三引号('''或""")

利用三引号,你可以指示一个多行的字符串。你可以在三引号中自由的使用单引号和双引号。例如:

'''This is a multi-line string. This is the first line.
This is the second line.
"What's your name?," I asked.
He said "Bond, James Bond."
'''

转义符

用\'来指示单引号——注意这个反斜杠。现在你可以把字符串表示为'What\'s your name?'。

表示这个特别的字符串的方法是"What's your name?",即用双引号或三引号。

在一个字符串中,行末的单独一个反斜杠表示字符串在下一行继续,而不是开始一个新的行。例如:

"This is the first sentence.\
This is the second sentence."

b、变量

定义变量的方法与其它语言类似,变量的类型是通过赋值来确定的

Int型   Number = 0
字符串型 String = ‘'
Dict型  dict = {}

c、缩进

同一层次的语句必须有相同的缩进。每一组这样的语句称为一个块

i = 5
 print 'Value is', i
print 'I repeat, the value is', i

 d、运算符

运算符 名称 说明 例子
+ 两个对象相加 3 + 5得到8。'a' + 'b'得到'ab'。
- 得到负数或是一个数减去另一个数 -5.2得到一个负数。50 - 24得到26。
* 两个数相乘或是返回一个被重复若干次的字符串 2 * 3得到6。'la' * 3得到'lalala'。
/ x除以y 4/3得到1(整数的除法得到整数结果)。4.0/3或4/3.0得到1.3333333333333333
// 取整除 返回商的整数部分 4 // 3.0得到1.0
% 取模 返回除法的余数 8%3得到2。-25.5%2.25得到1.5
小于 返回x是否小于y。所有比较运算符返回1表示真,返回0表示假。这分别与特殊的变量True和False等价。注意,这些变量名的大写。 5
> 大于 返回x是否大于y 5 > 3返回True。如果两个操作数都是数字,它们首先被转换为一个共同的类型。否则,它总是返回False。
小于等于 返回x是否小于等于y x = 3; y = 6; x
>= 大于等于 返回x是否大于等于y x = 4; y = 3; x >= y返回True。
== 等于 比较对象是否相等 x = 2; y = 2; x == y返回True。x = 'str'; y = 'stR'; x == y返回False。x = 'str'; y = 'str'; x == y返回True。
!= 不等于 比较两个对象是否不相等 x = 2; y = 3; x != y返回True。
not 布尔“非” 如果x为True,返回False。如果x为False,它返回True。 x = True; not y返回False。
and 布尔“与” 如果x为False,x and y返回False,否则它返回y的计算值。 x = False; y = True; x and y,由于x是False,返回False。在这里,Python不会计算y,因为它知道这个表达式的值肯定是False(因为x是False)。这个现象称为短路计算。
or 布尔“或” 如果x是True,它返回True,否则它返回y的计算值。 x = True; y = False; x or y返回True。短路计算在这里也适用。

最常用的是小(等)于、大(等)于、(不)等于、not、and、or

e、控制流

if语句

number = 23
guess = int(raw_input('Enter an integer : '))
 
if guess == number:
  print 'Congratulations, you guessed it.' # New block starts here
  print "(but you do not win any prizes!)" # New block ends here
elif guess < number:
  print 'No, it is a little higher than that' # Another block
  # You can do whatever you want in a block ...
else:
  print 'No, it is a little lower than that'
  # you must have guess > number to reach here
 
print 'Done'
# This last statement is always executed, after the if statement is executed
 
if 'a' in name:
  print 'Yes, it contains the string "a"'

elif和else部分是可选的

while语句

number = 23
running = True
while running:
  guess = int(raw_input('Enter an integer : '))
  if guess == number:
    print 'Congratulations, you guessed it.'
    running = False # this causes the while loop to stop
  elif guess < number:
    print 'No, it is a little higher than that'
  else:
    print 'No, it is a little lower than that'
else:
  print 'The while loop is over.'
  # Do anything else you want to do here
print 'Done'

for语句

for i in range(1, 5):
print i
等价于
for i in range(5):
print i

for循环在这个范围内递归——for i in range(1,5)等价于for i in [1, 2, 3, 4],这就如同把序列中的每个数(或对象)赋值给i,一次一个,然后以每个i的值执行这个程序块

break语句

while True:
  s = raw_input('Enter something : ')
  if s == 'quit':
    break
  print 'Length of the string is', len(s)
print 'Done'

continue语句

while True:
  s = raw_input('Enter something : ')
  if s == 'quit':
    break
  if len(s) < 3:
    continue
print 'Input is of sufficient length'
Python 相关文章推荐
Python中for循环详解
Jan 17 Python
使用IPython来操作Docker容器的入门指引
Apr 08 Python
python中is与双等于号“==”的区别示例详解
Nov 21 Python
10个Python小技巧你值得拥有
Sep 29 Python
对pycharm 修改程序运行所需内存详解
Dec 03 Python
Python解决线性代数问题之矩阵的初等变换方法
Dec 12 Python
浅谈python标准库--functools.partial
Mar 13 Python
python中时间转换datetime和pd.to_datetime详析
Aug 11 Python
python不相等的两个字符串的 if 条件判断为True详解
Mar 12 Python
python 实现倒计时功能(gui界面)
Nov 11 Python
Python实现随机爬山算法
Jan 29 Python
Jupyter Notebook内使用argparse报错的解决方案
Jun 03 Python
python实现单向链表详解
Feb 08 #Python
Python生成器以及应用实例解析
Feb 08 #Python
代码分析Python地图坐标转换
Feb 08 #Python
python爬虫中get和post方法介绍以及cookie作用
Feb 08 #Python
Python OpenCV 直方图的计算与显示的方法示例
Feb 08 #Python
python OpenCV学习笔记之绘制直方图的方法
Feb 08 #Python
Python列表推导式与生成器表达式用法示例
Feb 08 #Python
You might like
深入探讨:Nginx 502 Bad Gateway错误的解决方法
2013/06/03 PHP
PHP JS Ip地址及域名格式检测代码
2013/09/27 PHP
php商品对比功能代码分享
2015/09/24 PHP
PHP预定义超全局数组变量小结
2018/08/20 PHP
再次分享18个非常棒的jQuery表格插件
2011/04/10 Javascript
下拉菜单点击实现连接跳转功能的js代码
2013/05/19 Javascript
网页中可关闭的漂浮窗口实现可自行调节
2013/08/20 Javascript
javascript阻止浏览器后退事件防止误操作清空表单
2013/11/22 Javascript
sliderToggle在写jquery的计时器setTimeouter中不生效
2014/05/26 Javascript
Bootstrap模块dropdown实现下拉框响应
2016/05/22 Javascript
jQuery EasyUI Tab 选项卡问题小结
2016/08/16 Javascript
微信小程序 保留小数(toFixed)详细介绍
2016/11/16 Javascript
关于使用axios的一些心得技巧分享
2017/07/02 Javascript
iView框架问题整理小结
2018/10/16 Javascript
iview实现图片上传功能
2020/06/29 Javascript
TF-IDF与余弦相似性的应用(二) 找出相似文章
2017/12/21 Python
python爬虫使用cookie登录详解
2017/12/27 Python
Python实现的网页截图功能【PyQt4与selenium组件】
2018/07/12 Python
python使用插值法画出平滑曲线
2018/12/15 Python
完美解决Python matplotlib绘图时汉字显示不正常的问题
2019/01/29 Python
python实现计数排序与桶排序实例代码
2019/03/28 Python
python matplotlib库绘制散点图例题解析
2019/08/10 Python
基于Python测试程序是否有错误
2020/05/16 Python
django日志默认打印request请求信息的方法示例
2020/05/17 Python
html2canvas把div保存图片高清图的方法示例
2018/03/05 HTML / CSS
阿里云:Aliyun.com
2017/02/15 全球购物
Origins悦木之源英国官网:雅诗兰黛集团高端植物护肤品牌
2017/11/06 全球购物
新西兰便宜隐形眼镜购买网站:QUICKLENS New Zealand
2019/03/02 全球购物
销售总监工作职责
2013/11/21 职场文书
公司年终奖分配方案
2014/06/16 职场文书
高中学生会竞选演讲稿
2014/08/25 职场文书
2014年新教师工作总结
2014/11/08 职场文书
硕士论文致谢范文
2015/05/14 职场文书
治理商业贿赂工作总结
2015/08/10 职场文书
基于Redis过期事件实现订单超时取消
2021/05/08 Redis
Spring Boot 使用 Spring-Retry 进行重试框架
2022/04/24 Java/Android