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中精确输出JSON浮点数的方法
Apr 18 Python
用Python中的wxPython实现最基本的浏览器功能
Apr 14 Python
Python读写ini文件的方法
May 28 Python
Python模块搜索概念介绍及模块安装方法介绍
Jun 03 Python
Python爬虫获取图片并下载保存至本地的实例
Jun 01 Python
终端命令查看TensorFlow版本号及路径的方法
Jun 13 Python
Ubuntu下Python2与Python3的共存问题
Oct 31 Python
使用Django简单编写一个XSS平台的方法步骤
Mar 25 Python
python如何提取英语pdf内容并翻译
Mar 03 Python
Python startswith()和endswith() 方法原理解析
Apr 28 Python
Java爬虫技术框架之Heritrix框架详解
Jul 22 Python
用Python提取PDF表格的方法
Apr 11 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
全国FM电台频率大全 - 8 黑龙江省
2020/03/11 无线电
PHP实现的基于单向链表解决约瑟夫环问题示例
2017/09/30 PHP
Prototype 学习 工具函数学习($w,$F方法)
2009/07/12 Javascript
JS date对象的减法处理实现代码
2010/12/28 Javascript
JS代码优化技巧之通俗版(减少js体积)
2011/12/23 Javascript
js window.print实现打印特定控件或内容
2013/09/16 Javascript
判断在css加载完毕后执行后续代码示例
2014/09/03 Javascript
基于bootstrap3和jquery的分页插件
2015/07/31 Javascript
JavaScript中innerHTML,innerText,outerHTML的用法及区别
2015/09/01 Javascript
JQuery自适应窗口大小导航菜单附源码下载
2015/09/01 Javascript
a标签置灰不可点击的实现方法
2017/02/06 Javascript
Bootstrap 过渡效果Transition 模态框(Modal)
2017/03/17 Javascript
jQuery EasyUI 组件加上“清除”功能实例详解
2017/04/11 jQuery
Avalonjs双向数据绑定与监听的实例代码
2017/06/23 Javascript
JavaScript实现的文本框placeholder提示文字功能示例
2018/07/25 Javascript
微信小程序制作表格的方法
2019/02/14 Javascript
vue项目中使用scss的方法步骤
2019/05/16 Javascript
JavaScript实现星级评价效果
2019/05/17 Javascript
python基础教程之popen函数操作其它程序的输入和输出示例
2014/02/10 Python
python爬虫爬取某站上海租房图片
2018/02/04 Python
详解pandas中MultiIndex和对象实际索引不一致问题
2019/07/23 Python
解决使用python print打印函数返回值多一个None的问题
2020/04/09 Python
django form和field具体方法和属性说明
2020/07/09 Python
详解python中的闭包
2020/09/07 Python
HTML5拖放效果的实现代码
2016/11/17 HTML / CSS
一套C++笔试题面试题
2012/06/06 面试题
python+selenium小米商城红米K40手机自动抢购的示例代码
2021/03/24 Python
药学专业毕业生求职信
2013/10/20 职场文书
酒店总经理工作职责
2013/12/13 职场文书
监督检查工作方案
2014/05/28 职场文书
学校社会实践活动总结
2014/07/03 职场文书
党员承诺书范文2015
2015/04/27 职场文书
法人身份证明书
2015/06/18 职场文书
2016年寒假学习心得体会
2015/10/09 职场文书
职场中的你,辞职信写对了吗?
2019/06/26 职场文书
Javascript 解构赋值详情
2021/11/17 Javascript