Python programming language provides following types of loops to handle looping requirements.
while expression: statement(s)
In Python, all the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code. Python uses indentation as its method of grouping statements.
Hello Geek Hello Geek Hello Geek
See this for an example where while loop is used for iterators. As mentioned in the article, it is not recommended to use while loop for iterators in python.
In Python, there is no C style for loop, i.e., for (i=0; i<n; i++). There is “for in” loop which is similar to for each loop in other languages.
for iterator_var in sequence: statements(s)
It can be used to iterate over iterators and a range.
List Iteration geeks for geeks Tuple Iteration geeks for geeks String Iteration G e e k s Dictionary Iteration xyz 123 abc 345
We can use for in loop for user defined iterators. See this for example.
Python programming language allows to use one loop inside another loop. Following section shows few examples to illustrate the concept.
for iterator_var in sequence: for iterator_var in sequence: statements(s) statements(s)
The syntax for a nested while loop statement in Python programming language is as follows:
while expression: while expression: statement(s) statement(s)
A final note on loop nesting is that we can put any type of loop inside of any other type of loop. For example a for loop can be inside a while loop or vice versa.
1 2 2 3 3 3 4 4 4 4
Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements.
It returns the control to the beginning of the loop.
Current Letter : g Current Letter : k Current Letter : f Current Letter : o Current Letter : r Current Letter : g Current Letter : k
It brings control out of the loop
Current Letter : e
We use pass statement to write empty loops. Pass is also used for empty control statement, function and classes.
Last Letter : s
How to print a list in reverse order (from last to first item) using while and for in loops.
This article is contributed by Ashirwad Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Looping Techniques in Python
- Keywords in Python | Set 2
- Keywords in Python | Set 1
- Programs for printing pyramid patterns in Python
- Counters in Python | Set 1 (Initialization and Updation)
- Cartooning an Image using OpenCV - Python
- Using Iterations in Python Effectively
- Iterators in Python
- Object Oriented Programming in Python | Set 1 (Class, Object and Members)
- Function Decorators in Python | Set 1 (Introduction)
- Python Modules
- What is the maximum possible value of an integer in Python ?
- Python | Set 2 (Variables, Expressions, Conditions and Functions)
- Python Language Introduction
- G-Fact 19 (Logical and Bitwise Not Operators on Boolean)