0, suddenly comes confusion
Flow control statements, Boolean, Boolean operators are all python
based 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 python
of 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 python
code is simple, and the writing is very common. A
The result is 1
, B
the 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? C
The result is 1
, D
the 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 D
of 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 D
first branches are False
, the other branch is placed in else
the other branch of the first branch corresponding to the complement, i.e., not False
i.e. True
, so the code segment D
is equivalent to:
E snippet
if False:
print('1')
elif True:
print('2')
Suit, snippet C
is equivalent to:
Snippet F
if True:
print('1')
elif False:
print('2')
Which can be found: if
the branch control statements, will only trigger conditions for True
that branch, condition False
statement 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 if
the branch control, as well as while
and for
two 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 G
of the result is 2
, the code segment H
of the result is 1
. Code segment I
of the result is 1 2 3 haha
.
Now look for
cycle.
Snippet J :
for True:
print(‘1’)
else:
print('2')
Snippet K :
for i in range(4):
print(i)
else:
print('aha')
Code segment J
will report an error, prompting a syntax error. Code segment K
results0 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
for
andwhile
loop control statements,else
not an integral part of. while
The body of the loop, the condition is onlyTrue
when the trigger condition has beenTrue
has been executed; conditionsFalse
or conditions becomeFalse
the 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
else
portions whichelse
belong towhile
part of the overall control loop. Typically,else
when part of the cycle is finished after the trigger. The exception is that if the loop portion containsbreak
the statement and is triggered, the entire out loop control, i.e., does not executeelse
code portions.
3. A brief summary
For me personally, for
and while
loop 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 False
the condition is False
not 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 ~