Is Pass the Same as Continue Python

6 Answers 6

Using continue passes for the next iteration of the for loop
Using pass just does nothing
So when using continue the print won't happen (because the code continued to next iteration)
And when using pass it will just end the if peacefully (doing nothing actually) and do the print as well

answered May 9, 2016 at 20:22

Neo's user avatar

1

  • So if I understand this correctly, continue is saying "next value in loop please"? And only "continue" if the if statement is False?

    Mar 10, 2021 at 19:26

'0' not printed because of the condition "if not element:"

If the element is None, False, empty string('') or 0 then , loop will continue with next iteration.

answered May 9, 2016 at 20:34

Anoop's user avatar

3

  • It isn't limited to None and 0. It will continue if the element is Falsey.

    May 9, 2016 at 20:40

  • Does "if not 1" mean False? Then why 1 is printed with continue? In addition, if someone writes "if None", it will also continue without printing? Thanks.

    May 15, 2016 at 0:19

  • 1) Yes. if not 1 means False. In this case condition will fail and execution will continue with print statement. This is the reason for printing 1 in your code. If not <some value> will be True only if the value of <some value> is any falsey values like None, False, empty string('') or 0. 2) if None means always False

    May 15, 2016 at 18:48

              if not element:                          

In both examples, this will only match the 0.

              pass                          

This does nothing. So the next command, print element, will be executed.

              continue                          

This tells Python to stop this for loop cycle and skip to the next cycle of the loop. So print element will never be reached. Instead, the for loop will take the next value, 1 and start from the top.

answered May 9, 2016 at 20:23

C14L's user avatar

There is a fundamental difference between pass and continue in Python. pass simply does nothing, while continue jumps to the next iteration of the for loop. The statement if not 0 always evaluates to True, so both pass and continue statements will be executed. pass will do nothing and print the value, while continue will skip to the next iteration ignoring the print statement written below.

answered May 9, 2016 at 20:24

Vedang Mehta's user avatar

From: https://docs.python.org/2/tutorial/controlflow.html#pass-statements

The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action.

In your code snippet above if not element will evaluate to true when element = 0. In python 0 is same as boolean false. In the first loop pass does nothing, so it prints all three elements. In the second loop, continue will stop the execution of the rest of loop for that iteration. so the print statement never executes. so it only prints 1 and 2.

answered May 9, 2016 at 20:22

WreckeR's user avatar

continue is a control-flow statement used to escape the innermost body of iteration. When your code hits

              if not element                          

the interpreter skips for all values of element that don't validate totrue. 0 is one such value and it skips to the next iteration of the loop when it does not encounter the continue statement and therefore goes on to print the value of element 1 and thereafter 2

In contrast, the pass statement does nothing but skip and return to the next line of code to execute.

answered May 9, 2016 at 20:20

Spade's user avatar

pruittspecculp1971.blogspot.com

Source: https://stackoverflow.com/questions/37124717/if-pass-and-if-continue-in-python

0 Response to "Is Pass the Same as Continue Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel