The following loops are available in Python to fulfil the looping needs. Python offers 3 choices for running the loops. The basic functionality of all the techniques is the same, although the syntax and the amount of time required for checking the condition differ.
We can run a single statement or set of statements repeatedly using a loop command.
The following sorts of loops are available in the Python programming language.
Loop Control Statements
Statements used to control loops and change the course of iteration are called control statements. All the objects produced within the local scope of the loop are deleted when execution is completed.
Python provides the following control statements. We will discuss them later in detail.
Let us quickly go over the definitions of these loop control statements.
The for Loop
Python's for loop is designed to repeatedly execute a code block while iterating through a list, tuple, dictionary, or other iterable objects of Python. The process of traversing a sequence is known as iteration.
Syntax of the for Loop
In this case, the variable value is used to hold the value of every item present in the sequence before the iteration begins until this particular iteration is completed.
Loop iterates until the final item of the sequence are reached.
The list of squares is [16, 4, 36, 49, 9, 25, 64, 100, 36, 1, 81, 4]
Using else Statement with for Loop
As already said, a for loop executes the code block until the sequence element is reached. The statement is written right after the for loop is executed after the execution of the for loop is complete.
Only if the execution is complete does the else statement comes into play. It won't be executed if we exit the loop or if an error is thrown.
Here is a code to better understand if-else statements.
P y t h If block n L If block If block p
Now similarly, using else with for loop.
3 9 3 9 7 These are the odd numbers present in the tuple
The range() Function
With the help of the range() function, we may produce a series of numbers. range(10) will produce values between 0 and 9. (10 numbers).
We can give specific start, stop, and step size values in the manner range(start, stop, step size). If the step size is not specified, it defaults to 1.
Since it doesn't create every value it "contains" after we construct it, the range object can be characterized as being "slow." It does provide in, len, and __getitem__ actions, but it is not an iterator.
The example that follows will make this clear.
range(0, 15) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] [4, 5, 6, 7, 8] [5, 9, 13, 17, 21]
To iterate through a sequence of items, we can apply the range() method in for loops. We can use indexing to iterate through the given sequence by combining it with an iterable's len() function. Here's an illustration.
PYTHON LOOPS SEQUENCE CONDITION RANGE
While loops are used in Python to iterate until a specified condition is met. However, the statement in the program that follows the while loop is executed once the condition changes to false.
Syntax of the while loop is:
All the coding statements that follow a structural command define a code block. These statements are intended with the same number of spaces. Python groups statements together with indentation.
Python Loops Python Loops Python Loops Python Loops
Using else Statement with while Loops
As discussed earlier in the for loop section, we can use the else statement with the while loop also. It has the same syntax.
Python Loops Python Loops Python Loops Python Loops Code block inside the else statement
Single statement while Block
The loop can be declared in a single statement, as seen below. This is similar to the if-else block, where we can write the code block in a single line.
Loop Control Statements
Now we will discuss the loop control statements in detail. We will see an example of each control statement.
It returns the control to the beginning of the loop.
Current Letter: P Current Letter: y Current Letter: h Current Letter: n Current Letter: Current Letter: L Current Letter: s
It stops the execution of the loop when the break statement is reached.
Current Letter: P Current Letter: y Current Letter: t Current Letter: h Current Letter: o Current Letter: n Current Letter:
Pass statements are used to create empty loops. Pass statement is also employed for classes, functions, and empty control statements.
Last Letter: s
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week