Python條件測(cè)試——if語(yǔ)句
隨著對(duì)Python了解的越來(lái)越多茁彭,很多新知和已知可以融合瞬沦,發(fā)揮更大的功能臊泌。
(1)條件測(cè)試實(shí)例
情景引入:某餐廳10月29號(hào)做活動(dòng)走哺,凡是當(dāng)天生日的顧客可以享受半折優(yōu)惠怎抛。
birthday ?= 1029
if birthday == 1029:
? ? print('Happy birthday to you! \nYou can have a 50% discount! ?\nHave a nice day!')
else:
? ? print('Sorry to let you know that today is 29th Oct.')
(2)其它符號(hào)
不等于 ? ? ? !=
大于 ? ? ? ? ? >
小于 ? ? ? ? ? <
大于等于 ? ?>=
小于等于 ? ?<=
(3)多個(gè)條件
同時(shí)滿足條件 ? ?and
滿足一個(gè)即可 ? ?or
情景引入:某商店10月29日做活動(dòng)卑吭,18周歲生日的顧客可享受免單。
if age == 18 and birthday ==1029:
? ? print('Happy birthday to you! \nYou can have a free meal! ?\nHave a nice day!')
(4)個(gè)體與整體
整體含有個(gè)體 ? ?in
整體不含個(gè)體 ? ?not in
情景引入:某培訓(xùn)機(jī)構(gòu)只提供C語(yǔ)言和Python語(yǔ)言的學(xué)習(xí)马绝。
language='C#'
classes=['C','Python']
if?language not in?classes:
????message="Sorry, we don't have "+language+" language?"+"class!"
????print(message)
小結(jié)
上文簡(jiǎn)單回顧了Python條件測(cè)試的內(nèi)容豆赏,包括各種比較符號(hào)、多個(gè)條件測(cè)試富稻、檢查整體中的個(gè)體掷邦。