官术网_书友最值得收藏!

Looping

The last concept I want to cover in our Python basics is looping, and we saw a couple of examples of this already, but let's just do another one:

for x in range(10):
print (x),

The output of the previous code is as follows:

0 1 2 3 4 5 6 7 8 9

For example, we can use this range operator to automatically define a list of numbers in the range. So if we say for x in range(10), range 10 will produce a list of 0 through 9, and by saying for x in that list, we will iterate through every individual entry in that list and print it out. Again, the comma after the print statement says don't give me a new line, just keep on going. So the output of this ends up being all the elements of that list printed next to each other.

To do something a little bit more complicated, we'll do something similar, but this time we'll show how continue and break work. As in other languages, you can actually choose to skip the rest of the processing for a loop iteration, or actually stop the iteration of the loop prematurely:

for x in range(10):
if (x is 1):
continue
if (x > 5):
break
print (x),

The output of the above code is as follows:

0 2 3 4 5

In this example, we'll go through the values 0 through 9, and if we hit on the number 1, we will continue before we print it out. We'll skip the number 1, basically, and if the number is greater than 5, we'll break the loop and stop the processing entirely. The output that we expect is that we will print out the numbers 0 through 5, unless it's 1, in which case, we'll skip number 1, and sure enough, that's what it does.

主站蜘蛛池模板: 邵阳市| 鹤峰县| 大田县| 神池县| 青川县| 津市市| 靖远县| 庆安县| 新巴尔虎右旗| 宿迁市| 嫩江县| 墨竹工卡县| 天全县| 新野县| 红河县| 元江| 镇坪县| 婺源县| 武平县| 梁河县| 正镶白旗| 澄江县| 错那县| 泸西县| 蒙城县| 绥宁县| 平定县| 上栗县| 大名县| 花垣县| 扶绥县| 醴陵市| 和硕县| 印江| 台南市| 百色市| 定远县| 稻城县| 手游| 新野县| 扎鲁特旗|