做自动化测试之前---你需要掌握的Python基础

前言

不用说想必大家也知道,在做自动化测试之前需要自身具备很多的知识,例如计算机基础、计算机网络、操作系统——Linux、数据库——MySQL、英语基础、编程语言——Python等。所以说想做好自动化测试还得一步一步来,今天我们就来讲讲最基础的Python语言基础,好好看,好好学,希望对你们的测试生涯尽些绵薄之力。

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、运算符
运算符

最常用的是小(等)于、大(等)于、(不)等于、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语言基础或者进阶自动化测试的朋友应该会很有帮助

结语

学习软件测试是件需要坚持的事情,学习的过程可能会很枯燥,不过有一些人一起学的话大概就不会了吧,跟我一起学习,有人陪伴,就不会孤单。

这篇贴子到这里就结束了,最后,希望看这篇帖子的朋友能够有所收获。欢迎留言,或是关注我的专栏和我交流。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值