blog/docs/code/python/loop.md

45 lines
915 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: 循环
date: 2020-09-6 17:25:10
tags: [Python]
categories: [Python]
author: Anges黎梦
---
### while
以代码来看一下 **while** 语句的语法
**示例代码**
```python
a = 0
b = []
while a<10:
# 当右边的表达式为真时循环执行while语句内的代码块
a += 1
b.append(a)
print(a)
else:
# 当表达式为假时,执行的代码块
print("---------------------")
```
### for
以代码来看一下 **while** 语句的语法
**示例代码**
```python
# 使用 in 循环
for i in b: # 循环输出列表中的每一个元素i为列表中的元素适用元组、集合、字典->循环的内容是字典中的key b[i]
print(i)
# 使用 range 循环
for j in range(0, 20, 2): # 遍历数字序列 start end step len()
print(j)
```
### continue 关键字
跳出当前循环,继续下一个循环
### break 关键字
跳出所有循环