1. 程式人生 > >python 條件表示式學習

python 條件表示式學習

與:and 或:or 非:not #rfind 用法:返回值是查詢到的目標字元在原字串中的下標,如果沒找到返回-1,如果在第0個位置找到返回0,其他的數字。 錯誤例項:

rule=‘suffix:ls’ rule.rfind(‘pre’) -1

not rule.rfind(‘pre’) False

rule.rfind(‘pre’)!=-1 & rule.rfind(‘suf’) True 僥倖正確的:

rule.rfind(‘pre’)!=-1 and rule.rfind(‘suf’) False 實際過程:

rule.rfind(‘pre’)!=-1 && rule.rfind(‘suf’) SyntaxError: invalid syntax

rule.rfind(‘pre’)!=-1 or rule.rfind(‘suf’)

File “<pyshell#7>”, line 2 rule.rfind(‘pre’)!=-1 or rule.rfind(‘suf’) ^ IndentationError: unexpected indent

rule.rfind(‘pre’)!=-1 False

rule.rfind(‘suf’) 0 應該寫成:

rule.rfind(‘pre’)!=-1 or rule.rfind(‘suf’)!=-1 True

rule.rfind(‘pre’)!=-1 and rule.rfind(‘suf’)!=-1#有字首又有後綴 False

rule.rfind(‘pre’)==-1 and rule.rfind(‘suf’)!=-1#僅僅字尾 True

rule.rfind(‘pre’)!=-1 and rule.rfind(‘suf’)==-1#僅僅字首 False