python的keyword模块用法实例分析

yipeiwu_com5年前Python基础

本文实例讲述了python的keyword模块用法。分享给大家供大家参考。具体如下:

Help on module keyword:
NAME
  keyword - Keywords (from "graminit.c")
FILE
  /usr/lib64/python2.6/keyword.py
DESCRIPTION
  This file is automatically generated; please don't muck it up!
  To update the symbols in this file, 'cd' to the top directory of
  the python source tree after building the interpreter and run:
    python Lib/keyword.py
FUNCTIONS
  iskeyword = __contains__(...)
    x.__contains__(y) <==> y in x.
DATA
  __all__ = ['iskeyword', 'kwlist']
  kwlist = ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', ...

得到python的关键字列表:

>>> keyword.kwlist
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

判断字符串是否是python的关键字

>>> keyword.iskeyword('and')
True
>>> 
>>> keyword.iskeyword('has')
False

希望本文所述对大家的Python程序设计有所帮助。

相关文章

python基础教程之while循环

python基础教程之while循环

前言 今天来说下python中的循环。循环的意思是什么,以环形、回路或轨道运行;沿曲折的路线运行;特指运行一周而回到原处。这是百度给出的循环的意思。在python中,就是重复执行你给的指...

Python 访问限制 private public的详细介绍

 一、知识点 在一个模块中,我们可能会定义很多函数和变量。但有的函数和变量我们希望能给别人使用,有的函数和变量我们希望仅仅在模块内部使用,so? 我们可以通过定义该函...

Python读写/追加excel文件Demo分享

三个工具包 python操作excel的三个工具包如下,注意,只能操作.xls,不能操作.xlsx。 • xlrd: 对excel进行读相关操作 • xlwt:...

详解PyCharm配置Anaconda的艰难心路历程

详解PyCharm配置Anaconda的艰难心路历程

在安装好pycharm后,想着anaconda中的类库会比较全,就想着将anaconda配置到pycharm中,这样可以避免以后下载各种类库。 第一步就是要下载并安装anaconda,在...

Python入门必须知道的11个知识点

Python被誉为全世界高效的编程语言,同时也被称作是“胶水语言”,那它为何能如此受欢迎,下面我们就来说说Python入门学习的必备11个知识点,也就是它为何能够如此受欢迎的原因. Py...