亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Python必須了解的35個關鍵詞

瀏覽:104日期:2022-07-17 11:31:30

每種編程語言都會有一些特殊的單詞,稱為關鍵詞。對待關鍵詞的基本要求是,你在命名的時候要避免與之重復。本文將介紹一下Python中的關鍵詞。關鍵詞不是內置函數或者內置對象類型,雖然在命名的時候同樣也最好不要與這些重名,但是,畢竟你還可以使用與內置函數或者內置對象類型重名的名稱來命名。關鍵詞則不同,它是不允許你使用。

在Python3.8中提供了35個關鍵詞,如下所示:

False await else import passNone break except in raiseTrue class finally is returnand continue for lambda tryas def from nonlocal whileassert del global not withasync elif if or yield

如果打算在交互模式里面查看關鍵詞,可以使用help():

>>> help('keywords')Here is a list of the Python keywords. Enter any keyword to get more help.False await else import passNone break except in raiseTrue class finally is returnand continue for lambda tryas def from nonlocal whileassert del global not withasync elif if or yield

對每個關鍵詞的詳細說明,也可以用help()查看:

>>> help(’pass’) # 敲回車后出現下面的內容The 'pass' statement******************** pass_stmt ::= 'pass''pass' is a null operation — when it is executed, nothing happens. Itis useful as a placeholder when a statement is required syntactically,but no code needs to be executed, for example: def f(arg): pass # a function that does nothing (yet) class C: pass # a class with no methods (yet)

除了上面的方法之外,還有一個標準庫的模塊keyword提供了關鍵詞查詢功能。

>>> import keyword>>> keyword.kwlist[’False’, ’None’, ’True’, ’and’, ’as’, ’assert’, ’async’, ...>>> len(keyword.kwlist)35

那么,這些關鍵詞如何使用?在什么情景下應用?下面以示例的方式對部分關鍵詞進行說明。

True、False和None

True和False是布爾類型的兩個值,注意必須首字母大寫。

>>> x = True>>> x is TrueTrue>>> y = False>>> y is FalseTrue

如果我們要判斷某個對象的布爾值是True還是False,可以使用bool()函數實現,例如:

>>> x = 'this is a truthy value'>>> x is TrueFalse>>> bool(x) is TrueTrue>>> y = '' # 這個是假>>> y is FalseFalse>>> bool(y) is FalseTrue

注意,如果向bool()傳入的參數是0, '', {}, []中的任何一個,返回值都是False。

在條件語句中,本來是要判斷條件是否為True,但是,通常不需要直接與True或者False進行比較,依靠Python解析器自動進行條件判斷。

>>> x = 'this is a truthy value'>>> if x is True: # 不要這么做... print('x is True')...>>> if x: # 應該如此寫... print('x is truthy')...x is truthy

None這個關鍵詞,在Python中表示沒有值,其他語言中,同樣的含義可能會用null,nil,none,undef,undefined等。None也是函數中沒有return語句的時候默認返回值。

>>> def func():... print('hello')...>>> x = func()hello>>> print(x)None>>> def func():... print('hello')...>>> x = func()hello>>> print(x)None

and、or、not、in、is

這幾個關鍵詞,其實都對應著數學中的操作符,如下表所示。

數據符合 關鍵詞 AND, ∧ and OR, ∨ or NOT, ¬ not CONTAINS, ∈ in IDENTITY is

Python代碼具有很強的可讀性,通過關鍵詞就能一目了然曉得是什么操作。

這幾個關鍵詞比較好理解,這里僅提醒注意在Python中有一個著名的短路運算,例如and:

<expr1> and <expr2> 不要將上面的式子理解成兩邊都是真的時候返回True。對此,在**《Python大學實用教程》**一書中有非常詳細的說明,請參閱。另外一個就是or,也存在短路運算。

break、continue和else這幾個是經常用于循環語句的關鍵詞。

break 的作用是終止當前循環,其使用的基本格式:

for <element> in <container>: if <expr>: break舉個例子:>>> nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]>>> sum = 0>>> for num in nums:... sum += num... if sum > 10:... break...>>> sum15

continue則是要跳過某些循環,然后讓循環繼續。

for <element> in <container>: if <expr>:continue

else在條件語句中有,這里提到它,是在循環語句中,它的作用是當循環結束后還要繼續執行的代碼。

在for循環中,使用格式如下:

for <element> in <container>: <statements>else: <statements>

在while循環中,使用格式如下:

while <expr>: <statements>else: <statements>

例如,有時候我們要在循環語句中使用一個旗幟變量:

>>> for n in range(2, 10):... prime = True... for x in range(2, n):... if n % x == 0:... prime = False... print(f'{n} is not prime')... break... if prime:... print(f'{n} is prime!')...2 is prime!3 is prime!4 is not prime5 is prime!6 is not prime7 is prime!8 is not prime9 is not prime

在上面的代碼中,prime就是一個旗幟變量,如果循環正常結束,prime的值就是True,否則,就是False。如果從循環中退出了,第8行判斷這個變量的值,如果為True則打印相應內容。

對于上面的代碼,如果用else改寫,可以更簡潔,并且可讀性更強。

>>> for n in range(2, 10):... for x in range(2, n):... if n % x == 0:... print(f'{n} is not prime')... break... else:... print(f'{n} is prime!')...2 is prime!3 is prime!4 is not prime5 is prime!6 is not prime7 is prime!8 is not prime9 is not prime

以上就是Python必須了解的35個關鍵詞的詳細內容,更多關于Python 關鍵詞的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 国产成人综合欧美精品久久 | 国产视频一区二区在线播放 | 中文字幕中出在线 | 国产黄色a三级三级三级 | 亚洲国产成人久久精品图片 | 91精品一区二区 | 免费的色视频 | 闫盼盼福利 | 日本不卡中文字幕 | 成年网址网站在线观看 | 91中文字幕视频 | 免费黄色小视频 | 免费在线黄网 | 国产真实偷乱视频在线观看 | 桃色视频网 | 国产日本三级在线播放线观看 | 精品推荐国产麻豆剧传媒 | 亚洲国产一区在线精选 | 99v视频国产在线观看免费 | 欧美成在线播放 | 欧美成人精品欧美一级乱黄 | 成人男女网18免费软件大全 | sese欧美| 中日韩欧美中文字幕毛片 | 亚洲精品国产手机 | 欧美一级特黄特黄毛片 | 欧美精品在线看 | 国产日韩精品欧美一区喷水 | 伊人久久精品午夜 | 久久在线综合 | 亚洲不卡av不卡一区二区 | 黄色三及片 | 女人与zzzxxxx0oo0 | a级黄色大片在线观看视频男男 | 精品一区二区三区免费视频 | 国产精品观看在线亚洲人成网 | 美国老黑一级毛片免费的大黄 | 久久成年人 | 中文字幕一区二区三区四区 | 久久精品中文字幕有码日本 | 亚洲精品一区henhen色 |