page contents

Python关键字(保留字)是什么?新手入门需了解

保留字是 Python 语言中一些已经...

保留字是 Python 语言中一些已经被赋予特定意义的单词,这就要求开发者在开发程序时,不能用这些保留字作为标识符给变量、函数、类、模板以及其他对象命名。

 

Python 包含的保留字可以执行如下命令进行查看:

>>> import keyword

>>> keyword.kwlist

['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

 

attachments-2021-03-1lDbNBKn606183ed6ee3e.jpg


所有的保留字,如下表所示:

and

as

assert

break

class

continue

def

del

elif

esle

except

finally

for

from

False

global

if

import

in

is

lambds

nonlocal

not

None

or

pass

raise

return

try

True

while

with

yield




表1Python保留字--一览表

需要注意的是,由于 Python 是严格区分大小写的,保留字也不例外。所以,我们可以说 if 是保留字,但 IF 就不是保留字。

 

在实际开发中,如果使用 Python 中的保留字作为标识符,则解释器会提示“invalid syntax” 的错误信息。


attachments-2021-03-qAJcQbky606184048da79.jpg

  • 发表于 2021-03-29 15:48
  • 阅读 ( 699 )
  • 分类:Python开发

0 条评论

请先 登录 后评论
小柒
小柒

1316 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 1316 文章
  3. Pack 1135 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章