python循环语句

循环语句有while和for

while循环:

    while 循环条件:

        语句

for循环:用于遍历任何序列

    for 变量 in 序列:

        语句

break语句用于跳出当前循环体

continue语句用于跳过当前循环快中的剩余语句,然后继续进行下一轮循环

else子句,它在穷尽列表(for循环)或条件变为false(while循环)导致循环终止是被执行,但循环被break终止时不执行。

pass语句,空语句,为了保持程序结构的完整性,不做任何事,只是占个位置。

 如果需要遍历数字序列,可以使用内置函数range(),它会生成数字序列。

| range(stop) -> range object
| range(start, stop[, step]) -> range object
|
| Return an object that produces a sequence of integers from start (inclusive) to stop (exclusive) by step.

| range(i, j) produces i, i+1, i+2, ..., j-1.
| start defaults to 0, and stop is omitted!

| range(4) produces 0, 1, 2, 3. These are exactly the valid indices for a list of 4 elements.
| When step is given, it specifies the increment (or decrement).