Comprehensive Overview of Python's 35 Keywords with Explanations and Code Samples
This article presents a detailed tutorial of Python's 35 built‑in keywords, covering logical operators, control flow statements, data types, functions, classes, modules, and advanced constructs, each accompanied by clear explanations and runnable code examples to help beginners master fundamental Python syntax.
This guide enumerates all 35 Python keywords and provides concise explanations for each, grouped by their functional categories such as logical operators, conditional statements, loops, Boolean values, flow control, exception handling, module import, functions, classes, lambda expressions, variable scope, and asynchronous programming.
Logical operators are demonstrated with a simple example:
# 1. and, or, not (logical operators)
a = 5 > 3 and 1 < 2
b = 5 > 3 or 1 < 2
c = not (5 < 3)
print(a, b, c)Conditional statements (if, elif, else) are illustrated by prompting for an age and printing a corresponding category:
# 2. if, elif, else (conditional statements)
age = int(input("请输入您的年龄:"))
if age < 18:
print(f"您的岁数是{age},是一个未成年人")
elif age < 40:
print(f"您的岁数是{age},是一个青年人")
else:
print(f"您的岁数是{age},是一个中老年人")Loop constructs (for, while) are shown with iteration over a list and a counting loop:
# 3. for, while (loop statements)
languages = ["python", "java", "ruby"]
for i in languages:
print(i, end="|")
print("-----分隔符-----")
a = 0
while a < 10:
print(a, end="|")
a += 1Other keywords such as True/False, continue/break, pass, try/except/finally/raise, import/from/as, def/return, class, lambda, del, global/nonlocal, in/is, None, assert, with, yield, and async/await are each described with their purpose and a short code snippet, for example:
# 4. True, False (boolean values)
print(3 > 5)
print("p" in "python") # 5. continue, break (loop control)
for i in range(10):
if i == 5:
continue
print(i, end="|")
print()
for i in range(10):
if i == 5:
break
print(i, end="|") # 7. with (file handling)
with open("test001.txt", "r") as f:
print(f.read()) # 18. yield (generator)
def yield_test():
yield 10
print(yield_test())
print(next(yield_test())) # 19. async, await (asynchronous programming)
async def test2(i):
r = await other_test(i)
print(i, r)
async def other_test(i):
r = requests.get(i)
await asyncio.sleep(4)
return rEach section includes the expected output screenshots in the original article, reinforcing the learning experience for readers new to Python.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.