ในโปรแกรมนี้ เราจะตรวจสอบว่าคำที่กำหนดเป็นคีย์เวิร์ดของ python หรือไม่ ในการดำเนินการดังกล่าว เราจะใช้ฟังก์ชัน iskeyword() จากคลังคำหลัก
อัลกอริทึม
Step 1: Import keyboard. Step 2: Check if the given string is a keyword or not.
โค้ดตัวอย่าง
import keyword
words = ["city", "music", "in", "if"]
for word in words:
if keyword.iskeyword(word):
print("{} is a keyword".format(word))
else:
print("{} is not a keyword".format(word)) ผลลัพธ์
city is not a keyword music is not a keyword in is a keyword if is a keyword