python循环结构练习
时间: 2023-11-26 12:48:24 浏览: 113
python的循环结构
以下是两个Python循环结构的练习例子:
1. 输出区间内的素数
```python
from math import sqrt
a, b = map(int, input().split())
for i in range(a, b+1):
x = 2
while x <= int(sqrt(i)) and i % x != 0:
x += 1
if x > int(sqrt(i)):
print(i, end=' ')
```
2. 输出1到n之间的偶数
```python
n = int(input())
for i in range(1, n+1):
if i % 2 == 0:
print(i, end=' ')
```
阅读全文