python写2000到2022循环
时间: 2023-05-28 12:01:09 浏览: 41
可以使用range函数循环2000到2022年:
```
for year in range(2000, 2023):
print(year)
```
输出:
```
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
```
相关问题
python写一个for循环
以下是一个使用for循环的Python代码示例[^1]:
```python
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
```
这个示例中,我们创建了一个名为fruits的列表,其中包含三种水果。然后,我们使用for循环遍历列表中的每个元素,并将其打印出来。在每次循环中,变量fruit都会被赋值为列表中的下一个元素。
python写个循环
当使用Python编写循环时,可以使用多种方式,其中最常用的是for循环和while循环。
1. for循环:
for循环用于遍历一个可迭代对象(如列表、元组、字符串等)中的元素。语法如下:
```
for 变量 in 可迭代对象:
循环体
```
示例:
```
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
```
输出:
```
apple
banana
cherry
```
2. while循环:
while循环用于在满足条件的情况下重复执行一段代码块。语法如下:
```
while 条件:
循环体
```
示例:
```
count = 0
while count < 5:
print(count)
count += 1
```
输出:
```
0
1
2
3
4
```