Tools, FAQ, Tutorials:
'for ... in ... else' Repeating Statement Blocks
How to enter "for ... in ... else" statements block in Python code?
✍: FYIcenter.com
"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
For example:
>>> 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.
⇒ 'for ... in' Statement with List of Tuples
⇐ 'if ... elif ... else' Conditional Statement Blocks
2017-09-12, 984👍, 0💬
Popular Posts:
Can You Specify the "new line" Character in Single-Quoted Strings? You can not specify the "new line...
Where to see some Examples of Invalid JSON Values? Here are some Examples of Invalid JSON Values: 1....
Where Is the Submitted Form Data Stored in PHP? When a user submit a form on your Web server, user e...
How To Control Vertical Alignment? By default, text in all table cells are aligned to the top vertic...
How to use the "rewrite-uri" Policy Statement for an Azure API service operation? The "rewrite-uri" ...