| Start | ||||||||
|---|---|---|---|---|---|---|---|---|
Break and Continue in Loops |
||||||||
| For loops
The above Each iteration sets the value of
gives the exact same result as the first Iterable objects and iterators (opens new window)
|
||||||||
| Iterating over lists To iterate through a list you can use This will print out the elements of the list: The The result will be a special range sequence type (opens new window) in python >=3 and a list in python <=2. Both can be looped through using the for loop. If you want to loop though both the elements of a list and have an index for the elements as well, you can use Python's
Iterate over a list with value manipulation using Output: NB: in Python 3.x |
||||||||
| Loops with an "else" clause The The output: The output: Most other programming languages lack this optional The original concept for such a clause dates back to Donald Knuth and the meaning of the For example: is equivalent to: These remain equivalent if we attach an For example: is equivalent to: A Why would one use this strange construct?The main use case for the To make the Some discussions on this can be found in [Python-ideas] Summary of for...else threads (opens new window), Why does python use 'else' after for and while loops? , and Else Clauses on Loop Statements (opens new window) |
||||||||
| Iterating over dictionaries Considering the following dictionary: To iterate through its keys, you can use: Output: This is equivalent to: or in Python 2: To iterate through its values, use: Output: To iterate through its keys and values, use: Output: Note that in Python 2, The difference between Note also that in Python 3, Order of items printed in the above manner does not follow any order. |
||||||||
| The Pass Statement
In this example, nothing will happen. The Similarly, |
||||||||
| While Loop A While the above loop can easily be translated into a more elegant
If the condition is always true the while loop will run forever (infinite loop) if it is not terminated by a break or return statement or an exception. |
||||||||
| Iterating different portion of a list with different step size Suppose you have a long list of elements and you are only interested in every other element of the list. Perhaps you only want to examine the first or last elements, or a specific range of entries in your list. Python has strong indexing built-in capabilities. Here are some examples of how to achieve these scenarios. Here's a simple list that will be used throughout the examples: Iteration over the whole listTo iterate over each element in the list, a The Often you need both the element and the index of that element. The The index Iterate over sub-listIf we want to iterate over a range (remembering that Python uses zero-based indexing), use the This would output: The list may also be sliced. The following slice notation goes from element at index 1 to the end with a step of 2. The two The above snippet outputs: Indexing and slicing (opens new window) is a topic of its own. |
||||||||
| The "half loop" do-while Unlike other languages, Python doesn't have a do-until or a do-while construct (this will allow code to be executed once before the condition is tested). However, you can combine a This will print: |
||||||||
| Looping and Unpacking If you want to loop over a list of tuples for example: instead of doing something like this: or something like this: You can simply do this: This will also work for most types of iterables, not just tuples. Syntax
Parameters
|
Read Article
https://develop.horje.com/learn/1434/reference