1 Loops1.1 For inLoops
[email protected] 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)
[email protected] python]# python!$
Python for.py
1
2
3
4
5
1
2
3
4
[3, 4]
(3, 4)
[email protected] python]# cat sum100.py
#!/usr/bin/python
#-*-Coding:utf-8-*-
Sum=0
L= list (range (101)) List (range (101)) generates An ordered integer of 0-100
For I in L:
Sum=sum+i
Print (sum)
l=Tuple (range (101))
For I in L:
Sum=sum+i
Print (sum)
[email protected] python]# python sum100.py
5050
10100
1.2 WhileLoops
As long as the conditions are met, the cycle is repeated, and the condition is not satisfied when exiting the loop.
[email protected] 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)
Delete the elements in L until you erase the
[email protected] python]# python listpop.py
4--4 is the number of elements in l
This article is from the "90SirDB" blog, be sure to keep this source http://90sirdb.blog.51cto.com/8713279/1795434
Python for &while Loop