在编程中,关键字(keyword )是语言的“reserved word”,它向解释器传递特殊的含义。它可以是命令或参数。关键字不能在程序段中用作变量名。
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中的有效关键字的方法介绍,希望对需要的朋友有所帮助!
Atas ialah kandungan terperinci 如何检查字符串是否是Python中的有效关键字?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!