Python的合法语句包括:
1. 赋值语句:
a, b, c = 'aa', 'bb', 'cc'
2. 函数调用:
log.write('spam,name')
3. 打印输出:
print('abc')
4. 条件语句(if, elif, else):
if 'iplaypython' in text:print(text)
5. 序列迭代(for):
for x in list:print(x)
6. 循环语句(while):
while a > b:print('hello')
7. 占位符(pass):
while True:pass
8. 退出循环(break):
while True:if exit:break
9. 跳过当前循环(continue):
while True:if skip:continue
10. 导入模块:
import math
11. 定义函数:

def my_function(x):return x * 2
12. 异常处理:
try:result = 10 / 0except ZeroDivisionError:print("Cannot divide by zero")
13. 类和对象:
class MyClass:def __init__(self, name):self.name = namemy_object = MyClass("example")print(my_object.name)
14. 列表推导式:
squares = [x2 for x in range(10)]
15. 异常捕获(except):
try:code that might raise an exceptionexcept SomeException as e:handle exception
16. 定义类(class):
class MyClass:def __init__(self, name):self.name = name
17. 定义函数(function):
def my_function(x):return x * 2
18. 定义模块(module):
my_module.pydef my_function(x):return x * 2
19. 定义包(package):
my_package/__init__.pyfrom . import my_module
20. 定义装饰器(decorator):
def my_decorator(func):def wrapper(*args, kwargs):print("Before function call")result = func(*args, kwargs)print("After function call")return resultreturn wrapper@my_decoratordef my_function(x):return x * 2
以上是一些Python中常见的合法语句,它们构成了Python语言的基本语法结构。需要注意的是,这些语句只是Python语言中的一部分,Python还支持更多的语法和功能
