There are four common flow control structures in Python, namely sequential structure, conditional structure, loop structure and jump structure. The following will introduce them one by one and provide corresponding code examples.
print("这是顺序结构示例1") print("这是顺序结构示例2") print("这是顺序结构示例3")
x = int(input("请输入一个整数: ")) if x > 0: print("输入的整数为正数") elif x < 0: print("输入的整数为负数") else: print("输入的整数为零")
# for循环示例 fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit) # while循环示例 count = 1 while count <= 5: print("当前数字为:", count) count += 1
# break示例 fruits = ["apple", "banana", "cherry"] for fruit in fruits: if fruit == "banana": break print(fruit) # continue示例 for i in range(1, 6): if i == 3: continue print("当前数字为:", i)
The above is an introduction and code examples of common process control structures in Python. For different scenarios, we can flexibly use these structures to achieve the functions we need.
The above is the detailed content of What are the common flow control structures in Python?. For more information, please follow other related articles on the PHP Chinese website!