python里33个关键字符是什么意思_如何检查字符串是否是Python中的有效关键字?...

在编程中,关键字(keyword )是语言的“reserved word”,它向解释器传递特殊的含义。它可以是命令或参数。关键字不能在程序段中用作变量名。

5c91da28afa34822.jpg

Python中的关键字:Python语言还保留了一些表达特殊含义的关键字。这些知识是学习这门语言的必要部分。下面是python的关键字列表。False, elif, lambda,

None, else, nonlocal,

True, except, not,

and, finally, or,

as, for, pass,

assert, from, raise,

break, global, return,

class, if, try,

continue, import, while,

def, in, with,

del, is, yield,

如何检查字符串是否是关键字?

Python在其语言中定义了一个内置模块“keyword”,它处理与关键字相关的某些操作。函数“iskeyword()”检查字符串是否为关键字。如果字符串是关键字,则返回true,否则返回false。#Python code to demonstrate working of iskeyword()

# importing "keyword" for keyword operations

import keyword

# initializing strings for testing

s = "for"

s1 = "geeksforgeeks"

s2 = "elif"

s3 = "elseif"

s4 = "nikhil"

s5 = "assert"

s6 = "shambhavi"

s7 = "True"

s8 = "False"

s9 = "akshat"

s10 = "akash"

s11 = "break"

s12 = "ashty"

s13 = "lambda"

s14 = "suman"

s15 = "try"

s16 = "vaishnavi"

# checking which are keywords

if keyword.iskeyword(s):

print ( s + " is a python keyword")

else : print ( s + " is not a python keyword")

if keyword.iskeyword(s1):

print ( s1 + " is a python keyword")

else : print ( s1 + " is not a python keyword")

if keyword.iskeyword(s2):

print ( s2 + " is a python keyword")

else : print ( s2 + " is not a python keyword")

if keyword.iskeyword(s3):

print ( s3 + " is a python keyword")

else : print ( s3 + " is not a python keyword")

if keyword.iskeyword(s4):

print ( s4 + " is a python keyword")

else : print ( s4 + " is not a python keyword")

if keyword.iskeyword(s5):

print ( s5 + " is a python keyword")

else : print ( s5 + " is not a python keyword")

if keyword.iskeyword(s6):

print ( s6 + " is a python keyword")

else : print ( s6 + " is not a python keyword")

if keyword.iskeyword(s7):

print ( s7 + " is a python keyword")

else : print ( s7 + " is not a python keyword")

if keyword.iskeyword(s8):

print ( s8 + " is a python keyword")

else : print ( s8 + " is not a python keyword")

if keyword.iskeyword(s9):

print ( s9 + " is a python keyword")

else : print ( s9 + " is not a python keyword")

if keyword.iskeyword(s10):

print ( s10 + " is a python keyword")

else : print ( s10 + " is not a python keyword")

if keyword.iskeyword(s11):

print ( s11 + " is a python keyword")

else : print ( s11 + " is not a python keyword")

if keyword.iskeyword(s12):

print ( s12 + " is a python keyword")

else : print ( s12 + " is not a python keyword")

if keyword.iskeyword(s13):

print ( s13 + " is a python keyword")

else : print ( s13 + " is not a python keyword")

if keyword.iskeyword(s14):

print ( s14 + " is a python keyword")

else : print ( s14 + " is not a python keyword")

if keyword.iskeyword(s15):

print ( s15 + " is a python keyword")

else : print ( s15 + " is not a python keyword")

if keyword.iskeyword(s16):

print ( s16 + " is a python keyword")

else : print ( s16 + " is not a python keyword")

输出:for is a python keyword

geeksforgeeks is not a python keyword

elif is a python keyword

elseif is not a python keyword

nikhil is not a python keyword

assert is a python keyword

shambhavi is not a python keyword

True is a python keyword

False is a python keyword

akshat is not a python keyword

akash is not a python keyword

break is a python keyword

ashty is not a python keyword

lambda is a python keyword

suman is not a python keyword

try is a python keyword

vaishnavi is not a python keyword

如何打印所有关键字的列表?

有时候,在分配变量名时,记住所有的关键字可能是比较困难。因此,在“keyword”模块中提供了一个函数“kwlist()”,它打印所有33个python关键字。#Python code to demonstrate working of iskeyword()

# importing "keyword" for keyword operations

import keyword

# printing all keywords at once using "kwlist()"

print ("The list of keywords is : ")

print (keyword.kwlist)

输出:The list of keywords is :

['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']

本篇文章就是关于检查字符串是否是Python中的有效关键字的方法介绍,希望对需要的朋友有所帮助!

以上就是如何检查字符串是否是Python中的有效关键字?的详细内容,更多请关注php中文网其它相关文章!

article_wechat2021.jpg?1111

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值