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的Django框架中simple-todo工具的简单使用
May 30 Python
Python使用正则表达式抓取网页图片的方法示例
Apr 21 Python
Python爬虫实例_利用百度地图API批量获取城市所有的POI点
Jan 10 Python
python 爬虫一键爬取 淘宝天猫宝贝页面主图颜色图和详情图的教程
May 22 Python
Anaconda下安装mysql-python的包实例
Jun 11 Python
Django获取应用下的所有models的例子
Aug 30 Python
Django框架 Pagination分页实现代码实例
Sep 04 Python
基于Python实现签到脚本过程解析
Oct 25 Python
使用Python paramiko模块利用多线程实现ssh并发执行操作
Dec 05 Python
pytorch查看torch.Tensor和model是否在CUDA上的实例
Jan 03 Python
Python内置类型性能分析过程实例
Jan 29 Python
浅谈python 调用open()打开文件时路径出错的原因
Jun 05 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
实例详解PHP中html word 互转的方法
2016/01/28 PHP
PHP函数checkdnsrr用法详解(Windows平台用法)
2016/03/21 PHP
PHP基于面向对象实现的留言本功能实例
2018/04/04 PHP
laravel通用化的CURD的实现
2019/12/13 PHP
JQuery 学习笔记 选择器之二
2009/07/23 Javascript
js 获取中文拼音,Select自动匹配字母获取值的代码
2009/09/23 Javascript
javascript 操作cookies及正确使用cookies的属性
2009/10/15 Javascript
关于javascript DOM事件模型的两件事
2010/07/22 Javascript
jquery中each方法示例和常用选择器
2014/07/08 Javascript
AngularJS中取消对HTML片段转义的方法例子
2015/01/04 Javascript
JS响应鼠标点击实现两个滑块区间拖动效果
2015/10/26 Javascript
Bootstrap整体框架之JavaScript插件架构
2016/12/15 Javascript
JS实现图片居中悬浮效果
2017/12/25 Javascript
Chart.js 轻量级HTML5图表绘制工具库(知识整理)
2018/05/22 Javascript
通过vue手动封装on、emit、off的代码详解
2019/05/29 Javascript
微信小程序利用Canvas绘制图片和竖排文字详解
2019/06/25 Javascript
JavaScript定时器使用方法详解
2020/03/26 Javascript
[56:21]LGD vs IG 2018国际邀请赛小组赛BO2 第二场 8.18
2018/08/19 DOTA
自己使用总结Python程序代码片段
2015/06/02 Python
Python冒泡排序注意要点实例详解
2016/09/09 Python
python安装PIL模块时Unable to find vcvarsall.bat错误的解决方法
2016/09/19 Python
Python RabbitMQ消息队列实现rpc
2018/05/30 Python
Python装饰器模式定义与用法分析
2018/08/06 Python
python脚本调用iftop 统计业务应用流量的思路详解
2019/10/11 Python
Python中生成一个指定长度的随机字符串实现示例
2019/11/06 Python
python super用法及原理详解
2020/01/20 Python
HTML5 History API 实现无刷新跳转
2016/01/11 HTML / CSS
阿联酋航空官方网站:Emirates
2017/10/17 全球购物
陈胜吴广起义口号
2014/06/20 职场文书
个人授权委托书范本格式
2014/10/12 职场文书
2019最新公司租房合同(例文)
2019/07/18 职场文书
Redis安装启动及常见数据类型
2021/04/14 Redis
如何用python反转图片,视频
2021/04/24 Python
解决WINDOWS电脑开机后桌面没有任何图标
2022/04/09 数码科技
MySQL数据库 任意ip连接方法
2022/05/20 MySQL
Windows server 2022创建创建林、域树、子域的步骤
2022/06/25 Servers