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." '''
轉(zhuǎn)義符
用\'來指示單引號——注意這個反斜杠?,F(xiàn)在你可以把字符串表示為'What\'s your name?'。
表示這個特別的字符串的方法是"What's your name?",即用雙引號或三引號。
在一個字符串中,行末的單獨(dú)一個反斜杠表示字符串在下一行繼續(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、運(yùn)算符
|
運(yùn)算符 |
名稱 |
說明 |
例子 |
|
+ |
加 |
兩個對象相加 |
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。所有比較運(yùn)算符返回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'
相關(guān)文章
對tensorflow中tf.nn.conv1d和layers.conv1d的區(qū)別詳解
今天小編就為大家分享一篇對tensorflow中tf.nn.conv1d和layers.conv1d的區(qū)別詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
使用python+Flask實現(xiàn)日志在web網(wǎng)頁實時更新顯示
日志是一種可以追蹤某些軟件運(yùn)行時所發(fā)生事件的方法,下面這篇文章主要給大家介紹了關(guān)于使用python+Flask實現(xiàn)日志在web網(wǎng)頁實時更新顯示的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
基于OpenCV實現(xiàn)小型的圖像數(shù)據(jù)庫檢索功能
下面就使用VLAD表示圖像,實現(xiàn)一個小型的圖像數(shù)據(jù)庫的檢索程序。下面實現(xiàn)需要的功能模塊,分步驟給大家介紹的非常詳細(xì),對OpenCV圖像數(shù)據(jù)庫檢索功能感興趣的朋友跟隨小編一起看看吧2021-12-12
matplotlib之Font family [‘sans-serif‘] not&nbs
本文主要介紹了matplotlib之Font family [‘sans-serif‘] not found的問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03

