And 35-year-old Liu aunt with self-test process control basic Python

0, suddenly comes confusion

Flow control statements, Boolean, Boolean operators are all pythonbased successful. Great weekend I write this note, is not to sort out the basics points, but recorded several code segments so I suddenly confused. - the code itself is very simple, but even have mastered the basic skills, which suddenly saw a section of code, programming beginner or easily confused!

You do not want to read the full text together with me, the next check if they are firmly in the hands pythonof the flow control statements? Start now.

1, process control if the branch control statements

1.1 appetizer: paragraphs simple code

Run the following code fragment, what will print it?

A code segment

if 1 + 2 == 3 :
    print('1')
else:
    print('2')

Code segment B

if 1 + 3 == 3 :
    print('1')
else:
    print('2')

The above pythoncode is simple, and the writing is very common. AThe result is 1, Bthe result is 2. You got it, right?

1.2 witness the miracle: abstract look

Next time again witness the miracle ...... abstract look, the expression Boolean value of the above statement into Boolean values. The following two paragraphs after running the code, what will print it?

Code segment C

if True:
    print('1')
else:
    print('2')

Code segment D

if False:
    print('1')
else:
    print('2')

You're not hesitate to blurt out the correct answer is to say it? CThe result is 1, Dthe result is 2. You still have the answer yet?

1.3 principles: more understanding, less rote

My truth is, two days before the xue.cn do answer the challenge, see the code segment Dof this Code, suddenly thinking tie. You will also come across clearly mastered long ago, but suddenly a knowledge and understanding of incompetence atavistic situation?

I know this is a common phenomenon, so there is no sense of anxiety. I tried to think, to try to understand rather than rote knowledge to completely grasp this point.

I understand it this way. Snippet Dfirst branches are False, the other branch is placed in elsethe other branch of the first branch corresponding to the complement, i.e., not Falsei.e. True, so the code segment Dis equivalent to:

E snippet

if False:
    print('1')
elif True: 
    print('2')

Suit, snippet Cis equivalent to:

Snippet F

if True:
    print('1')
elif False:
    print('2')

Which can be found: ifthe branch control statements, will only trigger conditions for Truethat branch, condition Falsestatement does not trigger.

Originally, this note here seem to be able to organize ending. But I immediately think of: flow control statements, in addition to ifthe branch control, as well as whileand fortwo loop control, the control loop statement, what will be the law?

Strike while the iron is hot, can not stop, we continue to examine their own power base is solid, then comb it! - there is a cognitive psychology concluded that the association between control of knowledge, with a very good learning.

2, while the flow control for the loop control statements

2.1 Serving: read the code, self-test basic skills

Snippet G :

while False:
    print('1')
    break  # 测试代码,加这句是为了用于跳出循环,避免无限循环
else:
    print('2')

Snippet H :

while True:
    print('1')
    break # 测试代码,加这句是为了用于跳出循环,避免无限循环
else:
    print('2')

Snippet the I :

a = 1
while a < 4:
    print(a)
    a = a + 1
else:
    print('haha')

Code segment Gof the result is 2, the code segment Hof the result is 1. Code segment Iof the result is 1 2 3 haha.

Now look forcycle.

Snippet J :

for True:
    print(‘1’)
else:
    print('2')

Snippet K :

for i in range(4):
    print(i)
else:
    print('aha')

Code segment Jwill report an error, prompting a syntax error. Code segment Kresults0 1 2 3 aha

2.2 Finishing Summary: understanding based on finishing a deeper impression

Prior to combine with "self-learning craft is" learning basic skills python impression - if the impression is very light, indicating their review interval too long, to conform to the best memory curve can be adjusted; when finishing, it is best to turn immediately the corresponding chapter in the book review; combined with the previous self-test code, there are a variety of code to achieve, and now I can try to summarize:

  • In forand whileloop control statements, elsenot an integral part of.
  • whileThe body of the loop, the condition is only Truewhen the trigger condition has been Truehas been executed; conditions Falseor conditions become Falsethe part when it is finished.
  • for The loop, or the number of times the condition part generally range control cycle, and not Boolean value.
  • Loop control statements sometimes contain elseportions which elsebelong to whilepart of the overall control loop. Typically, elsewhen part of the cycle is finished after the trigger. The exception is that if the loop portion contains breakthe statement and is triggered, the entire out loop control, i.e., does not execute elsecode portions.

3. A brief summary

For me personally, forand whileloop control statements, self-test and review very relaxed, temporarily we did not find any doubts. Initially I was confused by the branch control statements: if Falsethe condition is Falsenot true, so the branch will not be executed. - If the process control statements comprise Boolean operations, then when the trigger condition is true only in the corresponding block of statements.

I read here with you and, if detected what place there own confusion? If so, I wish to refer to the practice, try writing different code, run it immediately, summed up the law and find it! Finally, Liu aunt then told two: more understanding, less rote, follow the memory curve, timely review, collate and analyze ways to consolidate with the deepening of the yo ~

Guess you like

Origin www.cnblogs.com/jjliu/p/11829639.html