一入python深似海--while-loop与for-loop
2014-06-08 16:36
351 查看
while loops
定义与实例i = 0 numbers = [] while i < 6: print "At the top i is %d" % i numbers.append(i) i = i + 1 print "Numbers now: ", numbers print "At the bottom i is %d" % i print "The numbers: " for num in numbers: print num,
输出
At the top i is 0 Numbers now: [0] At the bottom i is 1 At the top i is 1 Numbers now: [0, 1] At the bottom i is 2 At the top i is 2 Numbers now: [0, 1, 2] At the bottom i is 3 At the top i is 3 Numbers now: [0, 1, 2, 3] At the bottom i is 4 At the top i is 4 Numbers now: [0, 1, 2, 3, 4] At the bottom i is 5 At the top i is 5 Numbers now: [0, 1, 2, 3, 4, 5] At the bottom i is 6 The numbers: 0 1 2 3 4 5
for loops
定义和实例elements = [] # then use the range function to do 0 to 5 counts for i in range(0, 6): print "Adding %d to the list." % i # append is a function that lists understand elements.append(i)
A&Q
What's the difference between a for-loop anda while-loop?
ans: A for-loop can
only iterate (loop) "over" collections of things. A while-loop can
do any kind of iteration (looping) you want. However, while-loops are
harder to get right and you normally can get many things done with for-loops.
相关文章推荐
- Python if for else elif while break loop pass 控制流工具
- python-可变迭代对象在for循环中的风险Risk in FOR loop while looping mutable iterable object
- python的for和while竟然有else语句,我石化了
- PL/SQL基础编程之(四) [LOOP WHILE FOR][异常处理、自定义异常]
- Oracle中四种循环(GOTO、For、While、Loop)
- Python for, while循环后面加else的作用
- python 笔记 for loop and extend, append
- Loop like a native: while, for, iterators, generators
- Python学习笔记(九):if、while、for语句
- python list comprehension twos for loop 嵌套for循环
- 【原创】游标循环loop,while和for…
- android Monkeyrunner:Python语法知识学习(for,while,if用法实例)
- Python学习--控制语句:if, while, for, break, continue
- oracle编程 if, for , while, loop, 存储过程,触发器,游标演示
- Python学习笔记--for, while循环后面加else的作用
- 《笨办法学 Python》 学习笔记04 if-while-for
- python学习之语句:循环语句(for,while)
- python - summary with code on Python For loop e...
- 09-python_流程控制-for-while
- 如何熟练掌握python的if、while和for以及与它们相关的break和continue语句 通过什么样的形式, 做题吗?