利用元组创建一个存储Python关键字的对象,并检测给定的单词是否是Python关键字。 Python
时间: 2023-11-05 18:05:35 浏览: 129
的关键字有:
```
False, None, True, and, as, assert, async, await, 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_keywords = ('False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', '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关键字:
```
word = 'if'
if word in python_keywords:
print(word, 'is a Python keyword')
else:
print(word, 'is not a Python keyword')
```
输出:
```
if is a Python keyword
```