for-in Statement
Description
The for-in statement is used to execute a loop for each element in a list. The body that follows the list can be either a single statement, or set of statements inside braces ( { and } ). This statement executes as follows:
- A variable is set to the first or next element of the list.
- The body executes. If a break statement is encountered in the body, the loop terminates.
- Steps 1 and 2 are repeated while there are elements left in the list or until a break statement is encountered.
When the loop is complete, the variable contains the last value assigned to it.
Syntax
for variable in list body;
for name in {"one", "two", "three"} print(name);
The statement prints each element in the list.
For more information, see the following: