The range() Function
The built-in function range() is the right function to iterate over a sequence of numbers. It generates an iterator of arithmetic progressions: The "for" loop
For loops can iterate over a sequence of numbers using the "range" and "xrange" functions. The difference between range and xrange is that the range function returns a new list with numbers of that specified range, whereas xrange returns an iterator, which is more efficient. (Python 3 uses the range function, which acts like xrange). Note that the range function is zero based. loop1.py
"while" loops
While loops repeat as long as a certain boolean condition is met. "break" and "continue" statements
break is used to exit a for loop or a while loop, whereas continue is used to skip the current block, and return to the "for" or "while" statement. can we use "else" clause for loops
We can use else for loops. When the loop condition of "for" or "while" statement fails then code part in "else" is executed. If break statement is executed inside for loop then the "else" part is skipped. Note that "else" part is executed even if there is a continue statement. |
Cheatography
https://cheatography.com
Loop - Python Cheat Sheet (DRAFT) by papapadzul
Python - Computer Language
This is a draft cheat sheet. It is a work in progress and is not finished yet.