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 实现微信公众号粉丝迁移流程
Jan 03 Python
Python装饰器用法实例总结
Feb 07 Python
Pyinstaller打包.py生成.exe的方法和报错总结
Apr 02 Python
基于多进程中APScheduler重复运行的解决方法
Jul 22 Python
用python wxpy管理微信公众号并利用微信获取自己的开源数据
Jul 30 Python
python实现一个函数版的名片管理系统过程解析
Aug 27 Python
numpy ndarray 取出满足特定条件的某些行实例
Dec 05 Python
python闭包、深浅拷贝、垃圾回收、with语句知识点汇总
Mar 11 Python
python函数中将变量名转换成字符串实例
May 11 Python
Python爬虫之爬取某文库文档数据
Apr 21 Python
pytorch加载预训练模型与自己模型不匹配的解决方案
May 13 Python
python playwright之元素定位示例详解
Jul 23 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
eWebEditor v3.8 商业完整版 (PHP)
2006/12/06 PHP
PHP 的几个配置文件函数
2006/12/21 PHP
基于HTTP长连接的&quot;服务器推&quot;技术的php 简易聊天室
2009/10/31 PHP
ionCube 一款类似zend的PHP加密/解密工具
2010/07/25 PHP
瀑布流布局并自动加载实现代码
2013/03/12 Javascript
javascript:void(0)是什么意思示例介绍
2013/11/17 Javascript
js 数组去重的四种实用方法
2014/09/09 Javascript
对JavaScript的全文搜索实现相关度评分的功能的方法
2015/06/24 Javascript
详解JS中Array对象扩展与String对象扩展
2016/01/07 Javascript
使用jquery.qrcode.min.js实现中文转化二维码
2016/03/11 Javascript
JavaScript 浏览器兼容性总结及常用浏览器兼容性分析
2016/03/30 Javascript
Javascript字符串拼接小技巧(推荐)
2016/06/02 Javascript
jQuery Validate 数组 全部验证问题
2017/01/12 Javascript
JS简单获取当前日期和农历日期的方法
2017/04/17 Javascript
JavaScript实现简单的四则运算计算器完整实例
2017/04/28 Javascript
Node.js中使用mongoose操作mongodb数据库的方法
2017/09/12 Javascript
详解动画插件wow.js的使用方法
2017/09/13 Javascript
vue-cli webpack2项目打包优化分享
2018/02/07 Javascript
Angular5中调用第三方库及jQuery的添加的方法
2018/06/07 jQuery
Node.js fs模块(文件模块)创建、删除目录(文件)读取写入文件流的方法
2019/09/03 Javascript
你不知道的SpringBoot与Vue部署解决方案
2020/11/09 Javascript
Python实现mysql数据库更新表数据接口的功能
2017/11/19 Python
python一键去抖音视频水印工具
2018/09/14 Python
django项目中使用云片网发送短信验证码的实现
2021/01/19 Python
Shopee新加坡:东南亚与台湾电商平台
2019/01/25 全球购物
潘多拉珠宝俄罗斯官方网上商店:PANDORA俄罗斯
2020/09/22 全球购物
电子商务毕业生求职信
2013/11/10 职场文书
优秀毕业生自我鉴定
2014/01/19 职场文书
创业计划书模版
2014/02/05 职场文书
幼儿园清明节活动总结
2014/07/04 职场文书
2014年个人教学工作总结
2014/12/09 职场文书
医院志愿者活动总结
2015/05/06 职场文书
2016年公司新年寄语
2015/08/17 职场文书
话题作文之诚信
2019/11/28 职场文书
Python OpenCV 彩色与灰度图像的转换实现
2021/06/05 Python
如何利用python创作字符画
2022/06/25 Python