python 循环语句下载
时间: 2023-11-26 17:47:06 浏览: 108
以下是Python中两种循环语句的基本用法和示例代码:
1. for循环
```python
# 遍历列表
fruits = ['apple', 'banana', 'orange']
for fruit in fruits:
print(fruit)
# 遍历字符串
for char in 'hello':
print(char)
# 遍历字典
dict = {'name': 'Tom', 'age': 18}
for key, value in dict.items():
print(key, value)
```
2. while循环
```python
# 计算1到100的和
sum = 0
i = 1
while i <= 100:
sum += i
i += 1
print(sum)
```
阅读全文