for in python
时间: 2023-08-28 09:22:38 浏览: 61
In Python, the `for` loop is used to iterate over a sequence (such as a list, tuple, string, or range) or any other iterable object. It allows you to execute a block of code repeatedly for each item in the sequence.
Here's the basic syntax of the `for` loop in Python:
```python
for item in sequence:
# code to be executed for each item
```
In each iteration, the variable `item` takes on the value of the next item in the sequence. You can choose any valid variable name instead of `item`.
Here's an example that demonstrates how to use a `for` loop to iterate over a list and print each item:
```python
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
```
Output:
```
apple
banana
cherry
```
In this example, the `for` loop iterates over each item in the `fruits` list, and the variable `fruit` takes on the value of each item in each iteration.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)