'for ... in ... else' Repeating Statement Blocks
How to enter "for ... in ... else" statements block in Python code?
"for ... in ... else" statement blocks allow you to execute the main statement block repeatedly.
When "for ... in ... else" statement blocks are interpreted, the "for" statement block will be executed repeatedly for each item in the given list. Then, the "else" statement block is executed once.
"for ... in ... else" statement blocks can be entered in Python code using the following syntax:
for item in list: statement-block else : statement-block
>>> words = ['cat', 'window', 'defenestrate'] >>> count = 0 >>> for w in words: ... count = count + 1 ... print("Item: "+w+", size: "+str(len(w))) ... else: ... print("Counts: "+str(count)) ... Item: cat, size: 3 Item: window, size: 6 Item: defenestrate, size: 12 Counts: 3
Note that Python uses indentation (leading spaces at the beginning of the statement) to indicate a sub statement block.
2017-09-12, 110👍, 0💬
What is this Website about? dev.FYIcenter.com is a Website for software developer looking for softwa...
Why Do You Need to Filter Out Empty Files in PHP? When you are processing uploaded files, you need t...
How To Return a Reference from a Function? in PHP? To return a reference from a function, you need t...
How To Run a PHP Script? A standard alone PHP script can be executed directly with the PHP Command L...
Where to find tutorials on how to create Your Own Functions in PHP? A collection of tutorials to ans...