1        循环

1.1   For in循环

[root@daidai python]# cat for.py

#!/usr/bin/python

# -*- coding:utf-8 -*-

 

list=[1,2,3,4,5]

for i in list:

    print(i)

 

list=(1,2,3,4,[3,4],(3,4))

for i in list:

print(i)

[root@daidai python]# python !$

python for.py

1

2

3

4

5

1

2

3

4

[3, 4]

(3, 4)

 

 

[root@daidai python]# cat sum100.py

#!/usr/bin/python

# -*- coding:utf-8 -*-

 

sum=0

l=list(range(101))   list(range(101))生成0-100的有序整数

for i in l:

   sum=sum+i

print(sum)

 

l=tuple(range(101))

for i in l:

   sum=sum+i

print(sum)

[root@daidai python]# python sum100.py

5050

10100

1.2   while循环

只要条件满足,就不断循环,条件不满足时退出循环。

[root@daidai python]# cat listpop.py

#!/usr/bin/python

# -*- coding:utf-8 -*-

 

l=[1,2,3,4]

num=0

while l:

   l.pop()

   num=num+1

print(num)

删除l中的元素,直到删尽

[root@daidai python]# python listpop.py

4  --4l中的元素个数