简体   繁体   中英

Python if-else on one line with continue statement

Similar to suggestions listed here , is it possible to do the same with the continue statement?

Something like this:

for x in range(10):
   continue if x<5

Thanks for the help

No, this is not possible in python you will have to revert to:

for x in range(10):
   if x<5:
     continue

However, like the comments pointed out you can make a one line if out of that:

if x < 5: continue

I would not recommend using if statements like that tho since it makes the code harder to read and you do not really gain anything from it.

You can use Python's one line if statements:

for x in range(10):
   if x < 5: continue
   print(x)

as explained here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM