python:if in 語(yǔ)句 和if not in 語(yǔ)句詳解

admin Python評(píng)論294字?jǐn)?shù) 236閱讀模式

if in 語(yǔ)句:

>>> kj = ['a','b','c','d','e']   #定義一個(gè)列表
>>> if 'b' in kj:
...     print("a在列表中")
...
a在列表中
>>>

if not in 語(yǔ)句:文章源自網(wǎng)吧系統(tǒng)維護(hù)-http://www.strong-digital.cn/11620.html

>>> kj = ['a','b','c','d','e']   #定義一個(gè)列表
>>> if 'b' in kj:
...     print("a在列表中")
...
a在列表中
>>> if 'h' not in kj:
...     print("h不在列表中")
...
h不在列表中
>>>

使用 if in可以盡量少使用for循環(huán),讓代碼更精簡(jiǎn)。文章源自網(wǎng)吧系統(tǒng)維護(hù)-http://www.strong-digital.cn/11620.html 文章源自網(wǎng)吧系統(tǒng)維護(hù)-http://www.strong-digital.cn/11620.html

版權(quán)聲明:文章圖片資源來(lái)源于網(wǎng)絡(luò),如有侵權(quán),請(qǐng)留言刪除!!!
廣告也精彩
admin
  • 本文由 發(fā)表于 2024年2月29日 10:58:57
  • 轉(zhuǎn)載請(qǐng)務(wù)必保留本文鏈接:http://www.strong-digital.cn/11620.html
匿名

發(fā)表評(píng)論

匿名網(wǎng)友 填寫信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: