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', iprint '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 < 3返回0(即False)而3 < 5返回1(即True)。比較可以被任意串連:3 < 5 < 7返回True。 |
| > |
大於 |
返回x是否大於y |
5 > 3返回True。如果兩個運算元都是數字,它們首先被轉換為一個共同的類型。否則,它總是返回False。 |
| <= |
小於等於 |
返回x是否小於等於y |
x = 3; y = 6; x <= y返回True。 |
| >= |
大於等於 |
返回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 = 23guess = 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 hereelif 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 = 23running = Truewhile 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 hereprint '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: continueprint 'Input is of sufficient length'