White Science Python (8): the basis of flow control (lower)

Life is short, I chose Python

The foregoing Portal

White learn Python (1): Opening

White Science Python (2): basic data type (on)

White Science Python (3): fundamental data types (lower)

White Science Python (4): Variable Basic Operation

White Science Python (5): base operator (on)

White Science Python (6): base operator (lower)

White Science Python (7): based flow control (on)

Loop structure

Loop structure in general, is to keep doing something until you meet certain conditions.

Normal while loop

For example: learning to make me happy, I have been studying happiness.

It seems, and I said above, the definition a bit inconsistent, so I would add that, till I am not happy, then I will not learn.

Then the above scenario how to achieve this with the code?

happy = 0

while happy < 10:
    print("学习使我快乐,快乐 + 1,当前快乐值为:", happy)
    happy += 1

print("我不快乐了")

Results are as follows:

学习使我快乐,快乐 + 1,当前快乐值为: 0
学习使我快乐,快乐 + 1,当前快乐值为: 1
学习使我快乐,快乐 + 1,当前快乐值为: 2
学习使我快乐,快乐 + 1,当前快乐值为: 3
学习使我快乐,快乐 + 1,当前快乐值为: 4
学习使我快乐,快乐 + 1,当前快乐值为: 5
学习使我快乐,快乐 + 1,当前快乐值为: 6
学习使我快乐,快乐 + 1,当前快乐值为: 7
学习使我快乐,快乐 + 1,当前快乐值为: 8
学习使我快乐,快乐 + 1,当前快乐值为: 9
我不快乐了

The so-called extremes meet, happy to the extreme in the future is not happy, not unhappy to learn.

while infinite loop

Of course not rule out the nature there are always some chiefs, they have been able to learn and has been happy to go:

Then their code and ordinary people not the same,

happy = 0

while True:
    print("学习使大佬快乐,快乐 + 1,当前快乐值为:", happy)
    happy += 1

This is more ruthless, but there is a problem, now that big brother happy not stop, it can be supposed that the chiefs want to stop time can only be resorted happy cool down our big kill:

No, no, dig wrong wrong, it is this:

for loop

loop is generally used for the following format:

for <variable> in <sequence>:
    <statements>
else:
    <statements>

Here  <sequence> can be a string we have learned, it may be we will have to learn lists, tuples, dictionaries and so on.

Ado, let's take a look at a chestnut:

Wrong is wrong, it is this:

for index in "Python":
    print(index)

The output is this:

P
y
t
h
o
n

Here is equivalent to the cycle "Python" of each character in the string.

Then click Print.

To use the sequence numbers for loop iterates, can use the built range () function. Briefly experience the next:

for index in range(5):
    print(index)

The results are as follows:

0
1
2
3
4

Syntax: range (start, stop [, step])

  • start: start counting from the beginning.
  • stop: counting until stop, but not stop.
  • step: step, also called interval.

For example, to this:

Legs wayward, one can walk three steps, then it should be to achieve this:

for index in range(0, 10, 3):
    print(index)

Output:

0
3
6
9

Cyclic interrupt

We are in the learning process, there will always face a variety of temptations, such as:

这时,我们快乐的学习过程就不得不中断了。

而在 Python 中,中断循环有 break 和 continue 两种。

  • break :结束本次循环,跳出所在的循环。
  • continue :中断本次循环,继续进行下一次循环。

那么如果你去开黑,不回来学习了就是 break 。

我们用代码描述下这个场景:

happy = 0

while happy < 10:
    happy += 1
    if happy == 5:
        break
    print("学习使我快乐,快乐 + 1,当前快乐值为:", happy)

print("还是开黑更快乐一些~~~")

运行结果如下:

学习使我快乐,快乐 + 1,当前快乐值为: 1
学习使我快乐,快乐 + 1,当前快乐值为: 2
学习使我快乐,快乐 + 1,当前快乐值为: 3
学习使我快乐,快乐 + 1,当前快乐值为: 4
还是开黑更快乐一些~~~

当然,有时候也会有些特殊情况,比如开黑玩了一局以后,还是觉得学习更快乐,接着回来学习:

happy = 0

while happy < 10:
    happy += 1
    if happy == 5:
        continue
    print("学习使我快乐,快乐 + 1,当前快乐值为:", happy)

print("还是学习会更快乐~~~")

执行结果如下:

学习使我快乐,快乐 + 1,当前快乐值为: 1
学习使我快乐,快乐 + 1,当前快乐值为: 2
学习使我快乐,快乐 + 1,当前快乐值为: 3
学习使我快乐,快乐 + 1,当前快乐值为: 4
学习使我快乐,快乐 + 1,当前快乐值为: 6
学习使我快乐,快乐 + 1,当前快乐值为: 7
学习使我快乐,快乐 + 1,当前快乐值为: 8
学习使我快乐,快乐 + 1,当前快乐值为: 9
学习使我快乐,快乐 + 1,当前快乐值为: 10
还是学习会更快乐~~~

是不是在 happy == 5 的时候去打了一局王者发现还是发现学习更加快乐,小编相信每一位粉丝都是这样的人。

示例代码

本系列的所有代码小编都会放在代码管理仓库 Github 和 Gitee 上,方便大家取用。

示例代码-Github

示例代码-Gitee

 

转载声明:本博客由极客挖掘机创作,采用  CC BY 3.0 CN 许可协议。可自由转载、引用,但需署名作者且注明文章出处。

Guess you like

Origin www.cnblogs.com/aliswell2king/p/11753902.html