用Python做测试——测试人员需要掌握的Python基础(二)
来源:互联网 发布:网络平台运行 编辑:程序博客网 时间:2024/05/17 23:59
f、函数
定义函数
def sayHello():
print 'Hello World!' # block belonging to the function
sayHello() # call the function
使用函数形参
def printMax(a, b):
if a > b:
print a, 'is maximum'
else:
print b, 'is maximum'
printMax(3, 4) # directly give literal values
x = 5
y = 7
printMax(x, y) # give variables as arguments
默认参数值
def say(message, times = 1):
print message * times
say('Hello')
say('World', 5)
默认参数只能在后面,不能在形参前面
def func(a, b=5, c=10):
print 'a is', a, 'and b is', b, 'and c is', c
func(3, 7)
func(25, c=24)
Return
def maximum(x, y):
if x > y:
return x
else:
return y
print maximum(2, 3)
g、模块
利用import语句 输入 sys模
import sys
print 'The command line arguments are:'
for i in sys.argv:
print i
print '\n\nThe PYTHONPATH is', sys.path, '\n'
如何导入自定义模块?
def sayhi():
print 'Hi, this is mymodule speaking.'
version = '0.1'
---------------------------------------------
import mymodule
mymodule.sayhi()
print 'Version', mymodule.version
h、数据结构
List(序列)
list是处理一组有序项目的数据结构,即你可以在一个列表中存储一个 序列 的项目
一旦你创建了一个列表,你可以添加、删除或是搜索列表中的项目。
由于你可以增加或删除项目,列表是 可变的数据类型,即这种类型是可以被改变的。
shoplist = ['apple', 'mango', 'carrot', 'banana']
print 'I have', len(shoplist),'items to purchase.'
print 'These items are:', # Notice the comma at end of the line
for item in shoplist:
print item,
print '\nI also have to buy rice.'
shoplist.append('rice')
print 'My shopping list is now', shoplist
print 'I will sort my list now'
shoplist.sort()
print 'Sorted shopping list is', shoplist
print 'The first item I will buy is', shoplist[0]
olditem = shoplist[0]
del shoplist[0]
print 'I bought the', olditem
print 'My shopping list is now', shoplist
tuple(元组)
元组和列表十分类似,只不过元组和字符串一样是 不可变的 即你不能修改元组
zoo = ('wolf', 'elephant', 'penguin')
print 'Number of animals in the zoo is', len(zoo)
new_zoo = ('monkey', 'dolphin', zoo)
print 'Number of animals in the new zoo is', len(new_zoo)
print 'All animals in new zoo are', new_zoo
print 'Animals brought from old zoo are', new_zoo[2]
print 'Last animal brought from old zoo is', new_zoo[2][2]
元组最通常的用法是用在打印语句中
age = 22
name = 'Swaroop'
print '%s is %d years old' % (name, age)
print 'Why is %s playing with that python?' % name
dict(字典)
键值对在字典中以这样的方式标记:d = {key1 : value1, key2 : value2 }。注意它们的键/值对用冒号分割,而各个对用逗号分割,所有这些都包括在花括号中。
记住字典中的键/值对是没有顺序的。如果你想要一个特定的顺序,那么你应该在使用前自己对它们排序。
ab = { 'Swaroop' : 'swaroopch@byteofpython.info',
'Larry' : 'larry@wall.org',
'Matsumoto' : 'matz@ruby-lang.org',
'Spammer' : 'spammer@hotmail.com'
}
print "Swaroop's address is %s" % ab['Swaroop']
# Adding a key/value pair
ab['Guido'] = 'guido@python.org'
# Deleting a key/value pair
del ab['Spammer']
print '\nThere are %d contacts in the address-book\n' % len(ab)
for name, address in ab.items():
print 'Contact %s at %s' % (name, address)
if 'Guido' in ab: # OR ab.has_key('Guido')
print "\nGuido's address is %s" % ab['Guido']
序列切片
shoplist = ['apple', 'mango', 'carrot', 'banana']
# Indexing or 'Subscription' operation
print 'Item 0 is', shoplist[0]
print 'Item 1 is', shoplist[1]
print 'Item 2 is', shoplist[2]
print 'Item 3 is', shoplist[3]
print 'Item -1 is', shoplist[-1]
print 'Item -2 is', shoplist[-2]
# Slicing on a list
print 'Item 1 to 3 is', shoplist[1:3]
print 'Item 2 to end is', shoplist[2:]
print 'Item 1 to -1 is', shoplist[1:-1]
print 'Item start to end is', shoplist[:]
# Slicing on a string
name = 'swaroop'
print 'characters 1 to 3 is', name[1:3]
print 'characters 2 to end is', name[2:]
print 'characters 1 to -1 is', name[1:-1]
print 'characters start to end is', name[:]
组合使用
接口测试,主要返回的数据类型为以下两种:
字典中+list
List中+字典
i、使用文件
poem = '''\
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
'''
f = file('poem.txt', 'w') # open for 'w'riting
f.write(poem) # write text to file
f.close() # close the file
f = file('poem.txt')
# if no mode is specified, 'r'ead mode is assumed by default
while True:
line = f.readline()
if len(line) == 0: # Zero length indicates EOF
break
print line,
# Notice comma to avoid automatic newline added by Python
f.close() # close the file
j、try..except
import sys
try:
s = raw_input('Enter something --> ')
except EOFError:
print '\nWhy did you do an EOF on me?'
sys.exit() # exit the program
except:
print '\nSome error/exception occurred.'
# here, we are not exiting the program
finally:
print 'Done'
- 用Python做测试——测试人员需要掌握的Python基础(二)
- 用Python做测试——测试人员需要掌握的Python基础(一)
- 测试人员需要掌握的几点
- 用python做http协议的api自动化测试二
- 做为测试人员需要用到的MySql命令大全—基础篇
- 亿能测试大讲堂 - YY在线课程[ 测试人员需要掌握的Shell脚本编程 ]
- 测试人员应该做的!
- 测试人员掌握代码的重要性
- 用Python做测试——单用例调用
- 用Python做测试——多用例调用
- 用Python做测试——多数据调用
- 性能测试需要掌握的
- Selenium自动化测试Python二:WebDriver基础
- 专为渗透测试人员设计的Python工具大合集
- 专为渗透测试人员设计的 Python 工具大合集
- Python-Selenium2做Web自动化测试(1)-软件测试分类以及什么时候需要进行自动化测试
- Java基础教程0-测试人员为什么要掌握Java基础
- 用Python做测试——Requests包的基本用法
- drop&drug 学习:拼图demo
- C语言中的基本类型
- Time(java模拟法)
- FineReport中如何用JavaScript解决控件值刷新不及时
- MaxCompute应用限制整理
- 用Python做测试——测试人员需要掌握的Python基础(二)
- 工作内容
- Hadoop第一天
- 一个简单例子理解C语言指针
- windows消息队列
- python中带星号/一个星号/两个星号的参数
- 设计模式-----适配器模式(Adapter)
- Web and php parse json data
- java中Map,List与Set的区别