博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
for 循环
阅读量:5914 次
发布时间:2019-06-19

本文共 2325 字,大约阅读时间需要 7 分钟。

# for 循环

while循环是可控,但是控制不好是不是就是一个死循环。

```python

wenyi_info_list = ['AA','BB','CC','DD','EE']
print(wenyi_info_list[0])
print(wenyi_info_list[1])
print(wenyi_info_list[2])
```

```

AA
BB
CC
```

 

```python

wengyi_info_list = ['AA','BB','CC']
len(wengyi_info_list)
```

 

```

3
```

 

```python

wenyi_info_list = ['wenyi', 150, 260, ['piao', '666', 'fish'],'zhangdechou',1,2,3,4,5,6,67,]
count = 0
while count < len(wenyi_info_list):
print(wenyi_info_list[count])
count += 1
```

```

wenyi
150
260
['piao', '666', 'fish']
zhangdechou
1
2
3
4
5
6
67
```

 

```python

wenyi_info_list = ['wenyi', 150, 260, ['piao', '666', 'fish'],'zhangdechou',1,2,3,4,5,6,67,]
for i in wenyi_info_list:
print(i)
```

```

wenyi
150
260
['piao', '666', 'fish']
zhangdechou
1
2
3
4
5
6
67
```

# for+break

```python

wenyi_info_list = ['wenyi', 150, 260, ['piao', '666', 'fish'],'zhangdechou',1,2,3,4,5,6,67,]
for i in wenyi_info_list:
if i == 'zhangdechou':
break
print(i)
```

```

wenyi
150
260
['piao', '666', 'fish']
```

# for+continue

```python

wenyi_info_list = ['wenyi', 150, 260, ['piao', '666', 'fish'],'zhangdechou',1,2,3,4,5,6,67,]
for i in wenyi_info_list:
if i =='zhangdechou':
continue
print(i)
```

```

wenyi
150
260
['piao', '666', 'fish']
1
2
3
4
5
6
67
```

# for else 如果for循环没有被break掉,则会运行else下面的代码

```python

wenyi_info_list = ['wenyi', 150, 260, ['piao', '666', 'fish'],'zhangdechou',1,2,3,4,5,6,67,]
for i in wenyi_info_list:
if i == 260:
break
print(i)
else:
print('我没有被break')
```

```

wenyi
150
```

 

```python

wenyi_info_list = ['wenyi', 150,['piao', '666', 'fish'],'zhangdechou',1,2,3,4,5,6,67,]
for i in wenyi_info_list:
if i == 260:
break
print(i)
else:
print('我没有被break')
```

```

wenyi
150
['piao', '666', 'fish']
zhangdechou
1
2
3
4
5
6
67
我没有被break
```

# while和for循环异同

异:while循环需要条件,for循环不需要条件

同:都是循环

# for循环实现loading......

```python

for i in range(0,6):
print('.')
```

```

.
.
.
.
.
.
```

 

```python

for i in range(0,6):
print('.',end = '')
```

```

......
```

```python

import time
s = 'loading'
print(s,end = '')
for i in range(0,6):
print('#',end='')
time.sleep(1)#字符出现时间间隔1秒
```

```

loading######
```

```python

import time
for i in range(0,6):
if i == 0:
print('[#',end = '')
if i == 4:
print('#]',end = '')
print('#',end = '')
time.sleep(0.5)
```

```

[######]##
```

```python

```

转载于:https://www.cnblogs.com/zrx19960128/p/10823329.html

你可能感兴趣的文章
Selenium IDE XPath元素定位器
查看>>
加入51CTO
查看>>
一个=号引发的错误.......
查看>>
CPU显卡内存与3DMAX渲染的关系
查看>>
【Java】方法重载于覆写的区别;This与Super的区别;Final关键字的作用
查看>>
10.2生成器
查看>>
VS2010编辑界面主题美化
查看>>
Linux 常用命令之touch
查看>>
RestTemplate设置通用header
查看>>
TRex 学习(2) ---- stateful (basic)
查看>>
[高并发Java 二] 多线程基础
查看>>
PHP源码目录结构
查看>>
Linux桌面虚拟化技术KVM介绍及其安装
查看>>
硬盘主引导记录详解
查看>>
用户与用户组管理
查看>>
CentOS 6.8 手工安装 Firefox
查看>>
【栈】POJ 1028 Web Navigation
查看>>
[文摘]JDK里的设计模式
查看>>
初学大数据需要了解哪些方面的知识?
查看>>
能量山水画家武湲承:搬运时空能量,聚藏山水画中
查看>>