Basics Tutorial python: Python cycle usage examples else, break and continue the Detailed

@ This article comes from public number: csdn2299, like the number of programmers can focus on public institutions

This article describes the use of Python loop else, break and continue a detailed analysis of the Python for loop with examples form, while loop else, break and continue functionality, usage and related handling precautions, refer a friend in need the
examples herein describes the use of Python cycle else, break and continue to. Share to you for your reference, as follows:

Looking at Python's time to see the documents for and while statements and the C language has one of the biggest difference - can have an optional else statement. The implementation of the trigger mechanism of this statement I do not quite understand, through code testing did not know the trigger else statement under what circumstances. "Where there is, are reasonable", Python designers certainly had a purpose, and now we come to explore some.

In order for the statement, for example, the development environment Python3.4.

Else the role of Python loop
presence circulation else is to make the code clearer and more concise. Below is an example for everyone to see, respectively, to achieve the same effect by writing the code with traditional writing and the else

Traditional C for loop format wording

myList = [1,2,3,4,5,6,7]
isFound = False
for item in myList:
  if item == 4:
    isFound = True
    print('List 有 4')
    break
if not isFound:
  print('List 没有 4')

Else statement written with a for loop

myList = [1,2,3,4,5,6,7]
  for item in myList:
  if item == 4:
    isFound = True
    print('List 有 4')
    break
else:
  print('List 没有 4')

Comparing two codes, not difficult to find compared to traditional loop for writing, with the wording else statements more concise and less isFound this variable and this out of the loop if the judge sentences.

to sum up

Under else structures for ...

for which statements and ordinary (not else for statement) makes no difference;
else statements will be executed after the implementation of the normal cycle;
when for the statement is interrupted by the break out of, will not be executed else.
Concluded one, for ... else structure is generally used together and break in order to reflect the power of this structure (at least I think so, I do not know what other cases, users want to let me know).

while ... else structure also meets the above criteria.

Python cycle continue and break
continue and break statement in fact and use C language is the same, just for ... else structure structure

When the for statement interrupted by a break in the jump, will not execute the contents else;
and will continue unusual statement and the statement is no different, if not not break through, there continue light will enter the else statement.
continue code sample

code show as below:

for x in range(1, 4):
  print(x, 'for语句')
  continue
  print(x, 'continue语句后')
else:
  print(x, 'else语句')

Results of the:

Statement 1 for
2 for statements
3 for sentence
3 else statements

break code sample

code show as below:

for x in range(1, 4):
  print(x, 'for语句')
  break
  print(x, 'break语句后')
else:
  print(x, 'else语句')

Results of the:

1 for statement

Thank you very much to read
in college chose the self-python, found that eating a working computer basic bad loss, this is not educated
can not do, can only be acquired to make up, then opened his own counter-attack outside the coding road, constantly learning python core knowledge, in-depth knowledge of basic computer learning, organized, if you are unwilling to mediocrity, it is with me outside of coding, growing it!
In fact, there is not only technical, more technical stuff than those, for example, how to make a fine programmer, rather than "Cock wire", the programmer itself is a noble presence, ah, is not it? [Click to join] want you want to be a noble person, come on!

Published 14 original articles · won praise 2 · Views 6672

Guess you like

Origin blog.csdn.net/chengxun03/article/details/105398021