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
2023-05-09, ∼2358🔥, 0💬
Popular Posts:
What is test testing area for? The testing area is provided to allow visitors to post testing commen...
How to use the RSS Online Validator at w3.org? You can follow this tutorial to learn how to use the ...
What is EPUB 3.0 Metadata "dc:description" Element? EPUB 3.0 Metadata "dc:description" is an optiona...
Where can I download the EPUB 2.0 sample book "The Metamorphosis" by Franz Kafka? You can following ...
How To Truncate an Array in PHP? If you want to remove a chunk of values from an array, you can use ...