# for循环是Python中常用的控制流语句之一,用于遍历序列(如列表、元组、字符串)或其他可迭代对象。
# 示例1:遍历列表
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
# 输出:
# apple
# banana
# cherry
# 示例2:遍历字符串
for letter in "Python":
print(letter)
# 输出:
# P
# y
# t
# h
# o
# n
# 示例3:使用range()函数生成数字序列
for i in range(5):
print(i)
# 输出:
# 0
# 1
# 2
# 3
# 4
# 示例4:遍历字典
student_scores = {"Alice": 88, "Bob": 92, "Charlie": 85}
for student, score in student_scores.items():
print(f"{student}: {score}")
# 输出:
# Alice: 88
# Bob: 92
# Charlie: 85
上一篇:python pyautogui
下一篇:python ctypes
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站