1. Định nghĩa các từ khoá(Keyword)

Trong lập trình, một từ khóa là một “từ dành riêng” của ngôn ngữ mà nó truyền đạt một ý nghĩa đặc biệt cho trình thông dịch. Nó có thể là một lệnh hoặc một tham số. Không thể sử dụng từ khóa làm tên biến trong đoạn code chương trình.

Từ khóa trong Python: Ngôn ngữ Python cũng có một số từ khóa truyền đạt ý nghĩa đặc biệt. Kiến thức về những điều này là một phần cần thiết khi học ngôn ngữ này. Dưới đây là danh sách các từ khóa được đăng ký bởi 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,

2. Làm thế nào để kiểm tra xem một chuỗi có phải là từ khóa không?

Python trong ngôn ngữ của nó định nghĩa một mô-đun  keyword có sẵn xử lý các hoạt động nhất định liên quan đến từ khóa. Hàm “iskeyword ()” kiểm tra xem một chuỗi có phải là từ khóa hay không. Trả về true nếu một chuỗi là từ khóa, còn lại trả về false.

# -----------------------------------------------------------
#Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
#@author cafedevn
#Contact: cafedevn@gmail.com
#Fanpage: https://www.facebook.com/cafedevn
#Group: https://www.facebook.com/groups/cafedev.vn/
#Instagram: https://instagram.com/cafedevn
#Twitter: https://twitter.com/CafedeVn
#Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
#Pinterest: https://www.pinterest.com/cafedevvn/
#YouTube: https://www.youtube.com/channel/UCE7zpY_SlHGEgo67pHxqIoA/
# -----------------------------------------------------------

#Instead of writing this massive Python code
#we can also code this in a different way
 
#Python code to demonstrate working of iskeyword()
 
# importing "keyword" for keyword operations
import keyword
import keyword
# initializing strings for testing while putting them in an array
keys = ["for", "while", "tanisha", "break", "sky",
"elif", "assert", "pulkit", "lambda", "else", "sakshar"]
 
for i in range(len(keys)):
     # checking which are keywords
    if keyword.iskeyword(keys[i]):
        print(keys[i] + " is python keyword")
    else:
        print(keys[i] + " is not a python keyword")

output

for is 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

3. Cách in danh sách tất cả các từ khóa

Đôi khi, việc ghi nhớ tất cả các từ khóa có thể là một nhiệm vụ khó khăn trong khi gán tên biến. Do đó, một hàm “kwlist()” được cung cấp trong mô-đun “keyword” để in ra tất cả 33 từ khóa 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)

ouput

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

Cài ứng dụng cafedev để dễ dàng cập nhật tin và học lập trình mọi lúc mọi nơi tại đây.

Nguồn và Tài liệu tiếng anh tham khảo:

Tài liệu từ cafedev:

Nếu bạn thấy hay và hữu ích, bạn có thể tham gia các kênh sau của cafedev để nhận được nhiều hơn nữa:

Chào thân ái và quyết thắng!

Đăng ký kênh youtube để ủng hộ Cafedev nha các bạn, Thanks you!