溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python測試人員需要掌握的知識

發(fā)布時間:2020-09-19 18:39:32 來源:腳本之家 閱讀:135 作者:_文字_ 欄目:開發(fā)技術(shù)

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."
'''

轉(zhuǎn)義符

用\'來指示單引號——注意這個反斜杠?,F(xiàn)在你可以把字符串表示為'What\'s your name?'。

表示這個特別的字符串的方法是"What's your name?",即用雙引號或三引號。

在一個字符串中,行末的單獨一個反斜杠表示字符串在下一行繼續(xù),而不是開始一個新的行。例如:

"This is the first sentence.\
This is the second sentence."

b、變量

定義變量的方法與其它語言類似,變量的類型是通過賦值來確定的

Int型   Number = 0
字符串型 String = ‘'
Dict型  dict = {}

c、縮進(jìn)

同一層次的語句必須有相同的縮進(jìn)。每一組這樣的語句稱為一個塊

i = 5
 print 'Value is', i
print 'I repeat, the value is', i

 d、運算符

運算符

名稱

說明

例子

+

兩個對象相加

3 + 5得到8。'a' + 'b'得到'ab'。

-

得到負(fù)數(shù)或是一個數(shù)減去另一個數(shù)

-5.2得到一個負(fù)數(shù)。50 - 24得到26。

*

兩個數(shù)相乘或是返回一個被重復(fù)若干次的字符串

2 * 3得到6。'la' * 3得到'lalala'。

/

x除以y

4/3得到1(整數(shù)的除法得到整數(shù)結(jié)果)。4.0/3或4/3.0得到1.3333333333333333

//

取整除

返回商的整數(shù)部分

4 // 3.0得到1.0

%

取模

返回除法的余數(shù)

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。如果兩個操作數(shù)都是數(shù)字,它們首先被轉(zhuǎn)換為一個共同的類型。否則,它總是返回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,因為它知道這個表達(dá)式的值肯定是False(因為x是False)。這個現(xiàn)象稱為短路計算。

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循環(huán)在這個范圍內(nèi)遞歸——for i in range(1,5)等價于for i in [1, 2, 3, 4],這就如同把序列中的每個數(shù)(或?qū)ο螅┵x值給i,一次一個,然后以每個i的值執(zhí)行這個程序塊

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'

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI