Python LoopsThe 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 StatementsStatements 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 LoopPython'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. Code Output: The list of squares is [16, 4, 36, 49, 9, 25, 64, 100, 36, 1, 81, 4] Using else Statement with for LoopAs 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. Code Output: P y t h If block n L If block If block p Now similarly, using else with for loop. Syntax: Code Output: 3 9 3 9 7 These are the odd numbers present in the tuple The range() FunctionWith 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. Code Output: 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. Code Output: PYTHON LOOPS SEQUENCE CONDITION RANGE While LoopWhile 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. Output: Python Loops Python Loops Python Loops Python Loops Using else Statement with while LoopsAs discussed earlier in the for loop section, we can use the else statement with the while loop also. It has the same syntax. Code Output: Python Loops Python Loops Python Loops Python Loops Code block inside the else statement Single statement while BlockThe 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. Code Loop Control StatementsNow we will discuss the loop control statements in detail. We will see an example of each control statement. Continue StatementIt returns the control to the beginning of the loop. Code Output: Current Letter: P Current Letter: y Current Letter: h Current Letter: n Current Letter: Current Letter: L Current Letter: s Break StatementIt stops the execution of the loop when the break statement is reached. Code Output: Current Letter: P Current Letter: y Current Letter: t Current Letter: h Current Letter: o Current Letter: n Current Letter: Pass StatementPass statements are used to create empty loops. Pass statement is also employed for classes, functions, and empty control statements. Code Output: Last Letter: s
Next TopicPython For Loop
|